utils.py
changeset 51 07ca28f3a9f2
parent 50 f13cf27a360b
child 53 8103d18907a0
equal deleted inserted replaced
50:f13cf27a360b 51:07ca28f3a9f2
     2     Miscellaneous things
     2     Miscellaneous things
     3 """
     3 """
     4 
     4 
     5 import datetime
     5 import datetime
     6 
     6 
     7 class Date (object) :
     7 from qmsk.web.urltree import URLType
       
     8 
       
     9 class URLChannelName (URLType) :
     8     """
    10     """
     9         Handle dates in URLs as datetime objects (with indeterminate time info) in some timezone
    11         Handle LogChannel names in URLs. Deals with instances of LogChannel
    10     """
    12     """
    11 
    13 
    12     def __init__ (self, tz, date_fmt="%Y-%m-%d") :
    14     def __init__ (self, channels) :
    13         """
    15         """
    14             Format/parse dates in the given timezone using the given format
    16             Use the given { name -> LogChannel } dict
       
    17         """
       
    18 
       
    19         self.channels = channels
       
    20     
       
    21     def parse (self, chan_name) :
       
    22         """
       
    23             chan_name -> LogChannel
       
    24         """
       
    25 
       
    26         return self.channels[chan_name]
       
    27 
       
    28     def build (self, chan) :
       
    29         """
       
    30             LogChannel -> chan_name
       
    31         """
       
    32 
       
    33         return chan.id
       
    34 
       
    35 class URLFormatterName (URLType) :
       
    36     """
       
    37         Handle LogFormatter names in URLs. Note that they evaluate into the LogFormatter class itself, not an
       
    38         instance, although build requiers an instance
       
    39     """
       
    40 
       
    41     def __init__ (self, formatters) :
       
    42         """
       
    43             Use the given { name -> class LogFormatter } dict
       
    44         """
       
    45 
       
    46         self.formatters = formatters
       
    47     
       
    48     def parse (self, fmt_name) :
       
    49         """
       
    50             fmt_name -> class LogFormatter
       
    51         """
       
    52 
       
    53         return self.formatters[fmt_name]
       
    54     
       
    55     def build (self, fmt) :
       
    56         """
       
    57             LogFormatter -> fmt_name
       
    58         """
       
    59 
       
    60         return fmt.name
       
    61 
       
    62 class URLDateType (URLType) :
       
    63     """
       
    64         Handle dates in URLs as naive datetime objects (with indeterminate time info)
       
    65     """
       
    66 
       
    67     def __init__ (self, date_fmt="%Y-%m-%d") :
       
    68         """
       
    69             Format/parse dates using the given format
       
    70         """
       
    71 
       
    72         self.date_fmt = date_fmt
       
    73     
       
    74     def parse (self, date_str) :
       
    75         """
       
    76             date_str -> naive datetime.datetime
    15         """
    77         """
    16         
    78         
    17         self.tz = tz
    79         return datetime.datetime.strptime(date_str, self.date_fmt)
    18         self.date_fmt = date_fmt
       
    19     
    80     
    20     __name__ = "date"
    81     def build (self, date) :
       
    82         """
       
    83             datetime.date -> date_str
       
    84         """
    21 
    85 
    22     def __call__ (self, date_str) :
    86         return date.strftime(self.date_fmt)
    23         """
       
    24             Parse the given date string
       
    25         """
       
    26         
       
    27         return datetime.datetime.strptime(date_str, self.date_fmt).replace(tzinfo=self.tz)
       
    28 
    87