diff --git a/libpathod/language/base.py b/libpathod/language/base.py index f91add752..5b9e99f6c 100644 --- a/libpathod/language/base.py +++ b/libpathod/language/base.py @@ -64,6 +64,9 @@ class Token(object): """ Resolves this token to ready it for transmission. This means that the calculated offsets of actions are fixed. + + settings: a language.Settings instance + msg: The containing message """ return self @@ -76,7 +79,7 @@ class _ValueLiteral(Token): self.val = val.decode("string_escape") def get_generator(self, settings): - return generators.LiteralGenerator(self.val) + return self.val def freeze(self, settings): return self @@ -215,13 +218,14 @@ Offset = pp.MatchFirst( class _Component(Token): """ A value component of the primary specification of an message. + Components produce byte values desribe the bytes of the message. """ @abc.abstractmethod def values(self, settings): # pragma: no cover """ - A sequence of value objects. + A sequence of values, which can either be strings or generators. """ - return None + pass def string(self, settings=None): """ @@ -361,7 +365,7 @@ class Integer(_Component): return e.setParseAction(lambda x: klass(*x)) def values(self, settings): - return [generators.LiteralGenerator(self.value)] + return self.value def spec(self): return "%s"%(self.value) diff --git a/libpathod/language/generators.py b/libpathod/language/generators.py index 58be7130d..07b0be06a 100644 --- a/libpathod/language/generators.py +++ b/libpathod/language/generators.py @@ -43,23 +43,6 @@ class TransformGenerator: return "'transform(%s)'"%self.gen -class LiteralGenerator: - def __init__(self, s): - self.s = s - - def __len__(self): - return len(self.s) - - def __getitem__(self, x): - return self.s.__getitem__(x) - - def __getslice__(self, a, b): - return self.s.__getslice__(a, b) - - def __repr__(self): - return "'%s'"%self.s - - class RandomGenerator: def __init__(self, dtype, length): self.dtype = dtype diff --git a/libpathod/language/http.py b/libpathod/language/http.py index c1c2ae96e..f926a8cf1 100644 --- a/libpathod/language/http.py +++ b/libpathod/language/http.py @@ -81,7 +81,7 @@ class ShortcutUserAgent(_HeaderMixin, base.OptionsOrValue): self.value.val.lower() )[2] else: - value = self.value + value = self.value.val return self.format_header( self.key.get_generator(settings), value @@ -129,7 +129,7 @@ class _HTTPMessage(message.Message): vals.extend(h.values(settings)) vals.append("\r\n") if self.body: - vals.append(self.body.value.get_generator(settings)) + vals.extend(self.body.values(settings)) return vals @@ -169,11 +169,9 @@ class Response(_HTTPMessage): l.extend(self.reason.values(settings)) else: l.append( - generators.LiteralGenerator( - http_status.RESPONSES.get( - code, - "Unknown code" - ) + http_status.RESPONSES.get( + code, + "Unknown code" ) ) return l @@ -205,7 +203,9 @@ class Response(_HTTPMessage): if not self.body: length = 0 else: - length = len(self.body.value.get_generator(settings)) + length = sum( + len(i) for i in self.body.values(settings) + ) tokens.append( Header( base.ValueLiteral("Content-Length"), @@ -301,7 +301,9 @@ class Request(_HTTPMessage): if not self.raw: if not get_header("Content-Length", self.headers): if self.body: - length = len(self.body.value.get_generator(settings)) + length = sum( + len(i) for i in self.body.values(settings) + ) tokens.append( Header( base.ValueLiteral("Content-Length"), diff --git a/test/test_language_base.py b/test/test_language_base.py index deb333176..65c253da5 100644 --- a/test/test_language_base.py +++ b/test/test_language_base.py @@ -208,7 +208,7 @@ class TestMisc: assert e.freeze({}) assert e.values({}) - def test_value(self): + def test_prevalue(self): class TT(base.PreValue): preamble = "m" e = TT.expr() @@ -226,6 +226,7 @@ class TestMisc: class TKeyValue(base.KeyValue): preamble = "h" + def values(self, settings): return [ self.key.get_generator(settings), diff --git a/test/test_language_generators.py b/test/test_language_generators.py index 854b84857..945560c33 100644 --- a/test/test_language_generators.py +++ b/test/test_language_generators.py @@ -14,13 +14,6 @@ def test_randomgenerator(): assert g[0] -def test_literalgenerator(): - g = generators.LiteralGenerator("one") - assert repr(g) - assert g[:] == "one" - assert g[1] == "n" - - def test_filegenerator(): with tutils.tmpdir() as t: path = os.path.join(t, "foo") @@ -41,7 +34,7 @@ def test_filegenerator(): def test_transform_generator(): def trans(offset, data): return "a" * len(data) - g = generators.LiteralGenerator("one") + g = "one" t = generators.TransformGenerator(g, trans) assert len(t) == len(g) assert t[0] == "a" diff --git a/test/test_language_http.py b/test/test_language_http.py index f9d2cf24b..512ad2772 100644 --- a/test/test_language_http.py +++ b/test/test_language_http.py @@ -309,11 +309,11 @@ def test_shortcuts(): def test_user_agent(): e = http.ShortcutUserAgent.expr() v = e.parseString("ua")[0] - assert "Android" in str(v.values({})[2]) + assert "Android" in v.string() e = http.ShortcutUserAgent.expr() v = e.parseString("u'a'")[0] - assert "Android" not in str(v.values({})[2]) + assert "Android" not in v.string() v = e.parseString("u@100'")[0] assert len(str(v.freeze({}).value)) > 100