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):
|
class TaggedMessage(rfc1459.RFC1459Message):
|
||||||
def __init__(self, command, params, tags=None, **kw):
|
def __init__(self, tags=None, **kw):
|
||||||
if tags is None:
|
super().__init__(**kw)
|
||||||
tags = {}
|
self._kw['tags'] = tags
|
||||||
super().__init__(command, params, **kw)
|
self.__dict__.update(self._kw)
|
||||||
self.tags = tags
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
||||||
|
@ -40,6 +39,7 @@ class TaggedMessage(rfc1459.RFC1459Message):
|
||||||
message = message[:-len(pydle.protocol.LINE_SEPARATOR)]
|
message = message[:-len(pydle.protocol.LINE_SEPARATOR)]
|
||||||
elif message.endswith(pydle.protocol.MINIMAL_LINE_SEPARATOR):
|
elif message.endswith(pydle.protocol.MINIMAL_LINE_SEPARATOR):
|
||||||
message = message[:-len(pydle.protocol.MINIMAL_LINE_SEPARATOR)]
|
message = message[:-len(pydle.protocol.MINIMAL_LINE_SEPARATOR)]
|
||||||
|
raw = message
|
||||||
|
|
||||||
# Parse tags.
|
# Parse tags.
|
||||||
tags = {}
|
tags = {}
|
||||||
|
@ -57,7 +57,7 @@ class TaggedMessage(rfc1459.RFC1459Message):
|
||||||
|
|
||||||
# Parse rest of message.
|
# Parse rest of message.
|
||||||
message = super().parse(message.lstrip().encode(encoding), encoding=encoding)
|
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):
|
def construct(self, force=False):
|
||||||
"""
|
"""
|
||||||
|
@ -89,6 +89,13 @@ class TaggedMessageSupport(rfc1459.RFC1459Support):
|
||||||
def _enable_message_tags(self):
|
def _enable_message_tags(self):
|
||||||
self._message_tags_enabled = True
|
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):
|
def _parse_message(self):
|
||||||
if self._message_tags_enabled:
|
if self._message_tags_enabled:
|
||||||
sep = rfc1459.parsing.MINIMAL_LINE_SEPARATOR.encode(self.encoding)
|
sep = rfc1459.parsing.MINIMAL_LINE_SEPARATOR.encode(self.encoding)
|
||||||
|
|
|
@ -5,12 +5,13 @@ from . import protocol
|
||||||
|
|
||||||
class RFC1459Message(pydle.protocol.Message):
|
class RFC1459Message(pydle.protocol.Message):
|
||||||
def __init__(self, command, params, source=None, _raw=None, _valid=True, **kw):
|
def __init__(self, command, params, source=None, _raw=None, _valid=True, **kw):
|
||||||
self.command = command
|
self._kw = kw
|
||||||
self.params = params
|
self._kw['command'] = command
|
||||||
self.source = source
|
self._kw['params'] = params
|
||||||
self.kw = kw
|
self._kw['source'] = source
|
||||||
self._valid = _valid
|
self._valid = _valid
|
||||||
self._raw = _raw
|
self._raw = _raw
|
||||||
|
self.__dict__.update(self._kw)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
def parse(cls, line, encoding=pydle.protocol.DEFAULT_ENCODING):
|
||||||
|
|
Loading…
Reference in New Issue