log_formatter.py
changeset 97 6165f1ba458d
parent 92 74f6a0b01ddf
child 108 d0aca7894fc5
--- a/log_formatter.py	Wed Feb 11 03:04:35 2009 +0200
+++ b/log_formatter.py	Wed Feb 11 03:05:11 2009 +0200
@@ -29,17 +29,23 @@
 
             Use the given TTF font to render image text with the given size, if given, otherwise, a default one.
         """
-
+        
+        # store
         self.tz = tz
         self.timestamp_fmt = timestamp_fmt
         self.img_ttf_path = img_ttf_path
         self.img_font_size = img_font_size
+        
+        # XXX: harcoded
+        self.date_fmt = '%Y-%m-%d'
     
-    def _format_line_text (self, line, template_dict, type=None, full_timestamp=False) :
+    def _format_line_text (self, line, template_dict, type=None, full_timestamp=False, **extra) :
         """
             Format the given line as text, using the given { type: string template } dict.
             
             If type is given, then it overrides line.type
+
+            Any additional keyword args will also be available for the template to use
         """
 
         # default type?
@@ -47,15 +53,19 @@
             type = line.type
             
         # look up the template
-        template = template_dict[type]
+        if type in template_dict :
+            template = template_dict[type]
+
+        else :
+            raise Exception("Format template not defined for type: %s" % LogTypes.name_from_code(type))
         
         # convert timestamp into display timezone
         dtz = line.timestamp.astimezone(self.tz)
         
         # full timestamps?
         if full_timestamp :
-            # XXX: ugly hack
-            timestamp_fmt = '%Y-%m-%d ' + self.timestamp_fmt
+            # XXX: let the user define a 'datetime' format instead?
+            timestamp_fmt = self.date_fmt + self.timestamp_fmt
 
         else :
             timestamp_fmt = self.timestamp_fmt
@@ -68,6 +78,7 @@
         return template % dict(
             channel_name    = line.channel.name,
             datetime        = dtz.strftime('%a %b %d %H:%M:%S %Y'),
+            date            = dtz.strftime(self.date_fmt),
             timestamp       = dtz.strftime(timestamp_fmt),
             source_nickname = source_nickname,
             source_username = source_username,
@@ -75,6 +86,7 @@
             source_chanflag = source_chanflag,
             target_nickname = target_nickname,
             message         = line.data,
+            **extra
         )
     
     def format_txt (self, lines, full_timestamps=False) :
@@ -165,6 +177,7 @@
         LogTypes.RAW        : "%(timestamp)s %(data)s",
         LogTypes.LOG_OPEN   : "--- Log opened %(datetime)s",
         LogTypes.LOG_CLOSE  : "--- Log closed %(datetime)s",
+        'DAY_CHANGED'       : "--- Day changed %(date)s",
 
         LogTypes.MSG        : "%(timestamp)s <%(source_chanflag)s%(source_nickname)s> %(message)s",
         LogTypes.NOTICE     : "%(timestamp)s -%(source_nickname)s- %(message)s",
@@ -183,20 +196,33 @@
 
         LogTypes.SELF_NOTICE: "%(timestamp)s -%(source_nickname)s- %(message)s",
         LogTypes.SELF_NICK  : "%(timestamp)s -!- %(source_nickname)s is now known as %(target_nickname)s",
+
+        LogTypes.NETSPLIT_START : 
+                              "%(timestamp)s -!- Netsplit %(source_hostname)s <-> %(target_nickname)s quits: %(_netsplit_targets)s",
+        LogTypes.NETSPLIT_END :
+                              "%(timestamp)s -!- Netsplit over, joins: %(_netsplit_targets)s",
     }
 
     def format_txt (self, lines, full_timestamps=False) :
         # ...handle each line
         for line in lines :
-            # specialcases
+            # extra args
+            extra = {}
+
+            # specialcase type?
             if line.type == LogTypes.TOPIC and line.data is None :
                 type = 'TOPIC_UNSET'
             
             else :
                 type = line.type
 
+            # format netsplit stuff
+            if line.type & LogTypes._NETSPLIT_MASK :
+                # format the netsplit-targets stuff
+                extra['_netsplit_targets'] = line.data
+
             # using __TYPES
-            yield line, self._format_line_text(line, self.__FMT, type, full_timestamps)
+            yield line, self._format_line_text(line, self.__FMT, type, full_timestamps, **extra)
 
 class IrssiFormatter (BaseHTMLFormatter, IrssiTextFormatter) :
     """
@@ -220,7 +246,7 @@
         # iterate
         for line in lines :
             # just dump
-            yield line, str(line)
+            yield line, unicode(line)
 
 def by_name (name) :
     """