fix recursive import

This commit is contained in:
Maximilian Hils 2017-05-26 16:22:13 +02:00
parent eb5f37a7d1
commit e7990e0983
5 changed files with 51 additions and 46 deletions

View File

@ -8,7 +8,6 @@ from mitmproxy.utils import human
import typing # noqa import typing # noqa
from . import generators from . import generators
from . import exceptions from . import exceptions
from . import message # noqa
class Settings: class Settings:
@ -539,43 +538,3 @@ class IntField(_Component):
def spec(self): def spec(self):
return "%s%s" % (self.preamble, self.origvalue) return "%s%s" % (self.preamble, self.origvalue)
class NestedMessage(Token):
"""
A nested message, as an escaped string with a preamble.
"""
preamble = ""
nest_type = None # type: typing.Optional[typing.Type[message.Message]]
def __init__(self, value):
Token.__init__(self)
self.value = value
try:
self.parsed = self.nest_type(
self.nest_type.expr().parseString(
value.val.decode(),
parseAll=True
)
)
except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col)
@classmethod
def expr(cls):
e = pp.Literal(cls.preamble).suppress()
e = e + TokValueLiteral.expr()
return e.setParseAction(lambda x: cls(*x))
def values(self, settings):
return [
self.value.get_generator(settings),
]
def spec(self):
return "%s%s" % (self.preamble, self.value.spec())
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
return self.__class__(TokValueLiteral(strutils.bytes_to_escaped_str(f.encode(), escape_single_quotes=True)))

View File

@ -253,7 +253,7 @@ class Response(_HTTPMessage):
return ":".join([i.spec() for i in self.tokens]) return ":".join([i.spec() for i in self.tokens])
class NestedResponse(base.NestedMessage): class NestedResponse(message.NestedMessage):
preamble = "s" preamble = "s"
nest_type = Response nest_type = Response

View File

@ -205,7 +205,7 @@ class Response(_HTTP2Message):
return ":".join([i.spec() for i in self.tokens]) return ":".join([i.spec() for i in self.tokens])
class NestedResponse(base.NestedMessage): class NestedResponse(message.NestedMessage):
preamble = "s" preamble = "s"
nest_type = Response nest_type = Response

View File

@ -1,8 +1,11 @@
import abc import abc
from . import actions, exceptions
from mitmproxy.utils import strutils
import typing # noqa import typing # noqa
import pyparsing as pp
from mitmproxy.utils import strutils
from . import actions, exceptions, base
LOG_TRUNCATE = 1024 LOG_TRUNCATE = 1024
@ -96,3 +99,46 @@ class Message:
def __repr__(self): def __repr__(self):
return self.spec() return self.spec()
class NestedMessage(base.Token):
"""
A nested message, as an escaped string with a preamble.
"""
preamble = ""
nest_type = None # type: typing.Optional[typing.Type[Message]]
def __init__(self, value):
super().__init__()
self.value = value
try:
self.parsed = self.nest_type(
self.nest_type.expr().parseString(
value.val.decode(),
parseAll=True
)
)
except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col)
@classmethod
def expr(cls):
e = pp.Literal(cls.preamble).suppress()
e = e + base.TokValueLiteral.expr()
return e.setParseAction(lambda x: cls(*x))
def values(self, settings):
return [
self.value.get_generator(settings),
]
def spec(self):
return "%s%s" % (self.preamble, self.value.spec())
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
return self.__class__(
base.TokValueLiteral(
strutils.bytes_to_escaped_str(f.encode(), escape_single_quotes=True)
)
)

View File

@ -236,7 +236,7 @@ class WebsocketFrame(message.Message):
return ":".join([i.spec() for i in self.tokens]) return ":".join([i.spec() for i in self.tokens])
class NestedFrame(base.NestedMessage): class NestedFrame(message.NestedMessage):
preamble = "f" preamble = "f"
nest_type = WebsocketFrame nest_type = WebsocketFrame