log_formatter.py
changeset 69 1f182913b1f2
parent 65 8b50694f841e
child 72 5ade0288f2ec
equal deleted inserted replaced
68:8157c41b3236 69:1f182913b1f2
     1 """
     1 """
     2     Format LogLines into some other representation
     2     Format LogLines into some other representation
     3 """
     3 """
     4 
     4 
     5 # for escape
     5 import re, xml.sax.saxutils
     6 import cgi
       
     7 
     6 
     8 from log_line import LogTypes
     7 from log_line import LogTypes
     9 
     8 
    10 class LogFormatter (object) :
     9 class LogFormatter (object) :
    11     """
    10     """
    70             See format_txt for information about arguments
    69             See format_txt for information about arguments
    71         """
    70         """
    72 
    71 
    73         abstract
    72         abstract
    74 
    73 
       
    74 class BaseHTMLFormatter (object) :
       
    75     """
       
    76         Implements some HTML-formatting utils
       
    77     """
       
    78 
       
    79     URL_REGEXP = re.compile(r"http://\S+")
       
    80 
       
    81     def _process_links (self, line) :
       
    82         """
       
    83             Processed the rendered line, adding in <a href>'s for things that look like URLs, returning the new line.
       
    84 
       
    85             The line should already be escaped
       
    86         """
       
    87 
       
    88         def _encode_url (match) :
       
    89             # encode URL
       
    90             url_html = match.group(0)
       
    91             url_link = xml.sax.saxutils.unescape(url_html)
       
    92 
       
    93             return '<a href="%(url_link)s">%(url_html)s</a>' % dict(url_link=url_link, url_html=url_html)
       
    94 
       
    95         return self.URL_REGEXP.sub(_encode_url, line)
       
    96 
    75 class IrssiTextFormatter (LogFormatter) :
    97 class IrssiTextFormatter (LogFormatter) :
    76     """
    98     """
    77         Implements format_txt for irssi-style output
    99         Implements format_txt for irssi-style output
    78     """
   100     """
    79 
   101 
    86         # ...handle each line
   108         # ...handle each line
    87         for line in lines :
   109         for line in lines :
    88             # using __TYPES
   110             # using __TYPES
    89             yield self._format_line_text(line, self.__FMT, full_timestamps)
   111             yield self._format_line_text(line, self.__FMT, full_timestamps)
    90 
   112 
    91 class IrssiFormatter (IrssiTextFormatter) :
   113 class IrssiFormatter (IrssiTextFormatter, BaseHTMLFormatter) :
    92     """
   114     """
    93         Implements plain black-and-white irssi-style formatting
   115         Implements plain black-and-white irssi-style formatting
    94     """
   116     """
    95 
   117 
    96     name = 'irssi'
   118     name = 'irssi'
   105         yield "<pre>"
   127         yield "<pre>"
   106         
   128         
   107         # format using IrssiTextFormatter
   129         # format using IrssiTextFormatter
   108         for line in self.format_txt(lines, full_timestamps) :
   130         for line in self.format_txt(lines, full_timestamps) :
   109             # escape HTML
   131             # escape HTML
   110             yield cgi.escape(line)
   132             line = xml.sax.saxutils.escape(line)
       
   133 
       
   134             # process links
       
   135             line = self._process_links(line)
       
   136 
       
   137             # yield
       
   138             yield line
   111 
   139 
   112         # close pre
   140         # close pre
   113         yield "</pre>"
   141         yield "</pre>"
   114 
   142 
   115 # define formatters by name
   143 # define formatters by name