log_source.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 11:05:53 +0200
changeset 64 cdb6403c2498
parent 63 416560b82116
child 65 8b50694f841e
permissions -rw-r--r--
beginnings of a LogSearchIndex class
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    A source of IRC log files
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     5
import datetime, calendar, itertools
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: 48
diff changeset
     6
import os, errno
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import pytz
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
class LogSource (object) :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        A collection of IRC logs for a specific target in some format. Provides the possibility to read specific events
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    def get_latest (self, count) :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
            Yield the latest events, up to `count` of them.
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        abstract
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: 48
diff changeset
    20
    
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    21
    def get_date (self, dt) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    22
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    23
            Get logs for the given date (as a datetime)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    24
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    25
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    26
        abstract
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    27
    
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    28
    def get_month_days (self, dt) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    29
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    30
            Get a set of dates, telling which days in the given month (as a datetime) have logs available
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    31
        """
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    33
        abstract
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    34
    
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    35
    def get_search (self, query) :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    36
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    37
            Search the logs for the given query
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    38
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    39
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    40
        abstract
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    41
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    42
class LogFile (object) :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        A file containing LogEvents
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    45
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    46
        XXX: modify to implement LogSource?
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
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: 48
diff changeset
    49
    def __init__ (self, path, parser, start_date=None, charset='utf-8', sep='\n') :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        """
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: 48
diff changeset
    51
            Open the file at the given path, which contains data with the given charset, as lines separated by the
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    52
            given separator. Lines are parsed using the given parser, using the given date as an initial date, see
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    53
            LogParser for more info. XXX: currently we assume start_date also for the end of the file
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        # store
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        self.path = path
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: 48
diff changeset
    58
        self.parser = parser
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    59
        self.start_date = start_date
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        self.charset = charset
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        self.sep = sep
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
        # open
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    64
        self.file = open(path, 'rb')
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    def __iter__ (self) :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        """
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: 48
diff changeset
    68
            Yields a series of unicode lines, as read from the top of the file
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        # seek to beginning
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        self.file.seek(0)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
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: 48
diff changeset
    74
        # iterate over lines, decoding them as well
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    75
        return (line.decode(self.charset) for line in self.file)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    
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: 48
diff changeset
    77
    def read_full (self) :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        """
64
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
    79
            Reads all LogLines. The LogLines will have a valid offset
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: 48
diff changeset
    80
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    81
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    82
        # just use our __iter__
