addons: an addonmanager.Loader is now passed to the load() event

This commit is contained in:
Aldo Cortesi 2017-03-24 10:32:37 +13:00
parent 541c1e8b9f
commit d69a411303
2 changed files with 32 additions and 3 deletions

View File

@ -1,3 +1,5 @@
import typing
from mitmproxy import exceptions
from mitmproxy import eventsequence
from mitmproxy import controller
@ -9,6 +11,30 @@ def _get_name(itm):
return getattr(itm, "name", itm.__class__.__name__.lower())
class Loader:
"""
A loader object is passed to the load() event when addons start up.
"""
def __init__(self, master):
self.master = master
def add_option(
self,
name: str,
typespec: type,
default: typing.Any,
help: str,
choices: typing.Optional[typing.Sequence[str]] = None
) -> None:
self.master.options.add_option(
name,
typespec,
default,
help,
choices
)
class AddonManager:
def __init__(self, master):
self.chain = []
@ -41,8 +67,9 @@ class AddonManager:
"""
self.chain.extend(addons)
with self.master.handlecontext():
l = Loader(self.master)
for i in addons:
self.invoke_addon(i, "load", self.master.options)
self.invoke_addon(i, "load", l)
def remove(self, addon):
"""

View File

@ -6,6 +6,7 @@ import threading
import traceback
import types
from mitmproxy import addonmanager
from mitmproxy import exceptions
from mitmproxy import ctx
from mitmproxy import eventsequence
@ -184,10 +185,11 @@ class Script:
def load_script(self):
self.ns = load_script(self.path, self.args)
ret = self.run("load", self.last_options)
l = addonmanager.Loader(ctx.master)
ret = self.run("load", l)
if ret:
self.ns = ret
self.run("load", self.last_options)
self.run("load", l)
def tick(self):
if self.should_reload.is_set():