change debug formatter to to use str(LogLine) for TSV, and fix handling of topic-unset
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 01:21:22 +0200
changeset 92 74f6a0b01ddf
parent 91 df2a6780cdf9
child 93 48fca00689e3
change debug formatter to to use str(LogLine) for TSV, and fix handling of topic-unset
log_formatter.py
log_line.py
log_parser.py
templates/preferences.tmpl
--- a/log_formatter.py	Wed Feb 11 01:00:18 2009 +0200
+++ b/log_formatter.py	Wed Feb 11 01:21:22 2009 +0200
@@ -35,13 +35,19 @@
         self.img_ttf_path = img_ttf_path
         self.img_font_size = img_font_size
     
-    def _format_line_text (self, line, template_dict, full_timestamp=False) :
+    def _format_line_text (self, line, template_dict, type=None, full_timestamp=False) :
         """
-            Format the given line as text, using the given { type: string template } dict
+            Format the given line as text, using the given { type: string template } dict.
+            
+            If type is given, then it overrides line.type
         """
+
+        # default type?
+        if type is None :
+            type = line.type
             
         # look up the template
-        template = template_dict[line.type]
+        template = template_dict[type]
         
         # convert timestamp into display timezone
         dtz = line.timestamp.astimezone(self.tz)
@@ -173,6 +179,7 @@
         LogTypes.QUIT       : "%(timestamp)s -!- %(source_nickname)s [%(source_username)s@%(source_hostname)s] has quit [%(message)s]",
 
         LogTypes.TOPIC      : "%(timestamp)s -!- %(source_nickname)s changed the topic of %(channel_name)s to: %(message)s",
+        'TOPIC_UNSET'       : "%(timestamp)s -!- Topic unset by %(source_nickname)s on %(channel_name)s",
 
         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",
@@ -181,8 +188,15 @@
     def format_txt (self, lines, full_timestamps=False) :
         # ...handle each line
         for line in lines :
+            # specialcases
+            if line.type == LogTypes.TOPIC and line.data is None :
+                type = 'TOPIC_UNSET'
+            
+            else :
+                type = line.type
+
             # using __TYPES
-            yield line, self._format_line_text(line, self.__FMT, full_timestamps)
+            yield line, self._format_line_text(line, self.__FMT, type, full_timestamps)
 
 class IrssiFormatter (BaseHTMLFormatter, IrssiTextFormatter) :
     """
@@ -206,7 +220,7 @@
         # iterate
         for line in lines :
             # just dump
-            yield line, repr(line)
+            yield line, str(line)
 
 def by_name (name) :
     """
--- a/log_line.py	Wed Feb 11 01:00:18 2009 +0200
+++ b/log_line.py	Wed Feb 11 01:21:22 2009 +0200
@@ -41,41 +41,45 @@
         ('LOG_CLOSE',   0x03),
 
         ## messages
-        # normal message
+        # <source> sent message <data> to <channel>
         ('MSG',         0x10),
         
-        # notice
+        # <source> sent notice with message <data> to <channel>
         ('NOTICE',      0x11),
 
-        # CTCP action
+        # <source> sent CTCP action with message <data> to <channel>
         ('ACTION',      0x12),
         
         ## user-channel stats
-        # join
+        # <source> joined <channel>
         ('JOIN',        0x21),
 
-        # part
+        # <source> left <channel> with message <data>
         ('PART',        0x22),
 
-        # kick
+        # <source> kicked <target> from <channel> with message <data>
         ('KICK',        0x25),
      
-        # channel modes
+        # <source> changed modes on <channel> with modestring <data>
         ('MODE',        0x26),
         
         ## user status
-        # nick-change
+        # <source> changed nickname to <target>
         ('NICK',        0x31),
 
-        # quit
+        # <source> quit the network with quit-message <data>
         ('QUIT',        0x32),
 
         ## general channel status
-        # topic changed
+        # <source> changed the topic of <channel> to <data>
+        # data may be None if the topic was unset
         ('TOPIC',       0x41),
 
         ## our own actions
+        # we (<source>) sent a notice with message <data> to <channel>
         ('SELF_NOTICE', 0x51),
+
+        # we (<source>) changed nickname to <target>
         ('SELF_NICK',   0x52),
     ]
     
@@ -108,10 +112,10 @@
     # the UTC timestamp of the event
     timestamp = None
 
-    # the event source, this should be a 
+    # the source, this should be a (nickname, username, hostname, chanflags) tuple
     source = None
 
-    # possible event target, for certain types (kick, nick)
+    # possible target nickname for certain types (kick, nick)
     target = None
 
     # associated data (message, etc)
@@ -139,7 +143,9 @@
 
     def format_source (self) :
         """
-            Formats source as [<chanflags>][<nickname>][!<username>][@<hostname>], omitting those parts that are missing
+            Formats source as [<chanflags>][<nickname>][!<username>][@<hostname>], omitting those parts that are missing.
+
+            If all parts are None, this returns the empty string
         """
 
         nick, user, host, flags = self.source
