Skip to content

Commit

Permalink
Clean up and clarify script API
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Jan 12, 2014
1 parent 58e1b3a commit e5776b8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
10 changes: 6 additions & 4 deletions libmproxy/console/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,14 @@ def run_script_once(self, path, f):
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:
Expand Down
21 changes: 5 additions & 16 deletions libmproxy/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,17 +1394,6 @@ def add_event(self, e, level="info"):
"""
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)
Expand All @@ -1414,11 +1403,11 @@ def load_script(self, script_argv):
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:
Expand Down
1 change: 1 addition & 0 deletions libmproxy/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, argv, master):
self.argv = argv
self.ctx = ScriptContext(master)
self.ns = None
self.load()

def load(self):
"""
Expand Down
15 changes: 5 additions & 10 deletions test/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,24 @@ def test_err(self):
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):
Expand Down Expand Up @@ -106,8 +102,7 @@ def test_concurrent2(self):
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
)

0 comments on commit e5776b8

Please sign in to comment.