Skip to content

Commit

Permalink
Extract command parsing and use in script grid editor
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Jan 13, 2014
1 parent 42d4a2f commit 4f69eef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
11 changes: 6 additions & 5 deletions libmproxy/console/grideditor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy, re, os
import urwid
import common
from .. import utils, filt
from .. import utils, filt, script
from netlib import http_uastrings


Expand Down Expand Up @@ -486,8 +486,9 @@ class PathEditor(GridEditor):
class ScriptEditor(GridEditor):
title = "Editing scripts"
columns = 1
headings = ("Path",)
headings = ("Command",)
def is_error(self, col, val):
return False


try:
script.Script.parse_command(val)
except script.ScriptError, v:
return str(v)
19 changes: 12 additions & 7 deletions libmproxy/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,31 @@ class Script:
"""
def __init__(self, command, master):
self.command = command
self.argv = shlex.split(command, posix=(os.name != "nt"))
self.argv = self.parse_command(command)
self.ctx = ScriptContext(master)
self.ns = None
self.load()

@classmethod
def parse_command(klass, command):
args = shlex.split(command, posix=(os.name != "nt"))
args[0] = os.path.expanduser(args[0])
if not os.path.exists(args[0]):
raise ScriptError("Command not found.")
elif not os.path.isfile(args[0]):
raise ScriptError("Not a file: %s" % args[0])
return args

def load(self):
"""
Loads a module.
Raises ScriptError on failure, with argument equal to an error
message that may be a formatted traceback.
"""
path = os.path.expanduser(self.argv[0])
if not os.path.exists(path):
raise ScriptError("No such file: %s" % path)
if not os.path.isfile(path):
raise ScriptError("Not a file: %s" % path)
ns = {}
try:
execfile(path, ns, ns)
execfile(self.argv[0], ns, ns)
except Exception, v:
raise ScriptError(traceback.format_exc(v))
self.ns = ns
Expand Down
2 changes: 1 addition & 1 deletion test/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_err(self):
fm = flow.FlowMaster(None, s)

tutils.raises(
"no such file",
"not found",
script.Script, "nonexistent", fm
)

Expand Down

0 comments on commit 4f69eef

Please sign in to comment.