]> git.treefish.org Git - mtxbot.git/blob - src/inputparser.py
be more tolerant on fifo writing
[mtxbot.git] / src / inputparser.py
1 from message import Message
2
3 class InputParser:
4     def __init__(self):
5         self._messages = {}
6         self._decoded = []
7
8     def feed_line(self, line):
9         try:
10             msg_id, msg_fragment = line.rstrip("\n").split(' ', 1)
11         except:
12             raise Exception("Invalid fragment!")
13
14         try:
15             if msg_id in self._messages:
16                 self._messages[msg_id].feed_fragment(msg_fragment)
17             else:
18                 self._messages[msg_id] = Message(msg_fragment)
19
20             if self._messages[msg_id].is_complete():
21                 self._decoded.append( self._messages[msg_id].get_content() )
22                 del self._messages[msg_id]
23
24         except Exception as e:
25             if msg_id in self._messages:
26                 del self._messages[msg_id]
27             raise e
28
29     def fetch_decoded(self):
30         decoded = self._decoded
31         self._decoded = []
32         return decoded