log_formatter.py
changeset 50 f13cf27a360b
child 51 07ca28f3a9f2
equal deleted inserted replaced
49:aaa62c8e5bd5 50:f13cf27a360b
       
     1 """
       
     2     Format LogLines into some other representation
       
     3 """
       
     4 
       
     5 from qmsk.web import helpers
       
     6 
       
     7 from log_line import LogTypes
       
     8 
       
     9 class LogFormatter (object) :
       
    10     """
       
    11         Provides a method to format series of LogLines into various output formats, with varying themes
       
    12     """
       
    13     
       
    14     def __init__ (self, tz, timestamp_fmt="%H:%M:%S") :
       
    15         """
       
    16             Initialize to format timestamps with the given timezone and timestamp
       
    17         """
       
    18 
       
    19         self.tz = tz
       
    20         self.timestamp_fmt = timestamp_fmt
       
    21     
       
    22     def _format_line_text (self, line, template_dict) :
       
    23         """
       
    24             Format the given line as text, using the given { type: string template } dict
       
    25         """
       
    26         
       
    27         # look up the template
       
    28         template = template_dict[line.type]
       
    29 
       
    30         # build timestamp
       
    31         timestamp = line.timestamp.astimezone(self.tz).strftime(self.timestamp_fmt)
       
    32         
       
    33         # format with dict
       
    34         return template % dict(
       
    35             timestamp       = timestamp,
       
    36             source          = line.source,
       
    37             data            = line.data,
       
    38         )
       
    39     
       
    40     def format_txt (self, lines) :
       
    41         """
       
    42             Format as plaintext
       
    43         """
       
    44 
       
    45         abstract
       
    46 
       
    47     def format_html (self, lines) :
       
    48         """
       
    49             Format as HTML
       
    50         """
       
    51 
       
    52         abstract
       
    53 
       
    54 class IrssiTextFormatter (LogFormatter) :
       
    55     """
       
    56         Implements format_txt for irssi-style output
       
    57     """
       
    58 
       
    59     # format definitions by type
       
    60     __FMT = {
       
    61         LogTypes.RAW        : "%(timestamp)s %(data)s",
       
    62     }
       
    63 
       
    64     def format_txt (self, lines) :
       
    65         # ...handle each line
       
    66         for line in lines :
       
    67             # using __TYPES
       
    68             yield self._format_line_text(line, self.__FMT)
       
    69 
       
    70 class IrssiFormatter (IrssiTextFormatter) :
       
    71     """
       
    72         Implements plain black-and-white irssi-style formatting
       
    73     """
       
    74 
       
    75     def format_html (self, lines) :
       
    76         """
       
    77             Just uses format_txt, but wraps in <pre></pre>
       
    78         """
       
    79         
       
    80         # open pre
       
    81         yield "<pre>"
       
    82         
       
    83         # format using IrssiTextFormatter
       
    84         for line in self.format_txt(lines) :
       
    85             # escape HTML
       
    86             yield helpers.escape(line)
       
    87 
       
    88         # close pre
       
    89         yield "</pre>"
       
    90 
       
    91 # define formatters by name
       
    92 FORMATTERS = {
       
    93     'irssi':        IrssiFormatter,
       
    94 }
       
    95 
       
    96 def by_name (name) :
       
    97     """
       
    98         Lookup and return a formatter by name
       
    99         
       
   100         XXX: uses default timezone/timefmt
       
   101     """
       
   102 
       
   103     import pytz
       
   104 
       
   105     return FORMATTERS[name](pytz.utc)
       
   106