mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 02:10:59 +00:00
Clean up and clarify script API
This commit is contained in:
parent
58e1b3a47f
commit
e5776b8be3
@ -446,12 +446,14 @@ class ConsoleMaster(flow.FlowMaster):
|
||||
if not path:
|
||||
return
|
||||
self.add_event("Running script on flow: %s"%path)
|
||||
ret = self.get_script(shlex.split(path, posix=(os.name != "nt")))
|
||||
if ret[0]:
|
||||
|
||||
try:
|
||||
s = script.Script(shlex.split(path, posix=(os.name != "nt")), self)
|
||||
except script.ScriptError, v:
|
||||
self.statusbar.message("Error loading script.")
|
||||
self.add_event("Error loading script:\n%s"%ret[0])
|
||||
self.add_event("Error loading script:\n%s"%v.args[0])
|
||||
return
|
||||
s = ret[1]
|
||||
|
||||
if f.request:
|
||||
self._run_script_method("request", s, f)
|
||||
if f.response:
|
||||
|
@ -1394,17 +1394,6 @@ class FlowMaster(controller.Master):
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_script(self, script_argv):
|
||||
"""
|
||||
Returns an (error, script) tuple.
|
||||
"""
|
||||
s = script.Script(script_argv, self)
|
||||
try:
|
||||
s.load()
|
||||
except script.ScriptError, v:
|
||||
return (v.args[0], None)
|
||||
return (None, s)
|
||||
|
||||
def unload_script(self, script):
|
||||
script.unload()
|
||||
self.scripts.remove(script)
|
||||
@ -1414,11 +1403,11 @@ class FlowMaster(controller.Master):
|
||||
Loads a script. Returns an error description if something went
|
||||
wrong.
|
||||
"""
|
||||
r = self.get_script(script_argv)
|
||||
if r[0]:
|
||||
return r[0]
|
||||
else:
|
||||
self.scripts.append(r[1])
|
||||
try:
|
||||
s = script.Script(script_argv, self)
|
||||
except script.ScriptError, v:
|
||||
return v.args[0]
|
||||
self.scripts.append(s)
|
||||
|
||||
def run_single_script_hook(self, script, name, *args, **kwargs):
|
||||
if script and not self.pause_scripts:
|
||||
|
@ -49,6 +49,7 @@ class Script:
|
||||
self.argv = argv
|
||||
self.ctx = ScriptContext(master)
|
||||
self.ns = None
|
||||
self.load()
|
||||
|
||||
def load(self):
|
||||
"""
|
||||
|
@ -41,28 +41,24 @@ class TestScript:
|
||||
s = flow.State()
|
||||
fm = flow.FlowMaster(None, s)
|
||||
|
||||
s = script.Script(["nonexistent"], fm)
|
||||
tutils.raises(
|
||||
"no such file",
|
||||
s.load
|
||||
script.Script, ["nonexistent"], fm
|
||||
)
|
||||
|
||||
s = script.Script([tutils.test_data.path("scripts")], fm)
|
||||
tutils.raises(
|
||||
"not a file",
|
||||
s.load
|
||||
script.Script, [tutils.test_data.path("scripts")], fm
|
||||
)
|
||||
|
||||
s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], fm)
|
||||
tutils.raises(
|
||||
script.ScriptError,
|
||||
s.load
|
||||
script.Script, [tutils.test_data.path("scripts/syntaxerr.py")], fm
|
||||
)
|
||||
|
||||
s = script.Script([tutils.test_data.path("scripts/loaderr.py")], fm)
|
||||
tutils.raises(
|
||||
script.ScriptError,
|
||||
s.load
|
||||
script.Script, [tutils.test_data.path("scripts/loaderr.py")], fm
|
||||
)
|
||||
|
||||
def test_concurrent(self):
|
||||
@ -106,8 +102,7 @@ class TestScript:
|
||||
def test_concurrent_err(self):
|
||||
s = flow.State()
|
||||
fm = flow.FlowMaster(None, s)
|
||||
s = script.Script([tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm)
|
||||
tutils.raises(
|
||||
"decorator not supported for this method",
|
||||
s.load
|
||||
script.Script, [tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user