Skip to content

Commit

Permalink
Adding handling for Trillain informational messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed Oct 19, 2018
1 parent 3e9140a commit 455f3b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion chat_unifier/parsers/trillian/line_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class InvalidMessageType(Error):
'SessionStopLine',
field_names=['timestamp', 'medium', 'sender', 'recipient'])

InformationalMessageLine = collections.namedtuple(
'InformationalMessageLine', field_names=['timestamp', 'medium', 'contents'])

OutgoingPrivateMessageLine = collections.namedtuple(
'OutgoingPrivateMessageLine',
field_names=[
Expand Down Expand Up @@ -86,14 +89,23 @@ def _parse_message_attributes(attributes):
except KeyError:
raise InvalidMessageType('Message element has no \'type\' attribute')

if message_type == u'outgoing_privateMessage':
if message_type == u'information_standard':
return _parse_informational_message(attributes)
elif message_type == u'outgoing_privateMessage':
return _parse_outgoing_message(attributes)
elif message_type == u'incoming_privateMessage':
return _parse_incoming_message(attributes)
else:
raise InvalidMessageType('Unrecognized message type: %s' % message_type)


def _parse_informational_message(attributes):
return InformationalMessageLine(
timestamp=_parse_timestamp_attribute(attributes[u'time']),
medium=attributes[u'medium'],
contents=_decode_message_text(attributes[u'text']))


def _parse_outgoing_message(attributes):
return OutgoingPrivateMessageLine(
timestamp=_parse_timestamp_attribute(attributes[u'time']),
Expand Down
10 changes: 10 additions & 0 deletions tests/parsers/trillian/test_line_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ def test_parses_valid_incoming_private_message_line(self):
'<message type="incoming_privateMessage" time="1124933025" medium="AIM" to="LocalUser222" from="RemoteBuddy555" from_display="Steve" text="hmm%2E%2E%2E%20no%20thanks"/>'
))

def test_parses_valid_informational_message_line(self):
self.assertEqual(
line_parser.InformationalMessageLine(
timestamp=datetime.datetime(2005, 9, 11, 3, 35, 37),
medium='AIM',
contents='"Steve" signed on at Sat Sep 10 23:35:37 2005.'),
line_parser.parse(
'<message type="information_standard" time="1126409737" medium="AIM" text="%22Steve%22%20signed%20on%20at%20Sat%20Sep%2010%2023%3A35%3A37%202005%2E"/>'
))

def test_invalid_message_type_raises_exception(self):
with self.assertRaises(line_parser.InvalidMessageType):
line_parser.parse('<message type="dummy_invalid_type" />')
Expand Down

0 comments on commit 455f3b3

Please sign in to comment.