log_formatter.py
changeset 97 6165f1ba458d
parent 92 74f6a0b01ddf
child 108 d0aca7894fc5
equal deleted inserted replaced
96:d30c88e89a7e 97:6165f1ba458d
    27         """
    27         """
    28             Initialize to format timestamps with the given timezone and timestamp.
    28             Initialize to format timestamps with the given timezone and timestamp.
    29 
    29 
    30             Use the given TTF font to render image text with the given size, if given, otherwise, a default one.
    30             Use the given TTF font to render image text with the given size, if given, otherwise, a default one.
    31         """
    31         """
    32 
    32         
       
    33         # store
    33         self.tz = tz
    34         self.tz = tz
    34         self.timestamp_fmt = timestamp_fmt
    35         self.timestamp_fmt = timestamp_fmt
    35         self.img_ttf_path = img_ttf_path
    36         self.img_ttf_path = img_ttf_path
    36         self.img_font_size = img_font_size
    37         self.img_font_size = img_font_size
    37     
    38         
    38     def _format_line_text (self, line, template_dict, type=None, full_timestamp=False) :
    39         # XXX: harcoded
       
    40         self.date_fmt = '%Y-%m-%d'
       
    41     
       
    42     def _format_line_text (self, line, template_dict, type=None, full_timestamp=False, **extra) :
    39         """
    43         """
    40             Format the given line as text, using the given { type: string template } dict.
    44             Format the given line as text, using the given { type: string template } dict.
    41             
    45             
    42             If type is given, then it overrides line.type
    46             If type is given, then it overrides line.type
       
    47 
       
    48             Any additional keyword args will also be available for the template to use
    43         """
    49         """
    44 
    50 
    45         # default type?
    51         # default type?
    46         if type is None :
    52         if type is None :
    47             type = line.type
    53             type = line.type
    48             
    54             
    49         # look up the template
    55         # look up the template
    50         template = template_dict[type]
    56         if type in template_dict :
       
    57             template = template_dict[type]
       
    58 
       
    59         else :
       
    60             raise Exception("Format template not defined for type: %s" % LogTypes.name_from_code(type))
    51         
    61         
    52         # convert timestamp into display timezone
    62         # convert timestamp into display timezone
    53         dtz = line.timestamp.astimezone(self.tz)
    63         dtz = line.timestamp.astimezone(self.tz)
    54         
    64         
    55         # full timestamps?
    65         # full timestamps?
    56         if full_timestamp :
    66         if full_timestamp :
    57             # XXX: ugly hack
    67             # XXX: let the user define a 'datetime' format instead?
    58             timestamp_fmt = '%Y-%m-%d ' + self.timestamp_fmt
    68             timestamp_fmt = self.date_fmt + self.timestamp_fmt
    59 
    69 
    60         else :
    70         else :
    61             timestamp_fmt = self.timestamp_fmt
    71             timestamp_fmt = self.timestamp_fmt
    62         
    72         
    63         # breakdown source
    73         # breakdown source
    66         
    76         
    67         # format with dict
    77         # format with dict
    68         return template % dict(
    78         return template % dict(
    69             channel_name    = line.channel.name,
    79             channel_name    = line.channel.name,
    70             datetime        = dtz.strftime('%a %b %d %H:%M:%S %Y'),
    80             datetime        = dtz.strftime('%a %b %d %H:%M:%S %Y'),
       
    81             date            = dtz.strftime(self.date_fmt),
    71             timestamp       = dtz.strftime(timestamp_fmt),
    82             timestamp       = dtz.strftime(timestamp_fmt),
    72             source_nickname = source_nickname,
    83             source_nickname = source_nickname,
    73             source_username = source_username,
    84             source_username = source_username,
    74             source_hostname = source_hostname,
    85             source_hostname = source_hostname,
    75             source_chanflag = source_chanflag,
    86             source_chanflag = source_chanflag,
    76             target_nickname = target_nickname,
    87             target_nickname = target_nickname,
    77             message         = line.data,
    88             message         = line.data,
       
    89             **extra
    78         )
    90         )
    79     
    91     
    80     def format_txt (self, lines, full_timestamps=False) :
    92     def format_txt (self, lines, full_timestamps=False) :
    81         """
    93         """
    82             Format given lines as plaintext.
    94             Format given lines as plaintext.
   163     # format definitions by type
   175     # format definitions by type
   164     __FMT = {
   176     __FMT = {
   165         LogTypes.RAW        : "%(timestamp)s %(data)s",
   177         LogTypes.RAW        : "%(timestamp)s %(data)s",
   166         LogTypes.LOG_OPEN   : "--- Log opened %(datetime)s",
   178         LogTypes.LOG_OPEN   : "--- Log opened %(datetime)s",
   167         LogTypes.LOG_CLOSE  : "--- Log closed %(datetime)s",
   179         LogTypes.LOG_CLOSE  : "--- Log closed %(datetime)s",
       
   180         'DAY_CHANGED'       : "--- Day changed %(date)s",
   168 
   181 
   169         LogTypes.MSG        : "%(timestamp)s <%(source_chanflag)s%(source_nickname)s> %(message)s",
   182         LogTypes.MSG        : "%(timestamp)s <%(source_chanflag)s%(source_nickname)s> %(message)s",
   170         LogTypes.NOTICE     : "%(timestamp)s -%(source_nickname)s- %(message)s",
   183         LogTypes.NOTICE     : "%(timestamp)s -%(source_nickname)s- %(message)s",
   171         LogTypes.ACTION     : "%(timestamp)s  * %(source_nickname)s %(message)s",
   184         LogTypes.ACTION     : "%(timestamp)s  * %(source_nickname)s %(message)s",
   172 
   185 
   181         LogTypes.TOPIC      : "%(timestamp)s -!- %(source_nickname)s changed the topic of %(channel_name)s to: %(message)s",
   194         LogTypes.TOPIC      : "%(timestamp)s -!- %(source_nickname)s changed the topic of %(channel_name)s to: %(message)s",
   182         'TOPIC_UNSET'       : "%(timestamp)s -!- Topic unset by %(source_nickname)s on %(channel_name)s",
   195         'TOPIC_UNSET'       : "%(timestamp)s -!- Topic unset by %(source_nickname)s on %(channel_name)s",
   183 
   196 
   184         LogTypes.SELF_NOTICE: "%(timestamp)s -%(source_nickname)s- %(message)s",
   197         LogTypes.SELF_NOTICE: "%(timestamp)s -%(source_nickname)s- %(message)s",
   185         LogTypes.SELF_NICK  : "%(timestamp)s -!- %(source_nickname)s is now known as %(target_nickname)s",
   198         LogTypes.SELF_NICK  : "%(timestamp)s -!- %(source_nickname)s is now known as %(target_nickname)s",
       
   199 
       
   200         LogTypes.NETSPLIT_START : 
       
   201                               "%(timestamp)s -!- Netsplit %(source_hostname)s <-> %(target_nickname)s quits: %(_netsplit_targets)s",
       
   202         LogTypes.NETSPLIT_END :
       
   203                               "%(timestamp)s -!- Netsplit over, joins: %(_netsplit_targets)s",
   186     }
   204     }
   187 
   205 
   188     def format_txt (self, lines, full_timestamps=False) :
   206     def format_txt (self, lines, full_timestamps=False) :
   189         # ...handle each line
   207         # ...handle each line
   190         for line in lines :
   208         for line in lines :
   191             # specialcases
   209             # extra args
       
   210             extra = {}
       
   211 
       
   212             # specialcase type?
   192             if line.type == LogTypes.TOPIC and line.data is None :
   213             if line.type == LogTypes.TOPIC and line.data is None :
   193                 type = 'TOPIC_UNSET'
   214                 type = 'TOPIC_UNSET'
   194             
   215             
   195             else :
   216             else :
   196                 type = line.type
   217                 type = line.type
   197 
   218 
       
   219             # format netsplit stuff
       
   220             if line.type & LogTypes._NETSPLIT_MASK :
       
   221                 # format the netsplit-targets stuff
       
   222                 extra['_netsplit_targets'] = line.data
       
   223 
   198             # using __TYPES
   224             # using __TYPES
   199             yield line, self._format_line_text(line, self.__FMT, type, full_timestamps)
   225             yield line, self._format_line_text(line, self.__FMT, type, full_timestamps, **extra)
   200 
   226 
   201 class IrssiFormatter (BaseHTMLFormatter, IrssiTextFormatter) :
   227 class IrssiFormatter (BaseHTMLFormatter, IrssiTextFormatter) :
   202     """
   228     """
   203         Implements plain black-and-white irssi-style formatting
   229         Implements plain black-and-white irssi-style formatting
   204     """
   230     """
   218     
   244     
   219     def format_txt (self, lines, full_timestamps=False) :
   245     def format_txt (self, lines, full_timestamps=False) :
   220         # iterate
   246         # iterate
   221         for line in lines :
   247         for line in lines :
   222             # just dump
   248             # just dump
   223             yield line, str(line)
   249             yield line, unicode(line)
   224 
   250 
   225 def by_name (name) :
   251 def by_name (name) :
   226     """
   252     """
   227         Lookup and return a class LogFormatter by name
   253         Lookup and return a class LogFormatter by name
   228     """
   254     """