44 class LogSource (object) : |
44 class LogSource (object) : |
45 """ |
45 """ |
46 A collection of IRC logs for a specific target in some format. Provides the possibility to read specific events |
46 A collection of IRC logs for a specific target in some format. Provides the possibility to read specific events |
47 """ |
47 """ |
48 |
48 |
49 def __init__ (self, decoder) : |
49 def __init__ (self, decoder, channel=None) : |
50 """ |
50 """ |
51 Use the given LogSourceDecoder |
51 The appropriate LogChannel must be given, as we need to be able to construct the LogLines. If it is not yet |
52 """ |
52 known, then it can be given as None, and set later with bind_channel. |
53 |
53 |
|
54 Uses the given LogSourceDecoder to decode the lines. |
|
55 """ |
|
56 |
|
57 self.channel = channel |
54 self.decoder = decoder |
58 self.decoder = decoder |
55 |
59 |
|
60 def bind_channel (self, channel) : |
|
61 """ |
|
62 Set this source's channel, where None was set before |
|
63 """ |
|
64 |
|
65 assert not self.channel |
|
66 |
|
67 self.channel = channel |
|
68 |
56 def get_latest (self, count) : |
69 def get_latest (self, count) : |
57 """ |
70 """ |
58 Yield the latest events, up to `count` of them. |
71 Yield the latest events, up to `count` of them. |
59 """ |
72 """ |
60 |
73 |
154 A file containing LogEvents |
167 A file containing LogEvents |
155 |
168 |
156 XXX: modify to implement LogSource? |
169 XXX: modify to implement LogSource? |
157 """ |
170 """ |
158 |
171 |
159 def __init__ (self, path, parser, decoder, start_date=None, sep='\n') : |
172 def __init__ (self, path, parser, decoder, channel=None, start_date=None, sep='\n') : |
160 """ |
173 """ |
161 Open the file at the given path, which contains lines as separated by the given separator. Lines are |
174 Open the file at the given path, which contains lines as separated by the given separator. Lines are |
162 decoded using the given LogSourceDecoder, and then parsed using the given parser, using the given date |
175 decoded using the given LogSourceDecoder, and then parsed using the given parser, using the given date |
163 as the initial date for this log's first line. |
176 as the initial date for this log's first line. |
164 |
177 |
165 XXX: currently we assume start_date also for the end of the file |
178 XXX: currently we assume start_date also for the end of the file |
166 """ |
179 """ |
167 |
180 |
168 # store |
181 # store |
|
182 self.channel = channel |
169 self.path = path |
183 self.path = path |
170 self.parser = parser |
184 self.parser = parser |
171 self.start_date = start_date |
185 self.start_date = start_date |
172 self.decoder = decoder |
186 self.decoder = decoder |
173 self.sep = sep |
187 self.sep = sep |
190 """ |
204 """ |
191 Reads all LogLines. The LogLines will have a valid offset. |
205 Reads all LogLines. The LogLines will have a valid offset. |
192 """ |
206 """ |
193 |
207 |
194 # just use our __iter__ |
208 # just use our __iter__ |
195 return self.parser.parse_lines(self, self.start_date, starting_offset=1) |
209 return self.parser.parse_lines(self.channel, self, self.start_date, starting_offset=1) |
196 |
210 |
197 def read_from (self, dt) : |
211 def read_from (self, dt) : |
198 """ |
212 """ |
199 Reads all LogLines from the given naive timestamp onwards |
213 Reads all LogLines from the given naive timestamp onwards |
200 """ |
214 """ |
314 break |
328 break |
315 |
329 |
316 # decode in reverse order, using our starting date.... |
330 # decode in reverse order, using our starting date.... |
317 # XXX: use lines[::-1] or reversed? |
331 # XXX: use lines[::-1] or reversed? |
318 # XXX: it may make more sense to parse in reverse order, using 'self.end_date' or something like that |
332 # XXX: it may make more sense to parse in reverse order, using 'self.end_date' or something like that |
319 return self.parser.parse_lines(reversed(lines), self.start_date) |
333 return self.parser.parse_lines(self.channel, reversed(lines), self.start_date) |
320 |
334 |
321 class LogDirectory (LogSource) : |
335 class LogDirectory (LogSource) : |
322 """ |
336 """ |
323 A directory containing a series of timestamped LogFiles |
337 A directory containing a series of timestamped LogFiles |
324 """ |
338 """ |
325 |
339 |
326 def __init__ (self, path, tz, parser, decoder, filename_fmt) : |
340 def __init__ (self, path, tz, parser, decoder, filename_fmt, channel=None) : |
327 """ |
341 """ |
328 Load the logfiles at the given path. |
342 Load the logfiles at the given path, which are for the given LogChannel |
329 |
343 |
330 Decode the file lines using the given decoder, the files are named according the the date in the given |
344 Decode the file lines using the given decoder, the files are named according the the date in the given |
331 timezone and date format, and will be parsed using the given parser. |
345 timezone and date format, and will be parsed using the given parser. |
332 """ |
346 """ |
333 |
347 |
334 # store |
348 # store |
|
349 self.channel = channel |
335 self.path = path |
350 self.path = path |
336 self.tz = tz |
351 self.tz = tz |
337 self.parser = parser |
352 self.parser = parser |
338 self.decoder = decoder |
353 self.decoder = decoder |
339 self.filename_fmt = filename_fmt |
354 self.filename_fmt = filename_fmt |
364 path = os.path.join(self.path, filename) |
379 path = os.path.join(self.path, filename) |
365 |
380 |
366 try : |
381 try : |
367 if load : |
382 if load : |
368 # open+return the LogFile |
383 # open+return the LogFile |
369 return LogFile(path, self.parser, self.decoder, d) |
384 return LogFile(path, self.parser, self.decoder, start_date=d, channel=self.channel) |
370 |
385 |
371 else : |
386 else : |
372 # test |
387 # test |
373 return os.path.exists(path) |
388 return os.path.exists(path) |
374 |
389 |