log_source.py
changeset 76 cc3ab2c39ded
parent 73 5a7188bf2894
child 77 4287fb77e312
equal deleted inserted replaced
75:c5ce145fdd70 76:cc3ab2c39ded
     1 """
     1 """
     2     A source of IRC log files
     2     A source of IRC log files
     3 """
     3 """
     4 
     4 
     5 import datetime, calendar, itertools
     5 import datetime, calendar, itertools, functools
     6 import os, errno
     6 import os, errno
     7 import pytz
     7 import pytz
     8 
     8 
     9 class LogSource (object) :
     9 class LogSource (object) :
    10     """
    10     """
    18 
    18 
    19         abstract
    19         abstract
    20     
    20     
    21     def get_date (self, dt) :
    21     def get_date (self, dt) :
    22         """
    22         """
    23             Get logs for the given date (as a datetime)
    23             Get logs for the given date (as a datetime).
    24         """
    24         """
    25 
    25 
    26         abstract
    26         abstract
    27     
    27     
       
    28     def get_date_paged (self, dt, count, page=None) :
       
    29         """
       
    30             Get the logs for a given date (as a datetime), divided into pages of count each. If page is given, the time
       
    31             portion of the dt is ignored, and the lines for the given page are returned. Otherwise, if page is None,
       
    32             then the lines for the page containing the given timestamp is returned.
       
    33 
       
    34             The return value is a (page, max, lines) tuple.
       
    35         """
       
    36         
       
    37         # how to act?
       
    38         if page :
       
    39             # constant skip
       
    40             skip = (page - 1) * count
       
    41 
       
    42         else :
       
    43             skip = None
       
    44 
       
    45         # collect lines
       
    46         lines = []
       
    47 
       
    48         # iterate using get_date
       
    49         for line in self.get_date(dt) :
       
    50             # skip?
       
    51             if skip :
       
    52                 skip -= 1
       
    53                 continue
       
    54             
       
    55             # store line
       
    56             lines.append(line)
       
    57 
       
    58             # count?
       
    59             if len(lines) >= count :
       
    60                 break
       
    61 
       
    62         return (page, 0, lines)
       
    63 
    28     def get_month_days (self, dt) :
    64     def get_month_days (self, dt) :
    29         """
    65         """
    30             Get a set of dates, telling which days in the given month (as a datetime) have logs available
    66             Get a set of dates, telling which days in the given month (as a datetime) have logs available
    31         """
    67         """
    32 
    68 
    33         abstract
    69         abstract
    34     
    70  
    35 class LogFile (object) :
    71 class LogFile (object) :
    36     """
    72     """
    37         A file containing LogEvents
    73         A file containing LogEvents
    38 
    74 
    39         XXX: modify to implement LogSource?
    75         XXX: modify to implement LogSource?
    67         # iterate over lines, decoding them as well
   103         # iterate over lines, decoding them as well
    68         return (line.decode(self.charset).rstrip(self.sep) for line in self.file)
   104         return (line.decode(self.charset).rstrip(self.sep) for line in self.file)
    69     
   105     
    70     def read_full (self) :
   106     def read_full (self) :
    71         """
   107         """
    72             Reads all LogLines. The LogLines will have a valid offset
   108             Reads all LogLines. The LogLines will have a valid offset.
    73         """
   109         """
    74         
   110         
    75         # just use our __iter__
   111         # just use our __iter__
    76         return self.parser.parse_lines(self, self.start_date, starting_offset=1)
   112         return self.parser.parse_lines(self, self.start_date, starting_offset=1)
    77 
   113 
   378         # otherwise, we need to pull two partial logs
   414         # otherwise, we need to pull two partial logs
   379         else :
   415         else :
   380             # open both of them
   416             # open both of them
   381             f_begin = self._get_logfile_date(d_begin)
   417             f_begin = self._get_logfile_date(d_begin)
   382             f_end = self._get_logfile_date(d_end)
   418             f_end = self._get_logfile_date(d_end)
   383 
   419             
   384             # chain together the two sources
   420             # chain together the two sources
   385             return itertools.chain(
   421             return itertools.chain(
   386                 f_begin.read_from(dtz_begin), 
   422                 f_begin.read_from(dtz_begin), 
   387                 f_end.read_until(dtz_end) if f_end else []
   423                 f_end.read_until(dtz_end) if f_end else []
   388             )
   424             )