aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/language/base.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2015-05-03 13:54:52 +1200
committerAldo Cortesi <aldo@nullcube.com>2015-05-03 13:54:52 +1200
commit5b6d3a80bbb57faa1de1cc7730d28b0678b0bdee (patch)
tree0f22273c92b0bd89464d66ef49c510aa33273344 /libpathod/language/base.py
parent67d2993339cb81ff61509c57807df77dde436a3d (diff)
downloadmitmproxy-5b6d3a80bbb57faa1de1cc7730d28b0678b0bdee.tar.gz
mitmproxy-5b6d3a80bbb57faa1de1cc7730d28b0678b0bdee.tar.bz2
mitmproxy-5b6d3a80bbb57faa1de1cc7730d28b0678b0bdee.zip
websockets: code specification
Diffstat (limited to 'libpathod/language/base.py')
-rw-r--r--libpathod/language/base.py42
1 files changed, 40 insertions, 2 deletions
diff --git a/libpathod/language/base.py b/libpathod/language/base.py
index 41855da3..725a0d42 100644
--- a/libpathod/language/base.py
+++ b/libpathod/language/base.py
@@ -291,14 +291,19 @@ class OptionsOrValue(_Component):
Can be any of a specified set of options, or a value specifier.
"""
preamble = ""
+ options = []
def __init__(self, value):
# If it's a string, we were passed one of the options, so we lower-case
# it to be canonical. The user can specify a different case by using a
# string value literal.
self.option_used = False
if isinstance(value, basestring):
- value = TokValueLiteral(value.lower())
- self.option_used = True
+ for i in self.options:
+ # Find the exact option value in a case-insensitive way
+ if i.lower() == value.lower():
+ self.option_used = True
+ value = TokValueLiteral(i)
+ break
self.value = value
@classmethod
@@ -369,3 +374,36 @@ class Value(_Component):
def freeze(self, settings):
return self.__class__(self.value.freeze(settings))
+
+
+class IntField(_Component):
+ """
+ An integer field, where values can optionally specified by name.
+ """
+ names = {}
+ max = 16
+ preamble = ""
+
+ def __init__(self, value):
+ self.origvalue = value
+ self.value = self.names.get(value, value)
+ if self.value > self.max:
+ raise exceptions.ParseException(
+ "Value can't exceed %s"%self.max, 0, 0
+ )
+
+ @classmethod
+ def expr(klass):
+ parts = [pp.CaselessLiteral(i) for i in klass.names.keys()]
+ m = pp.MatchFirst(parts)
+ spec = m | v_integer.copy()
+ spec = spec.setParseAction(lambda x: klass(*x))
+ if klass.preamble:
+ spec = pp.Literal(klass.preamble).suppress() + spec
+ return spec
+
+ def values(self, settings):
+ return [str(self.value)]
+
+ def spec(self):
+ return "%s%s"%(self.preamble, self.origvalue)