64
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
    83
        return self.parser.parse_lines(self, self.start_date, starting_offset=1)
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: 48
diff changeset
    84
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    85
    def read_from (self, dt) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    86
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    87
            Reads all LogLines from the given naive timestamp onwards
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    88
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    89
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    90
        # start reading at beginning
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    91
        events = self.read_full()
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    92
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    93
        # skip unwanted events
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    94
        for event in events :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    95
            if event.timestamp < dt :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    96
                continue
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    97
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    98
            else :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    99
                # include this line as well
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   100
                yield event
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   101
                break
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   102
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   103
        # yield the rest as-is
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   104
        for event in events :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   105
            yield event
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   106
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   107
    def read_until (self, dt) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   108
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   109
            Reads all LogLines up until the given naive timestamp
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
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: 48
diff changeset
   112
        # start reading events at the beginning
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   113
        events = self.read_full()
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   114
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   115
        # yield events until we hit the given timestamp
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   116
        for event in events :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   117
            if event.timestamp <= dt :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   118
                yield event
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   119
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   120
            else :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   121
                break
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   122
            
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   123
        # ignore the rest
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   124
        return
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   125
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   126
    def _read_blocks_reverse (self, blocksize=1024) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   127
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   128
            Yields blocks of file data in reverse order, starting at the end of the file
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   129
        """
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        # seek to end of file
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
        self.file.seek(0, os.SEEK_END)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
        # read offset
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   135
        # XXX: hack -1 to get rid of trailing newline
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   136
        size = offset = self.file.tell() - 1
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: 48
diff changeset
   137
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   138
        # do not try to read past the beginning of the file
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   139
        while offset > 0:
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   140
            # calc new offset + size
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: 48
diff changeset
   141
            if offset > blocksize :
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   142
                # full block
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: 48
diff changeset
   143
                offset -= blocksize
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   144
                read_size = blocksize
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   146
            else :
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   147
                # partial block
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   148
                read_size = offset
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
                offset = 0
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
43
fc11c4e86a82 implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   151
            # seek to offset
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
            self.file.seek(offset)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   154
            # read the data we want
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: 48
diff changeset
   155
            block = self.file.read(read_size)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   157
            # sanity check
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: 48
diff changeset
   158
            assert len(block) == read_size
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
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: 48
diff changeset
   160
            # yield 
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   161
            yield block
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   162
    
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   163
    def _read_lines_reverse (self) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   164
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   165
            Yields decoded lines from the end of the file, in reverse order.
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   166
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   167
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   168
        # partial lines
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   169
        buf = ''
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   170
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   171
        # read from end of file, a block at a time
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   172
        for block in self._read_blocks_reverse() :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
            # add in our previous buf
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: 48
diff changeset
   174
            buf = block + buf
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
            
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: 48
diff changeset
   176
            # split up lines
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   177
            lines = buf.split(self.sep)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
            # keep the first one as our buffer, as it's incomplete
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: 48
diff changeset
   180
            buf = lines[0]
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   181
           
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   182
            # yield the rest a line at a time in reverse order... this looks weird, but that's how slicing works :)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   183
            # XXX: use something like islice, this has to build a slice object
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   184
            for line in lines[:0:-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: 48
diff changeset
   185
                yield line.decode(self.charset)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   187
    def read_latest (self, count) :
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: 48
diff changeset
   188
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   189
            Returns up to count events, from the end of the file, or less, if the file doesn't contain that many lines.
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   190
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   191
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   192
        # the list of lines
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   193
        lines = []
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   194
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   195
        # start reading lines into lines
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   196
        for line in self._read_lines_reverse() :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   197
            # append
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   198
            lines.append(line)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   199
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   200
            # done?
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   201
            if len(lines) >= count :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   202
                break
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   203
        
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: 48
diff changeset
   204
        # decode in reverse order, using our starting date....
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   205
        # XXX: use lines[::-1] or reversed?
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   206
        # XXX: it may make more sense to parse in reverse order, using 'self.end_date' or something like that
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   207
        return self.parser.parse_lines(reversed(lines), self.start_date)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
class LogDirectory (LogSource) :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
        A directory containing a series of timestamped LogFiles
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
    """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
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: 48
diff changeset
   214
    def __init__ (self, path, tz, parser, charset='utf-8', filename_fmt='%Y-%m-%d') :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
            Load the logfiles at the given path.
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
            
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
            The files contain data in the given charset, and are named according the the date in the given timezone and
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: 48
diff changeset
   219
            date format, and will be parsed using the given parser.
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
        # store
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
        self.path = path
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
        self.tz = tz
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: 48
diff changeset
   225
        self.parser = parser
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
        self.charset = charset
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
        self.filename_fmt = filename_fmt
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
    def _get_logfile_datetime (self, dt) :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
            Get the logfile corresponding to the given datetime
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
        # convert to target timezone
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
        dtz = dt.astimezone(self.tz)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
        # convert to date and use that
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
        return self._get_logfile_date(dtz.date())
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   240
    def _get_logfile_date (self, d, load=True) :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
        """
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   242
            Get the logfile corresponding to the given naive date in our timezone. If load is False, only test for the
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   243
            presence of the logfile, do not actually open it.
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   244
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   245
            Returns None if the logfile does not exist.
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
        # format filename
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   249
        filename = d.strftime(self.filename_fmt)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
        # build path
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
        path = os.path.join(self.path, filename)
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   253
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   254
        try :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   255
            if load :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   256
                # open+return the LogFile
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   257
                return LogFile(path, self.parser, d, self.charset)
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   258
            
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   259
            else :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   260
                # test
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   261
                return os.path.exists(path)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   263
        # XXX: move to LogFile
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   264
        except IOError, e :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   265
            # return None for missing files
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   266
            if e.errno == errno.ENOENT :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   267
                return None
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   268
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   269
            else :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   270
                raise
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
    
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: 48
diff changeset
   272
    def _iter_date_reverse (self, dt=None) :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
            Yields an infinite series of naive date objects in our timezone, iterating backwards in time starting at the
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
            given *datetime*, or the the current date, if none given
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   276
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   277
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   278
        # default to now
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
        if not dt :
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: 48
diff changeset
   280
            dt = datetime.datetime.now(pytz.utc)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   282
        # convert to target timezone
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
        dtz = dt.astimezone(self.tz)
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   284
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
        # our timedelta
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: 48
diff changeset
   286
        ONE_DAY = datetime.timedelta(1)
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   287
        
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
        # iterate unto infinity
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
        while True :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
            # yield
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   291
            yield dtz.date()
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   292
            
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
            # one day sdrawkcab
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
            dtz -= ONE_DAY
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
    
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   296
    def _iter_logfile_reverse (self, dt=None, max_files=100) :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
        """
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   298
            Yields a series of LogFile objects, iterating backwards in time starting at the given datetime, or the
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   299
            current date, if none given.
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   300
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   301
            Reads/probes at most max_files files.
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
        """
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   303
        
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   304
        # start counting at zero...
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   305
        file_count = 0
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   306
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   307
        # iterate backwards over days
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   308
        for day in self._iter_date_reverse(dt) :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   309
            # stop if we've handled enough files by now
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   310
            if file_count > max_files :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   311
                break
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   312
            
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   313
            # try and open the next logfile
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
            logfile = None
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   315
            
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   316
            file_count += 1
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   317
            logfile = self._get_logfile_date(day)
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   318
            
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   319
            # no logfile there?
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   320
            if not logfile :
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   321
                # if we didn't find any logfiles at all, terminate rudely
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   322
                if file_count > max_files :
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   323
                    raise Exception("No recent logfiles found")
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   324
                
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   325
                else :
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   326
                    # skip to next day
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   327
                    continue
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   328
            
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   329
            # yield it
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   330
            yield logfile
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   331
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   332
    def get_latest (self, count) :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   333
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   334
            Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   335
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   336
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   337
        # iterate over logfiles
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   338
        iter = self._iter_logfile_reverse()
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   339
        
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   340
        # read the events into here
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   341
        lines = []
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   342
        
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   343
        # loop until done
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   344
        while len(lines) < count :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   345
            # next logfile
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   346
            logfile = iter.next()
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   347
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: 48
diff changeset
   348
            # read the events
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   349
            # XXX: use a queue
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   350
            lines = list(logfile.read_latest(count)) + lines
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   351
        
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: 48
diff changeset
   352
        # return the events
48
7858b7b8ffe3 fix bugs with file tailing
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   353
        return lines
41
9585441a4bfb working basic logs stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   354
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: 48
diff changeset
   355
    def get_date (self, dt) :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   356
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   357
            A 'day' is considered to be a 24-hour period from 00:00:00 23:59:59. If the timezone of the given datetime
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   358
            differs from our native datetime, this may involve lines from more than one logfile.
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   359
        """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   360
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   361
        # begin/end of 24h period, in target timezone
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   362
        dtz_begin = dt.replace(hour=0, minute=0, second=0).astimezone(self.tz)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   363
        dtz_end = dt.replace(hour=23, minute=59, second=59, microsecond=999999).astimezone(self.tz)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   364
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   365
        # as dates
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   366
        d_begin = dtz_begin.date() 
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   367
        d_end = dtz_end.date()
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   368
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   369
        # if they're the same, just pull the full log for that date
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   370
        if d_begin == d_end :
