Add contextmanager to load an example

This commit is contained in:
Shadab Zafar 2016-03-07 22:52:55 +05:30
parent b95cc63b87
commit af9442a9ba

View File

@ -1,4 +1,6 @@
import glob import glob
import os
from contextlib import contextmanager
from mitmproxy import utils, script from mitmproxy import utils, script
from mitmproxy.proxy import config from mitmproxy.proxy import config
@ -6,25 +8,28 @@ from netlib import tutils as netutils
from netlib.http import Headers from netlib.http import Headers
from . import tservers, tutils from . import tservers, tutils
from examples import ( example_dir = utils.Data(__name__).path("../../examples")
add_header,
custom_contentviews,
iframe_injector, @contextmanager
modify_form, def example(command):
modify_querystring, command = os.path.join(example_dir, command)
modify_response_body, # tmaster = tservers.TestMaster(config.ProxyConfig())
redirect_requests, # ctx = script.ScriptContext(tmaster)
) ctx = DummyContext()
s = script.Script(command, ctx)
yield s
s.unload()
class DummyContext(object): class DummyContext(object):
"""Emulate script.ScriptContext() functionality."""
def log(self, *args, **kwargs): def log(self, *args, **kwargs):
pass pass
def test_load_scripts(): def test_load_scripts():
example_dir = utils.Data(__name__).path("../../examples")
scripts = glob.glob("%s/*.py" % example_dir) scripts = glob.glob("%s/*.py" % example_dir)
tmaster = tservers.TestMaster(config.ProxyConfig()) tmaster = tservers.TestMaster(config.ProxyConfig())