log_formatter.py
changeset 79 43ac75054d5c
parent 73 5a7188bf2894
child 80 a0662cff1d9d
equal deleted inserted replaced
78:85345abbd46a 79:43ac75054d5c
     3 """
     3 """
     4 
     4 
     5 import re, xml.sax.saxutils
     5 import re, xml.sax.saxutils
     6 
     6 
     7 from log_line import LogTypes
     7 from log_line import LogTypes
       
     8 from log_formatter_pil import PILImageFormatter
     8 
     9 
     9 class LogFormatter (object) :
    10 class LogFormatter (object) :
    10     """
    11     """
    11         Provides a method to format series of LogLines into various output formats, with varying themes.
    12         Provides a method to format series of LogLines into various output formats, with varying themes.
    12     """
    13     """
    19 
    20 
    20     ## parameters
    21     ## parameters
    21     # use a fixed-width font for HTML output
    22     # use a fixed-width font for HTML output
    22     html_fixedwidth = True
    23     html_fixedwidth = True
    23 
    24 
    24     def __init__ (self, tz, timestamp_fmt="%H:%M:%S") :
    25     def __init__ (self, tz, timestamp_fmt, img_ttf_path, img_font_size) :
    25         """
    26         """
    26             Initialize to format timestamps with the given timezone and timestamp
    27             Initialize to format timestamps with the given timezone and timestamp.
       
    28 
       
    29             Use the given TTF font to render image text with the given size, if given, otherwise, a default one.
    27         """
    30         """
    28 
    31 
    29         self.tz = tz
    32         self.tz = tz
    30         self.timestamp_fmt = timestamp_fmt
    33         self.timestamp_fmt = timestamp_fmt
       
    34         self.img_ttf_path = img_ttf_path
       
    35         self.img_font_size = img_font_size
    31     
    36     
    32     def _format_line_text (self, line, template_dict, full_timestamp=False) :
    37     def _format_line_text (self, line, template_dict, full_timestamp=False) :
    33         """
    38         """
    34             Format the given line as text, using the given { type: string template } dict
    39             Format the given line as text, using the given { type: string template } dict
    35         """
    40         """
    75             
    80             
    76             See format_txt for information about arguments
    81             See format_txt for information about arguments
    77         """
    82         """
    78 
    83 
    79         abstract
    84         abstract
       
    85     
       
    86     def format_png (self, lines) :
       
    87         """
       
    88             Format as a PNG image, returning the binary PNG data
       
    89         """
    80 
    90 
    81 class BaseHTMLFormatter (object) :
    91 class BaseHTMLFormatter (object) :
    82     """
    92     """
    83         Implements some HTML-formatting utils
    93         Implements some HTML-formatting utils
    84     """
    94     """
    99 
   109 
   100             return '<a href="%(url_link)s">%(url_html)s</a>' % dict(url_link=url_link, url_html=url_html)
   110             return '<a href="%(url_link)s">%(url_html)s</a>' % dict(url_link=url_link, url_html=url_html)
   101 
   111 
   102         return self.URL_REGEXP.sub(_encode_url, line)
   112         return self.URL_REGEXP.sub(_encode_url, line)
   103 
   113 
   104 class IrssiTextFormatter (LogFormatter) :
   114 class IrssiTextFormatter (PILImageFormatter, LogFormatter) :
   105     """
   115     """
   106         Implements format_txt for irssi-style output
   116         Implements format_txt for irssi-style output
   107     """
   117     """
   108 
   118 
   109     # format definitions by type
   119     # format definitions by type
   127     title = "Irssi (plain)"
   137     title = "Irssi (plain)"
   128 
   138 
   129     # parameters
   139     # parameters
   130     html_fixedwidth = True
   140     html_fixedwidth = True
   131 
   141 
   132     def format_html (self, lines, full_timestamps=False) :
   142     def format_html (self, lines, **kwargs) :
   133         """
   143         """
   134             Just uses format_txt, but processes links, etc
   144             Just uses format_txt, but processes links, etc
   135         """
   145         """
   136         
   146         
   137         # format using IrssiTextFormatter
   147         # format using IrssiTextFormatter
   138         for line, txt in self.format_txt(lines, full_timestamps) :
   148         for line, txt in self.format_txt(lines, **kwargs) :
   139             # escape HTML
   149             # escape HTML
   140             html = xml.sax.saxutils.escape(txt)
   150             html = xml.sax.saxutils.escape(txt)
   141 
   151 
   142             # process links
   152             # process links
   143             html = self._process_links(html)
   153             html = self._process_links(html)
   144 
   154 
   145             # yield
   155             # yield
   146             yield line, html
   156             yield line, html
   147 
   157 
   148 # define formatters by name
       
   149 FORMATTERS = {
       
   150     'irssi':        IrssiFormatter,
       
   151 }
       
   152 
       
   153 def by_name (name) :
   158 def by_name (name) :
   154     """
   159     """
   155         Lookup and return a class LogFormatter by name
   160         Lookup and return a class LogFormatter by name
   156     """
   161     """
   157 
   162