qmsk/irclogs/utils.py
changeset 140 6db2527b67cf
parent 129 67a30d680f60
equal deleted inserted replaced
139:9c7769850195 140:6db2527b67cf
       
     1 """
       
     2     Miscellaneous things
       
     3 """
       
     4 
       
     5 import datetime, calendar, pytz
       
     6 import os, errno
       
     7 
       
     8 from qmsk.web.urltree import URLType
       
     9 
       
    10 class URLChannelName (URLType) :
       
    11     """
       
    12         Handle LogChannel names in URLs. Deals with instances of LogChannel
       
    13     """
       
    14 
       
    15     def __init__ (self, channels) :
       
    16         """
       
    17             Use the given { name -> LogChannel } dict
       
    18         """
       
    19 
       
    20         self.channels = channels
       
    21     
       
    22     def parse (self, chan_name) :
       
    23         """
       
    24             chan_name -> LogChannel
       
    25         """
       
    26 
       
    27         return self.channels[chan_name]
       
    28 
       
    29     def build (self, chan) :
       
    30         """
       
    31             LogChannel -> chan_name
       
    32         """
       
    33 
       
    34         return chan.id
       
    35 
       
    36 class URLDateType (URLType) :
       
    37     """
       
    38         Handle dates in URLs as naive datetime objects (with indeterminate time info)
       
    39     """
       
    40 
       
    41     def __init__ (self, date_fmt) :
       
    42         """
       
    43             Format/parse dates using the given format
       
    44         """
       
    45 
       
    46         self.date_fmt = date_fmt
       
    47     
       
    48     def parse (self, date_str) :
       
    49         """
       
    50             date_str -> naive datetime.datetime
       
    51         """
       
    52         
       
    53         return datetime.datetime.strptime(date_str, self.date_fmt)
       
    54     
       
    55     def build (self, date) :
       
    56         """
       
    57             datetime.date -> date_str
       
    58         """
       
    59 
       
    60         return date.strftime(self.date_fmt)
       
    61 
       
    62 class URLTimestampType (URLType) :
       
    63     """
       
    64         Handles an integer UNIX timestamp as an UTC datetime
       
    65     """
       
    66 
       
    67     def parse (self, timestamp_str) :
       
    68         """
       
    69             timestamp_str -> pytz.utc datetime.datetime
       
    70         """
       
    71         
       
    72         return from_utc_timestamp(int(timestamp_str))
       
    73     
       
    74     def build (self, dtz) :
       
    75         """
       
    76             pytz.utc datetime.datetime -> timestamp_str
       
    77         """
       
    78         
       
    79         return str(to_utc_timestamp(dtz))
       
    80 
       
    81 def from_utc_timestamp (timestamp) :
       
    82     """
       
    83         Converts a UNIX timestamp into a datetime.datetime
       
    84     """
       
    85 
       
    86     return datetime.datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc)
       
    87 
       
    88 def to_utc_timestamp (dt) :
       
    89     """
       
    90         Converts a datetime.datetime into a UNIX timestamp
       
    91     """
       
    92 
       
    93     return calendar.timegm(dt.utctimetuple())
       
    94 
       
    95 def mtime (path, ignore_missing=False) :
       
    96     """
       
    97         Gets the mtime for the given path as an UTC datetime, or None, if the file doesn't exist and ignore_missing
       
    98     """
       
    99 
       
   100     try :
       
   101         # stat
       
   102         st = os.stat(path)
       
   103     
       
   104     # trap IOError
       
   105     except os.error, e :
       
   106         # ENOENT?
       
   107         if ignore_missing and e.errno == errno.ENOENT :
       
   108             return None
       
   109 
       
   110         else :
       
   111             raise
       
   112 
       
   113     else :
       
   114         # decode
       
   115         return from_utc_timestamp(st.st_mtime)
       
   116 
       
   117 class FixedOffsetTimezone (pytz._FixedOffset) :
       
   118     """
       
   119         A Fixed-offset timezone with no DST info, compatible with pytz.
       
   120 
       
   121         This is based on pytz._FixedOffset, but overrides dst() to return timedelta(0)
       
   122     """
       
   123 
       
   124     def __init__ (self, minutes) :
       
   125         """
       
   126             Minutes is simply the offset from UTC in minutes, positive or negative, at most 24h.
       
   127         """
       
   128 
       
   129         pytz._FixedOffset.__init__(self, minutes)
       
   130 
       
   131     def dst (self, dt) :
       
   132         """
       
   133             No DST info
       
   134         """
       
   135 
       
   136         return datetime.timedelta(0)
       
   137