mirror of https://github.com/Shizmob/pydle.git
Implement creation of tagged messages.
This commit is contained in:
parent
d065d1a780
commit
3045fc2fd1
|
@ -11,11 +11,10 @@ TAGGED_MESSAGE_LENGTH_LIMIT = 1024
|
|||
|
||||
|
||||
class TaggedMessage(rfc1459.RFC1459Message):
|
||||
def __init__(self, command, params, tags=None, **kw):
|
||||
if tags is None:
|
||||
tags = {}
|
||||
super().__init__(command, params, **kw)
|
||||
self.tags = tags
|
||||
def __init__(self, tags=None, **kw):
|
||||
super().__init__(**kw)
|
||||
self._kw['tags'] = tags
|
||||
self.__dict__.update(self._kw)
|
||||
|
||||
@classmethod
|
||||
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
||||
|
@ -40,6 +39,7 @@ class TaggedMessage(rfc1459.RFC1459Message):
|
|||
message = message[:-len(pydle.protocol.LINE_SEPARATOR)]
|
||||
elif message.endswith(pydle.protocol.MINIMAL_LINE_SEPARATOR):
|
||||
message = message[:-len(pydle.protocol.MINIMAL_LINE_SEPARATOR)]
|
||||
raw = message
|
||||
|
||||
# Parse tags.
|
||||
tags = {}
|
||||
|
@ -57,7 +57,7 @@ class TaggedMessage(rfc1459.RFC1459Message):
|
|||
|
||||
# Parse rest of message.
|
||||
message = super().parse(message.lstrip().encode(encoding), encoding=encoding)
|
||||
return TaggedMessage(message.command, message.params, _raw=message._raw, _valid=message._valid and valid, source=message.source, tags=tags, **message.kw)
|
||||
return TaggedMessage(_raw=raw, _valid=message._valid and valid, tags=tags, **message._kw)
|
||||
|
||||
def construct(self, force=False):
|
||||
"""
|
||||
|
@ -89,6 +89,13 @@ class TaggedMessageSupport(rfc1459.RFC1459Support):
|
|||
def _enable_message_tags(self):
|
||||
self._message_tags_enabled = True
|
||||
|
||||
def _create_message(self, command, *params, tags={}, **kwargs):
|
||||
message = super()._create_message(command, *params, **kwargs)
|
||||
if self._message_tags_enabled:
|
||||
return TaggedMessage(tags=tags, **message._kw)
|
||||
else:
|
||||
return message
|
||||
|
||||
def _parse_message(self):
|
||||
if self._message_tags_enabled:
|
||||
sep = rfc1459.parsing.MINIMAL_LINE_SEPARATOR.encode(self.encoding)
|
||||
|
|
|
@ -5,12 +5,13 @@ from . import protocol
|
|||
|
||||
class RFC1459Message(pydle.protocol.Message):
|
||||
def __init__(self, command, params, source=None, _raw=None, _valid=True, **kw):
|
||||
self.command = command
|
||||
self.params = params
|
||||
self.source = source
|
||||
self.kw = kw
|
||||
self._kw = kw
|
||||
self._kw['command'] = command
|
||||
self._kw['params'] = params
|
||||
self._kw['source'] = source
|
||||
self._valid = _valid
|
||||
self._raw = _raw
|
||||
self.__dict__.update(self._kw)
|
||||
|
||||
@classmethod
|
||||
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
||||
|
|
Loading…
Reference in New Issue