aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-04-29 14:20:27 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-04-29 14:20:27 +1200
commit6d4500c67943d03cf0910256ef9dab46789f366f (patch)
tree7d2d68047e6241c081560a042d4dcdeaae0dbffe /libpathod
parentf03ce8107075567ea902b88ace2964ee035d45aa (diff)
downloadmitmproxy-6d4500c67943d03cf0910256ef9dab46789f366f.tar.gz
mitmproxy-6d4500c67943d03cf0910256ef9dab46789f366f.tar.bz2
mitmproxy-6d4500c67943d03cf0910256ef9dab46789f366f.zip
Regularize language even further
- Unify offset specifcation syntax, so there's now oly one form for specify pauses and disconnects. - Use @ instead of ! for content generation. Mildy more convenient in the shell.
Diffstat (limited to 'libpathod')
-rw-r--r--libpathod/rparse.py93
1 files changed, 41 insertions, 52 deletions
diff --git a/libpathod/rparse.py b/libpathod/rparse.py
index b47681cc..de69e9b9 100644
--- a/libpathod/rparse.py
+++ b/libpathod/rparse.py
@@ -146,18 +146,18 @@ class ValueGenerate:
@classmethod
def expr(klass):
- e = pp.Literal("!").suppress() + v_integer
+ e = pp.Literal("@").suppress() + v_integer
u = reduce(operator.or_, [pp.Literal(i) for i in klass.UNITS.keys()])
e = e + pp.Optional(u, default=None)
- s = pp.Literal("-").suppress()
+ s = pp.Literal(",").suppress()
s += reduce(operator.or_, [pp.Literal(i) for i in DATATYPES.keys()])
e += pp.Optional(s, default="bytes")
return e.setParseAction(lambda x: klass(*x))
def __str__(self):
- return "!%s%s-%s"%(self.usize, self.unit, self.datatype)
+ return "@%s%s,%s"%(self.usize, self.unit, self.datatype)
class ValueFile:
@@ -245,60 +245,50 @@ class Body:
return e.setParseAction(lambda x: klass(*x))
-class _Pause:
- def __init__(self, value):
- self.value = value
+class PauseAt:
+ def __init__(self, seconds, offset):
+ self.seconds, self.offset = seconds, offset
@classmethod
def expr(klass):
- e = pp.Literal("p%s"%klass.sub).suppress()
- e = e + pp.MatchFirst(
+ e = pp.Literal("p").suppress()
+ e +=pp.MatchFirst(
+ [
+ v_integer,
+ pp.Literal("f")
+ ]
+ )
+ e += pp.Literal(",").suppress()
+ e += pp.MatchFirst(
[
v_integer,
- pp.Literal("forever")
+ pp.Literal("r"),
+ pp.Literal("a"),
]
)
return e.setParseAction(lambda x: klass(*x))
-
-class PauseBefore(_Pause):
- sub = "b"
- def mod_response(self, settings, r):
- r.actions.append((0, "pause", self.value))
-
-
-class PauseAfter(_Pause):
- sub = "a"
- def mod_response(self, settings, r):
- r.actions.append((sys.maxint, "pause", self.value))
-
-
-class PauseRandom(_Pause):
- sub = "r"
def mod_response(self, settings, r):
- r.actions.append(("random", "pause", self.value))
+ r.actions.append((self.offset, "pause", self.seconds))
-class _Disconnect:
+class DisconnectAt:
def __init__(self, value):
self.value = value
- @classmethod
- def expr(klass):
- e = pp.Literal("d%s"%klass.sub)
- return e.setParseAction(klass)
-
-
-class DisconnectBefore(_Disconnect):
- sub = "b"
def mod_response(self, settings, r):
- r.actions.append((0, "disconnect"))
+ r.actions.append((self.value, "disconnect"))
-
-class DisconnectRandom(_Disconnect):
- sub = "r"
- def mod_response(self, settings, r):
- r.actions.append(("random", "disconnect"))
+ @classmethod
+ def expr(klass):
+ e = pp.Literal("d").suppress()
+ e = e + pp.MatchFirst(
+ [
+ v_integer,
+ pp.Literal("r")
+ ]
+ )
+ return e.setParseAction(lambda x: klass(*x))
class Header:
@@ -346,11 +336,8 @@ class Response:
comps = (
Body,
Header,
- PauseBefore,
- PauseAfter,
- PauseRandom,
- DisconnectBefore,
- DisconnectRandom,
+ PauseAt,
+ DisconnectAt,
ShortcutContentType,
ShortcutLocation,
)
@@ -391,13 +378,15 @@ class Response:
l += len(self.body)
return l
- def ready_randoms(self, l, lst):
+ def ready_actions(self, l, lst):
ret = []
- for k, v in lst:
- if k == "random":
- ret.append((random.randrange(l), v))
- else:
- ret.append((k, v))
+ for i in lst:
+ itms = list(i)
+ if i[0] == "r":
+ itms[0] = random.randrange(l)
+ if i[0] == "a":
+ itms[0] = l+1
+ ret.append(tuple(itms))
ret.sort()
return ret
@@ -469,7 +458,7 @@ class Response:
self.body
])
vals.reverse()
- actions = self.ready_randoms(self.length(), self.actions)
+ actions = self.ready_actions(self.length(), self.actions)
actions.reverse()
return self.write_values(fp, vals, actions)