diff options
| author | Yuval Adam <_@yuv.al> | 2018-03-29 21:47:26 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-04-02 11:16:06 +0300 |
| commit | 442a166869859b6c3e0d1119bbad60fc054580e3 (patch) | |
| tree | ea24f2e6b73de9de55b5f36f38fc0aa9c976151a | |
| parent | aca380284ca619cd08ede20bc204e9d03a5409c5 (diff) | |
Things looking good
| -rw-r--r-- | tests/test_parse.py | 8 | ||||
| -rw-r--r-- | viewstate/parse.py | 41 |
2 files changed, 28 insertions, 21 deletions
diff --git a/tests/test_parse.py b/tests/test_parse.py index 1a83ac5..570ab96 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -15,16 +15,14 @@ class TestNewParse(object): vs = ViewState(raw=b'\xff\x01\x02\x88\x01') assert vs.decode() == 136 -@pytest.mark.skip() -class TestParse(object): - - - def test_string_value(self): s = 'abcdefghij' vs = ViewState(raw=b'\xff\x01\x05' + bytes([len(s)]) + s.encode()) assert vs.decode() == s + +@pytest.mark.skip() +class TestParse(object): def test_simple_dict(self): vs = ViewState(raw=b'\xff\x01\x18\x02\x05\x01a\x05\x01b\x05\x01c\x05\x01d') assert vs.decode() == {'a': 'b', 'c': 'd'} diff --git a/viewstate/parse.py b/viewstate/parse.py index 3c213ca..99fe677 100644 --- a/viewstate/parse.py +++ b/viewstate/parse.py @@ -9,18 +9,20 @@ class ParserMeta(type): if not hasattr(cls, 'registry'): cls.registry = {} if hasattr(cls, 'marker'): - print('registry', cls) - cls.registry[getattr(cls, 'marker')] = cls + marker = getattr(cls, 'marker') + if type(marker) not in (tuple, list): + marker = [marker] + for m in marker: + cls.registry[m] = cls class Parser(metaclass=ParserMeta): - def parse(self, b): - marker, *remain = b + marker, remain = b[0], b[1:] try: return Parser.registry[marker]().parse(remain) except KeyError: - raise ViewStateException('Unknown marker') + raise ViewStateException(f'Unknown marker {marker}') class Const(Parser): @@ -28,22 +30,29 @@ class Const(Parser): return self.const, remain +class NoneConst(Const): + marker = 0x64 + const = None + + +class EmptyConst(Const): + marker = 0x65 + const = '' + + +class ZeroConst(Const): + marker = 0x66 + const = 0 + + class TrueConst(Const): marker = 0x67 const = True -CONSTS = { - 0x64: None, - 0x65: '', - 0x66: 0, - 0x67: True, - 0x68: False -} - - -def parse_const(b): - return CONSTS.get(b, None) +class FalseConst(Const): + marker = 0x68 + const = False class Integer(Parser): |
