utils.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 22:17:10 +0200
changeset 72 5ade0288f2ec
parent 53 8103d18907a0
child 73 5a7188bf2894
permissions -rw-r--r--
implement line-links as UTC timestamps
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Miscellaneous things
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
     5
import datetime, calendar, pytz
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     7
from qmsk.web.urltree import URLType
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     8
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     9
class URLChannelName (URLType) :
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    11
        Handle LogChannel names in URLs. Deals with instances of LogChannel
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    14
    def __init__ (self, channels) :
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        """
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    16
            Use the given { name -> LogChannel } dict
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    17
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    18
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    19
        self.channels = channels
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    20
    
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    21
    def parse (self, chan_name) :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    22
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    23
            chan_name -> LogChannel
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    24
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    25
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    26
        return self.channels[chan_name]
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    27
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    28
    def build (self, chan) :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    29
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    30
            LogChannel -> chan_name
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    31
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    32
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    33
        return chan.id
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    34
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    35
class URLDateType (URLType) :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    36
    """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    37
        Handle dates in URLs as naive datetime objects (with indeterminate time info)
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    38
    """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    39
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    40
    def __init__ (self, date_fmt="%Y-%m-%d") :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    41
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    42
            Format/parse dates using the given format
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    43
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    44
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    45
        self.date_fmt = date_fmt
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    46
    
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    47
    def parse (self, date_str) :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    48
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    49
            date_str -> naive datetime.datetime
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    52
        return datetime.datetime.strptime(date_str, self.date_fmt)
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    54
    def build (self, date) :
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    55
        """
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    56
            datetime.date -> date_str
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    57
        """
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
51
07ca28f3a9f2 use improved URLConfig/URLType
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    59
        return date.strftime(self.date_fmt)
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    61
class URLTimestampType (URLType) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    62
    """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    63
        Handles an integer UNIX timestamp as an UTC datetime
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    64
    """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    65
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    66
    def parse (self, timestamp_str) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    67
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    68
            timestamp_str -> pytz.utc datetime.datetime
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    69
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    70
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    71
        return datetime.datetime.utcfromtimestamp(int(timestamp_str)).replace(tzinfo=pytz.utc)
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    72
    
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    73
    def build (self, dtz) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    74
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    75
            pytz.utc datetime.datetime -> timestamp_str
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    76
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    77
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    78
        return str(calendar.timegm(dtz.utctimetuple()))
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    79