1010
1111
1212class LiveLogParser (LogParser ):
13+ """
14+ LiveLogParser adds live log translation into useful data.
15+
16+ Lines are read and pushed into a deque by a separate thread.
17+ Deque is emptied by parse_worker which replaces the read()
18+ function of LogParser and it's also in a separate thread.
19+
20+ This approach is non-blocking and allows for live parsing
21+ of incoming lines.
22+ """
1323
1424 def __init__ (self , filepath ):
1525 super (LiveLogParser , self ).__init__ ()
@@ -18,13 +28,27 @@ def __init__(self, filepath):
1828 self .lines_deque = deque ([])
1929
2030 def new_packet_tree (self , ts ):
31+ """
32+ LivePacketTree is introduced here because it instantiates LiveEntityTreeExporter
33+ and keeps track of the parser parent. It also contains a function that
34+ utilizes the liveExporter instance across all the games.
35+
36+ self.parser = parser
37+ self.liveExporter = LiveEntityTreeExporter(self)
38+ """
2139 self ._packets = LivePacketTree (ts , self )
2240 self ._packets .spectator_mode = self .spectator_mode
2341 self ._packets .manager = PlayerManager ()
2442 self .current_block = self ._packets
2543 self .games .append (self ._packets )
2644
27- """ why is this return important? """
45+ """
46+ why is this return important?
47+ it's called only here:
48+
49+ def create_game(self, ts):
50+ self.new_packet_tree(ts)
51+ """
2852 return self ._packets
2953
3054 def tag_change (self , ts , e , tag , value , def_change ):
@@ -47,26 +71,35 @@ def tag_change(self, ts, e, tag, value, def_change):
4771 return packet
4872
4973 def register_packet (self , packet , node = None ):
50- """ make sure we"re registering packets to the current game"""
74+ """
75+ LogParser.register_packet override
76+
77+ This uses the live_export functionality introduces by LivePacketTree
78+ It also keeps track of which LivePacketTree is being used when there
79+ are multiple in parser.games
80+
81+ A better naming for a PacketTree/LivePacketTree would be HearthstoneGame?
82+ Then parser.games would contain HearthstoneGame instances and would
83+ be more obvious what the purpose is.
84+ """
85+
86+ # make sure we're registering packets to the current game
5187 if not self ._packets or self ._packets != self .games [- 1 ]:
5288 self ._packets = self .games [- 1 ]
5389
5490 if node is None :
5591 node = self .current_block .packets
5692 node .append (packet )
57-
58- """ line below triggers packet export which will
59- run update_callback for entity being
60- updated by the packet.
61-
62- self._packets == EntityTreeExporter
63- """
6493 self ._packets .live_export (packet )
65-
6694 self ._packets ._packet_counter += 1
6795 packet .packet_id = self ._packets ._packet_counter
6896
6997 def file_worker (self ):
98+ """
99+ File reader thread. (Naive implementation)
100+ Reads the log file continuously and appends to deque.
101+ """
102+
70103 file = open (self .filepath , "r" )
71104 while self .running :
72105 line = file .readline ()
@@ -76,6 +109,9 @@ def file_worker(self):
76109 time .sleep (0.2 )
77110
78111 def parse_worker (self ):
112+ """
113+ If deque contains lines, this initiates parsing.
114+ """
79115 while self .running :
80116 if len (self .lines_deque ):
81117 line = self .lines_deque .popleft ()
0 commit comments