@@ -150,6 +156,17 @@
             '!' + user if user else '',
             '@' + host if host else ''
         )
+   
+    def __str__ (self) :
+        return '\t'.join((
+            self.channel.name,
+            str(self.offset),
+            self.format_type(),
+            str(self.timestamp),
+            self.format_source(),
+            str(self.target),
+            str(self.data)
+        ))
 
     def __repr__ (self) :
         return "LogLine(%r, %s, %-12s, %s, %-35s, %-10s, %r)" % (
--- a/log_parser.py	Wed Feb 11 01:00:18 2009 +0200
+++ b/log_parser.py	Wed Feb 11 01:21:22 2009 +0200
@@ -51,7 +51,9 @@
     _TS = r'(?P<timestamp>\S+)'
     _NICK = r'(?P<nickname>.+?)'
     _NICK2 = r'(?P<nickname2>.+?)'
+    _TARGET = r'(?P<target>.+?)'
     _CHAN = r'(?P<channel>.+?)'
+    _CHAN2 = r'(?P<channel2>.+?)'
     _USERHOST = r'(?P<username>.*?)@(?P<hostname>.*?)'
     _MSG = r'(?P<message>.*)'
 
@@ -64,14 +66,14 @@
         (   LogTypes.ACTION,        _TS + r'  \* ' + _NICK + ' ' + _MSG                             ),
         (   LogTypes.JOIN,          _TS + r' -!- ' + _NICK + ' \[' + _USERHOST + '\] has joined ' + _CHAN                               ), 
         (   LogTypes.PART,          _TS + r' -!- ' + _NICK + ' \[' + _USERHOST + '\] has left ' + _CHAN + ' \[(?P<message>.*?)\]'       ),
-        (   LogTypes.KICK,          _TS + r' -!- ' + _NICK2 + ' was kicked from ' + _CHAN + ' by ' + _NICK + ' \[(?P<message>.*?)\]'    ),
+        (   LogTypes.KICK,          _TS + r' -!- ' + _TARGET + ' was kicked from ' + _CHAN + ' by ' + _NICK + ' \[(?P<message>.*?)\]'   ),
         (   LogTypes.MODE,          _TS + r' -!- mode/' + _CHAN + ' \[(?P<mode>.+?)\] by (?P<nickname>\S+)'                             ),
-        (   LogTypes.NICK,          _TS + r' -!- ' + _NICK + ' is now known as (?P<nickname2>\S+)'                                      ),
+        (   LogTypes.NICK,          _TS + r' -!- ' + _NICK + ' is now known as (?P<target>\S+)'                                         ),
         (   LogTypes.QUIT,          _TS + r' -!- ' + _NICK + ' \[' + _USERHOST + '\] has quit \[(?P<message>.*?)\]'                     ),
-        (   LogTypes.TOPIC,         _TS + r' -!- ' + _NICK + ' changed the topic of ' + _CHAN + ' to: (?P<topic>.*)'                    ),
+        (   LogTypes.TOPIC,         _TS + r' -!- (' + _NICK + ' changed the topic of ' + _CHAN + ' to: (?P<topic>.*)|Topic unset by ' + _NICK2 + ' on ' + _CHAN2 + ')'    ),
 
         (   LogTypes.SELF_NOTICE,   _TS + r' \[notice\(' + _CHAN + '\)\] ' + _MSG                   ),
-        (   LogTypes.SELF_NICK,     _TS + r' -!- You\'re now known as (?P<nickname2>\S+)'           ),
+        (   LogTypes.SELF_NICK,     _TS + r' -!- You\'re now known as (?P<target>\S+)'           ),
     )
 
     # precompile
@@ -127,11 +129,14 @@
         # now localize with timezone
         dtz = self.tz.localize(dt)
 
+        # channel, currently unused
+        channel_name = (groups.get('channel') or groups.get('channel2'))
+
         # source
-        source = (groups.get('nickname'), groups.get('username'), groups.get('hostname'), groups.get('flags'))
+        source = (groups.get('nickname') or groups.get('nickname2'), groups.get('username'), groups.get('hostname'), groups.get('flags'))
 
         # target
-        target = groups.get('nickname2')
+        target = groups.get('target')
 
         # data
         if 'message' in groups :
--- a/templates/preferences.tmpl	Wed Feb 11 01:00:18 2009 +0200
+++ b/templates/preferences.tmpl	Wed Feb 11 01:21:22 2009 +0200
@@ -34,7 +34,7 @@
         <p>
             <label for="formatter">Formatter:</label>
             <select name="formatter">
-                ${h.select_options(((fmt_name, fmt.title) for fmt_name, fmt in preferences.formatter.formatters.iteritems()), prefs['formatter'])}
+                ${h.select_options(((fmt_name, fmt.title) for fmt_name, fmt in preferences.formatter.formatters.iteritems()), prefs['formatter'].name)}
             </select>
         </p>