64
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   371
            # open that log
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   372
            logfile = self._get_logfile_date(d_begin)
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   373
            
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   374
            if not logfile :
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   375
                raise Exception("No logfile for date=%r" % (dt, ))
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   376
            
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   377
            # return the full data
cdb6403c2498 beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   378
            return logfile.read_full()
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: 48
diff changeset
   379
        
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   380
        # otherwise, we need to pull two partial logs
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   381
        else :
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   382
            # open both of them
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   383
            f_begin = self._get_logfile_date(d_begin)
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   384
            f_end = self._get_logfile_date(d_end)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   385
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: 48
diff changeset
   386
            # chain together the two sources
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   387
            return itertools.chain(
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   388
                f_begin.read_from(dtz_begin), 
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   389
                f_end.read_until(dtz_end) if f_end else []
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   390
            )
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: 48
diff changeset
   391
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   392
    def get_month_days (self, month) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   393
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   394
            Returns a set of dates for which logfiles are available in the given datetime's month
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   395
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   396
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   397
        # the set of days
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   398
        days = set()
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   399
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   400
        # iterate over month's days using Calendar
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   401
        for date in calendar.Calendar().itermonthdates(month.year, month.month) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   402
            # convert date to target datetime
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   403
            dtz = month.tzinfo.localize(datetime.datetime.combine(date, datetime.time(0))).astimezone(self.tz)
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   404
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   405
            # date in our target timezone
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   406
            log_date = dtz.date()
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   407
            
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   408
            # test for it
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   409
            if self._get_logfile_date(log_date, load=False) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   410
                # add to set
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   411
                days.add(date)
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   412
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   413
        # return set
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   414
        return days
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   415
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   416
    def get_search (self, query) :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   417
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   418
            Just inspect the latest logfile
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   419
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   420
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   421
        # one logfile
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   422
        logfile = self._iter_logfile_reverse().next()
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   423
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   424
        # inspect each line
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   425
        for line in logfile.read_full() :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   426
            # XXX: use proper LogQuery stuff
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   427
            if query in line.data :
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   428
                yield line
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   429
        
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   430