utils.py
changeset 51 07ca28f3a9f2
parent 50 f13cf27a360b
child 53 8103d18907a0
--- 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)