log_line.py
author Tero Marttila <terom@fixme.fi>
Tue, 10 Feb 2009 23:00:11 +0200
changeset 86 645cf9c4441e
parent 64 cdb6403c2498
child 90 275a675712f1
permissions -rw-r--r--
implement full parser+formatter for irssi
"""
    An IRC logfile consists of a series of lines/events
"""

class LogTypes :
    """
        Definitions of the various LogLines types:

            LogTypes.RAW
            LogTypes.LOG_OPEN
            LogTypes.LOG_CLOSE

            LogTypes.MSG
            LogTypes.NOTICE
            LogTypes.ACTION

            LogTypes.JOIN
            LogTypes.PART
            LogTypes.KICK
            LogTypes.MODE

            LogTypes.NICK
            LogTypes.QUIT

            LogTypes.TOPIC

            LogTypes.SELF_NOTICE
            LogTypes.SELF_NICK
    """
 
    # list of LogType values by name
    LIST = [
        ## special
        # unknown type, may or may not have a timestamp, no source, only data
        ('RAW',         0x01),

        # log opened
        ('LOG_OPEN',    0x02),

        # log closed
        ('LOG_CLOSE',   0x03),

        ## messages
        # normal message
        ('MSG',         0x10),
        
        # notice
        ('NOTICE',      0x11),

        # CTCP action
        ('ACTION',      0x12),
        
        ## user-channel stats
        # join
        ('JOIN',        0x21),

        # part
        ('PART',        0x22),

        # kick
        ('KICK',        0x25),
     
        # channel modes
        ('MODE',        0x26),
        
        ## user status
        # nick-change
        ('NICK',        0x31),

        # quit
        ('QUIT',        0x32),

        ## general channel status
        # topic changed
        ('TOPIC',       0x41),

        ## our own actions
        ('SELF_NOTICE', 0x51),
        ('SELF_NICK',   0x52),
    ]
    
    @classmethod
    def name_from_code (cls, code) :
        """
            Looks up a LogType name by code
        """

        return dict((type, name) for name, type in cls.LIST)[code]

# apply as attributes
for name, code in LogTypes.LIST :
    setattr(LogTypes, name, code)

class LogLine (object) :
    """
        An event on some specific channel
    """

    # the LogChannel
    channel = None

    # the offset, only garunteed to be unique for a specific channel and date
    offset = None

    # the event type, as defiend in LogTypes
    type = None

    # the UTC timestamp of the event
    timestamp = None

    # the event source, this should be a 
    source = None

    # possible event target, for certain types (kick, nick)
    target = None

    # associated data (message, etc)
    data = None
    
    def __init__ (self, channel, offset, type, timestamp, source, target, data) :
        """
            Initialize with given values
        """
        
        self.channel = channel
        self.offset = offset
        self.type = type
        self.timestamp = timestamp
        self.source = source
        self.target = target
        self.data = data
    
    def __repr__ (self) :
        return "channel=%s, offset=%s, type=%s, timestamp=%s, source=%s, target=%s, data=%s" % (
            self.channel, self.offset, LogTypes.name_from_code(self.type), self.timestamp, self.source, self.target, self.data
        )