"""
Miscellaneous things
"""
import datetime
from qmsk.web.urltree import URLType
class URLChannelName (URLType) :
"""
Handle LogChannel names in URLs. Deals with instances of LogChannel
"""
def __init__ (self, channels) :
"""
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
"""
return datetime.datetime.strptime(date_str, self.date_fmt)
def build (self, date) :
"""
datetime.date -> date_str
"""
return date.strftime(self.date_fmt)