use improved URLConfig/URLType
authorTero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 01:11:05 +0200
changeset 51 07ca28f3a9f2
parent 50 f13cf27a360b
child 52 dcb67a8f24be
use improved URLConfig/URLType
channels.py
handlers.py
log_formatter.py
urls.py
utils.py
--- a/channels.py	Mon Feb 09 00:24:13 2009 +0200
+++ b/channels.py	Mon Feb 09 01:11:05 2009 +0200
@@ -47,6 +47,12 @@
         """
 
         return self.channels[channel_name]
+    
+    def dict (self) :
+        """
+            Returns a { name: LogChannel } dict
+        """
+        return self.channels
 
     def __iter__ (self) :
         """
--- a/handlers.py	Mon Feb 09 00:24:13 2009 +0200
+++ b/handlers.py	Mon Feb 09 01:11:05 2009 +0200
@@ -2,6 +2,8 @@
     Our URL action handlers
 """
 
+import pytz
+
 from qmsk.web import http, template
 
 import urls, channels, helpers
@@ -37,7 +39,10 @@
     # get latest events
     lines = channel.source.get_latest(count)
 
-    # format
+    # formatter
+    formatter = formatter(pytz.utc, '%H:%M:%S')
+    
+    # lines
     lines = formatter.format_html(lines)
 
     return templates.render_to_response("channel_view",
@@ -61,19 +66,28 @@
     else :
         raise http.ResponseError("Unknown filetype %r" % format)
 
+def channel_calendar (request, channel) :
+    """
+        Display a list of avilable logs for some days
+    """
+
+    pass
+
 def channel_date (request, channel, date, formatter) :
     """
         Display all log data for the given date
     """
     
     # XXX: fix date timezone
-    import pytz
     date = date.replace(tzinfo=pytz.utc)
 
     # get latest events
     lines = channel.source.get_date(date)
 
-    # format
+    # formatter
+    formatter = formatter(pytz.utc, '%H:%M:%S')
+    
+    # lines
     lines = formatter.format_html(lines)
 
     return templates.render_to_response("channel_date",
--- a/log_formatter.py	Mon Feb 09 00:24:13 2009 +0200
+++ b/log_formatter.py	Mon Feb 09 01:11:05 2009 +0200
@@ -10,6 +10,9 @@
     """
         Provides a method to format series of LogLines into various output formats, with varying themes
     """
+
+    # the formatter's code name
+    name = None
     
     def __init__ (self, tz, timestamp_fmt="%H:%M:%S") :
         """
@@ -72,6 +75,8 @@
         Implements plain black-and-white irssi-style formatting
     """
 
+    name = 'irssi'
+
     def format_html (self, lines) :
         """
             Just uses format_txt, but wraps in <pre></pre>
--- a/urls.py	Mon Feb 09 00:24:13 2009 +0200
+++ b/urls.py	Mon Feb 09 01:11:05 2009 +0200
@@ -10,16 +10,23 @@
 import handlers
 
 # for types
-import channels, log_formatter, utils
+import utils
+
+# for configuration
+import channels, log_formatter
 
 # our URLConfig
 urls = url = urltree.URLConfig(
-    type_dict   = { 
-        # lookup LogChannel
-        'cid':  channels.channel_list.lookup,
-        'fmt':  log_formatter.by_name,
-        'date': utils.Date(None, '%Y-%m-%d'),
-    }
+    type_dict   = dict(
+        # LogChannel
+        cid     = utils.URLChannelName(channels.channel_list.dict()),
+
+        # LogFormatter
+        fmt     =  utils.URLFormatterName(log_formatter.FORMATTERS),
+
+        # datetime
+        date    = utils.URLDateType('%Y-%m-%d'),
+    )
 )
 
 # urls
@@ -27,6 +34,7 @@
 channel_select  = url('/channel_select/?channel:cid',                                   handlers.channel_select         )
 channel_view    = url('/channels/{channel:cid}/?count:int=10&formatter:fmt=irssi',      handlers.channel_view           )
 channel_last    = url('/channels/{channel:cid}/last/{count:int=100}/{format=html}',     handlers.channel_last           )
+channel_date    = url('/channels/{channel:cid}/calendar',                               handlers.channel_calendar       )
 channel_date    = url('/channels/{channel:cid}/date/{date:date}/?formatter:fmt=irssi',  handlers.channel_date           )
 channel_search  = url('/channels/{channel:cid}/search/?q',                              handlers.channel_search         )
 
--- a/utils.py	Mon Feb 09 00:24:13 2009 +0200
+++ b/utils.py	Mon Feb 09 01:11:05 2009 +0200
@@ -4,25 +4,84 @@
 
 import datetime
 
-class Date (object) :
+from qmsk.web.urltree import URLType
+
+class URLChannelName (URLType) :
     """
-        Handle dates in URLs as datetime objects (with indeterminate time info) in some timezone
+        Handle LogChannel names in URLs. Deals with instances of LogChannel
     """
 
-    def __init__ (self, tz, date_fmt="%Y-%m-%d") :
+    def __init__ (self, channels) :
         """
-            Format/parse dates in the given timezone using the given format
+            Use the given { name -> LogChannel } dict
+        """
+
+        self.channels = channels
+    
+    def parse (self, chan_name) :
+        """
+            chan_name -> LogChannel
+        """
+
+        return self.channels[chan_name]
+
+    def build (self, chan) :
+        """
+            LogChannel -> chan_name
+        """
+
+        return chan.id
+
+class URLFormatterName (URLType) :
+    """
+        Handle LogFormatter names in URLs. Note that they evaluate into the LogFormatter class itself, not an
+        instance, although build requiers an instance
+    """
+
+    def __init__ (self, formatters) :
+        """
+            Use the given { name -> class LogFormatter } dict
+        """
+
+        self.formatters = formatters
+    
+    def parse (self, fmt_name) :
+        """
+            fmt_name -> class LogFormatter
+        """
+
+        return self.formatters[fmt_name]
+    
+    def build (self, fmt) :
+        """
+            LogFormatter -> fmt_name
+        """
+
+        return fmt.name
+
+class URLDateType (URLType) :
+    """
+        Handle dates in URLs as naive datetime objects (with indeterminate time info)
+    """
+
+    def __init__ (self, date_fmt="%Y-%m-%d") :
+        """
+            Format/parse dates using the given format
+        """
+
+        self.date_fmt = date_fmt
+    
+    def parse (self, date_str) :
+        """
+            date_str -> naive datetime.datetime
         """
         
-        self.tz = tz
-        self.date_fmt = date_fmt
+        return datetime.datetime.strptime(date_str, self.date_fmt)
     
-    __name__ = "date"
+    def build (self, date) :
+        """
+            datetime.date -> date_str
+        """
 
-    def __call__ (self, date_str) :
-        """
-            Parse the given date string
-        """
-        
-        return datetime.datetime.strptime(date_str, self.date_fmt).replace(tzinfo=self.tz)
+        return date.strftime(self.date_fmt)