annotate MoinMoin/logfile/__init__.py @ 2044:967d529b31e9

workaround for initially non-existing edit-log, remove edit-log from repo
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 02 Jun 2007 16:35:58 +0200
parents 3475126f78c6
children 94af8c2afeb0
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin - LogFile package
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
5 This module supports buffered log reads, iterating forward and backward line-by-line, etc.
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
6
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1856
diff changeset
7 @copyright: 2005-2007 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 """
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
10 import logging
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
12 import os, codecs, errno
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
13 from MoinMoin import config, wikiutil
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
14
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
15 class LogError(Exception):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
16 """ Base class for log errors """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
17
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
18 class LogMissing(LogError):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
19 """ Raised when the log is missing """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
20
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
21
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
22 class LineBuffer:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
23 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
24 Reads lines from a file
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
25 self.len number of lines in self.lines
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
26 self.lines list of lines (unicode)
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
27 self.offsets list of file offsets for each line. additionally the position
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
28 after the last read line is stored into self.offsets[-1]
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
29 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
30 def __init__(self, file, offset, size, forward=True):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
31 """
1986
3475126f78c6 fix some builtin names used as attribute names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
32
3475126f78c6 fix some builtin names used as attribute names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
33 TODO: when this gets refactored, don't use "file" (is a builtin)
3475126f78c6 fix some builtin names used as attribute names
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
34
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
35 @param file: open file object
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
36 @param offset: position in file to start from
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
37 @param size: aproximate number of bytes to read
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
38 @param forward : read from offset on or from offset-size to offset
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
39 @type forward: boolean
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
40 """
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
41 self.loglevel = logging.NOTSET
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
42 if forward:
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
43 begin = offset
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
44 logging.log(self.loglevel, "LineBuffer.init: forward seek %d read %d" % (begin, size))
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
45 file.seek(begin)
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
46 lines = file.readlines(size)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
47 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
48 if offset < 2 * size:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
49 begin = 0
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
50 size = offset
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
51 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
52 begin = offset - size
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
53 logging.log(self.loglevel, "LineBuffer.init: backward seek %d read %d" % (begin, size))
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
54 file.seek(begin)
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
55 lines = file.read(size).splitlines(True)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
56 if begin != 0:
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
57 # remove potentially incomplete first line
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
58 begin += len(lines[0])
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
59 lines = lines[1:]
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
60 # XXX check for min one line read
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
61
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
62 linecount = len(lines)
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
63
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
64 # now calculate the file offsets of all read lines
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
65 offsets = [len(line) for line in lines]
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
66 offsets.append(0) # later this element will have the file offset after the last read line
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
67
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
68 lengthpreviousline = 0
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
69 offset = begin
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
70 for i in xrange(linecount+1):
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
71 offset += lengthpreviousline
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
72 lengthpreviousline = offsets[i]
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
73 offsets[i] = offset
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
74
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
75 self.offsets = offsets
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
76 self.len = linecount
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
77 # Decode lines after offset in file is calculated
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
78 self.lines = [unicode(line, config.charset) for line in lines]
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
79
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
80
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
81 class LogFile:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
82 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
83 .filter: function that gets the values from .parser.
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
84 must return True to keep it or False to remove it
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
85 Overwrite .parser() and .add() to customize this class to special log files
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
86 """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
87
1838
e1d4cc010dec Reduced log file buffer to 4 kB.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1825
diff changeset
88 def __init__(self, filename, buffer_size=4096):
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
89 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
90 @param filename: name of the log file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
91 @param buffer_size: approx. size of one buffer in bytes
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
92 """
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
93 self.loglevel = logging.NOTSET
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
94 self.__filename = filename
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
95 self.__buffer = None # currently used buffer, points to one of the following:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
96 self.__buffer1 = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
97 self.__buffer2 = None
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
98 self.buffer_size = buffer_size
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
99 self.__lineno = 0
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
100 self.filter = None
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
101
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
102 def __iter__(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
103 return self
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
104
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
105 def reverse(self):
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
106 """ yield log entries in reverse direction starting from last one
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
107
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
108 @rtype: iterator
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
109 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
110 self.to_end()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
111 while 1:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
112 try:
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
113 logging.log(self.loglevel, "LogFile.reverse %s" % self.__filename)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
114 result = self.previous()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
115 except StopIteration:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
116 return
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
117 yield result
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
118
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
119 def sanityCheck(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
120 """ Check for log file write access.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
121
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
122 @rtype: string (error message) or None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
123 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
124 if not os.access(self.__filename, os.W_OK):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
125 return "The log '%s' is not writable!" % (self.__filename,)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
126 return None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
127
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
128 def __getattr__(self, name):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
129 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
130 generate some attributes when needed
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
131 """
1825
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
132 if name == "_LogFile__rel_index": # Python black magic: this is the real name of the __rel_index attribute
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
133 # starting iteration from begin
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
134 self.__buffer1 = LineBuffer(self._input, 0, self.buffer_size)
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
135 self.__buffer2 = LineBuffer(self._input,
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
136 self.__buffer1.offsets[-1],
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
137 self.buffer_size)
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
138 self.__buffer = self.__buffer1
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
139 self.__rel_index = 0
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
140 return 0
738bb2ec6b7f partially reverted changeset 1820, the code WAS used
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1820
diff changeset
141 elif name == "_input":
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
142 try:
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
143 # Open the file (NOT using codecs.open, it breaks our offset calculation. We decode it later.).
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
144 # Use binary mode in order to retain \r - otherwise the offset calculation would fail.
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
145 self._input = file(self.__filename, "rb",)
2044
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
146 except IOError, err:
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
147 if err.errno == 2: # POSIX errno.ENOENT "file not found"
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
148 try:
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
149 # XXX workaround if edit-log does not exist: just create it empty
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
150 f = file(self.__filename, "ab")
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
151 f.write('')
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
152 f.close()
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
153 self._input = file(self.__filename, "rb",)
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
154 return self._input
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
155 except:
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
156 pass
967d529b31e9 workaround for initially non-existing edit-log, remove edit-log from repo
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1986
diff changeset
157 raise StopIteration
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
158 return self._input
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
159 elif name == "_output":
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
160 self._output = codecs.open(self.__filename, 'a', config.charset)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
161 return self._output
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
162 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
163 raise AttributeError(name)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
164
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
165 def size(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
166 """ Return log size in bytes
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
167
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
168 Return 0 if the file does not exists. Raises other OSError.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
169
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
170 @return: size of log file in bytes
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
171 @rtype: Int
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
172 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
173 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
174 return os.path.getsize(self.__filename)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
175 except OSError, err:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
176 if err.errno == errno.ENOENT:
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
177 return 0
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
178 raise
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
179
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
180 def lines(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
181 """ Return number of lines in the log file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
182
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
183 Return 0 if the file does not exists. Raises other OSError.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
184
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
185 Expensive for big log files - O(n)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
186
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
187 @return: size of log file in lines
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
188 @rtype: Int
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
189 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
190 try:
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
191 f = file(self.__filename, 'r')
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
192 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
193 count = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
194 for line in f:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
195 count += 1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
196 return count
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
197 finally:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
198 f.close()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
199 except (OSError, IOError), err:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
200 if err.errno == errno.ENOENT:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
201 return 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
202 raise
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
203
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
204 def date(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
205 """ Return timestamp of log file in usecs """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
206 try:
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
207 mtime = os.path.getmtime(self.__filename)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
208 except OSError, err:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
209 if err.errno == errno.ENOENT:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
210 # This can happen on fresh wiki when building the index
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
211 # Usually the first request will create an event log
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
212 raise LogMissing(str(err))
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
213 raise
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
214 return wikiutil.timestamp2version(mtime)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
215
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
216 def peek(self, lines):
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
217 """ Move position in file forward or backwards by "lines" count
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
218
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
219 It adjusts .__lineno if set.
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
220 This function is not aware of filters!
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
221
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
222 @param lines: number of lines, may be negative to move backward
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
223 @rtype: boolean
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
224 @return: True if moving more than to the beginning and moving
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
225 to the end or beyond
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
226 """
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
227 logging.log(self.loglevel, "LogFile.peek %s" % self.__filename)
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
228 self.__rel_index += lines
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
229 while self.__rel_index < 0:
1646
2fbb179f3518 use filepos in (not mtime of) edit-log for editlog.news, fix some bugs in logfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
230 if self.__buffer is self.__buffer2:
1856
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
231 if self.__buffer.offsets[0] == 0:
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
232 # already at the beginning of the file
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
233 self.__rel_index = 0
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
234 self.__lineno = 0
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
235 return True
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
236 else:
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
237 # change to buffer 1
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
238 self.__buffer = self.__buffer1
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
239 self.__rel_index += self.__buffer.len
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
240 else: # self.__buffer is self.__buffer1
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
241 if self.__buffer.offsets[0] == 0:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
242 # already at the beginning of the file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
243 self.__rel_index = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
244 self.__lineno = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
245 return True
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
246 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
247 # load previous lines
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
248 self.__buffer2 = self.__buffer1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
249 self.__buffer1 = LineBuffer(self._input,
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
250 self.__buffer.offsets[0],
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
251 self.buffer_size,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
252 forward=False)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
253 self.__buffer = self.__buffer1
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
254 self.__rel_index += self.__buffer.len
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
255
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
256 while self.__rel_index >= self.__buffer.len:
1646
2fbb179f3518 use filepos in (not mtime of) edit-log for editlog.news, fix some bugs in logfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
257 if self.__buffer is self.__buffer1:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
258 # change to buffer 2
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
259 self.__rel_index -= self.__buffer.len
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
260 self.__buffer = self.__buffer2
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
261 else: # self.__buffer is self.__buffer2
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
262 # try to load next buffer
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
263 tmpbuff = LineBuffer(self._input,
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
264 self.__buffer.offsets[-1],
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
265 self.buffer_size)
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
266 if tmpbuff.len == 0:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
267 # end of file
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
268 if self.__lineno is not None:
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
269 self.__lineno += (lines -
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
270 (self.__rel_index - self.__buffer.len))
1646
2fbb179f3518 use filepos in (not mtime of) edit-log for editlog.news, fix some bugs in logfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
271 self.__rel_index = self.__buffer.len # point to after last read line
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
272 return True
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
273 # shift buffers
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
274 self.__rel_index -= self.__buffer.len
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
275 self.__buffer1 = self.__buffer2
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
276 self.__buffer2 = tmpbuff
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
277 self.__buffer = self.__buffer2
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
278
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
279 if self.__lineno is not None:
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
280 self.__lineno += lines
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
281 return False
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
282
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
283 def __next(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
284 """get next line already parsed"""
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
285 if self.peek(0):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
286 raise StopIteration
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
287 result = self.parser(self.__buffer.lines[self.__rel_index])
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
288 self.peek(1)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
289 return result
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
290
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
291 def next(self):
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
292 """get next line that passes through the filter
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
293 @return: next entry
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
294 raises StopIteration at file end
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
295 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
296 result = None
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
297 while result is None:
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
298 while result is None:
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
299 logging.log(self.loglevel, "LogFile.next %s" % self.__filename)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
300 result = self.__next()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
301 if self.filter and not self.filter(result):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
302 result = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
303 return result
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
304
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
305 def __previous(self):
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
306 """get previous line already parsed"""
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
307 if self.peek(-1):
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
308 raise StopIteration
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
309 return self.parser(self.__buffer.lines[self.__rel_index])
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
310
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
311 def previous(self):
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
312 """get previous line that passes through the filter
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
313 @return: previous entry
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
314 raises StopIteration at file begin
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
315 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
316 result = None
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
317 while result is None:
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
318 while result is None:
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
319 logging.log(self.loglevel, "LogFile.previous %s" % self.__filename)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
320 result = self.__previous()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
321 if self.filter and not self.filter(result):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
322 result = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
323 return result
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
324
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
325 def to_begin(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
326 """moves file position to the begin"""
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
327 logging.log(self.loglevel, "LogFile.to_begin %s" % self.__filename)
1820
9595d9d5e5ae logfile: removed some unused code, added some checks
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1818
diff changeset
328 if self.__buffer1 is None or self.__buffer1.offsets[0] != 0:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
329 self.__buffer1 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
330 0,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
331 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
332 self.__buffer2 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
333 self.__buffer1.offsets[-1],
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
334 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
335 self.__buffer = self.__buffer1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
336 self.__rel_index = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
337 self.__lineno = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
338
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
339 def to_end(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
340 """moves file position to the end"""
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
341 logging.log(self.loglevel, "LogFile.to_end %s" % self.__filename)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
342 self._input.seek(0, 2) # to end of file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
343 size = self._input.tell()
1820
9595d9d5e5ae logfile: removed some unused code, added some checks
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1818
diff changeset
344 if self.__buffer2 is None or size > self.__buffer2.offsets[-1]:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
345 self.__buffer2 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
346 size,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
347 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
348 forward=False)
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
349
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
350 self.__buffer1 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
351 self.__buffer2.offsets[0],
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
352 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
353 forward=False)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
354 self.__buffer = self.__buffer2
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
355 self.__rel_index = self.__buffer2.len
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
356 self.__lineno = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
357
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
358 def position(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
359 """ Return the current file position
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
360
1818
df984fe11783 logfile: cleanup/fix base class, fixes also EditLog.news result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1646
diff changeset
361 This can be converted into a String using back-ticks and then be rebuild.
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
362 For this plain file implementation position is an Integer.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
363 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
364 return self.__buffer.offsets[self.__rel_index]
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
365
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
366 def seek(self, position, line_no=None):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
367 """ moves file position to an value formerly gotten from .position().
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
368 To enable line counting line_no must be provided.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
369 .seek is much more efficient for moving long distances than .peek.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
370 raises ValueError if position is invalid
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
371 """
1855
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
372 logging.log(self.loglevel, "LogFile.seek %s pos %d" % (self.__filename, position))
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
373 if self.__buffer1:
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
374 logging.log(self.loglevel, "b1 %r %r" % (self.__buffer1.offsets[0], self.__buffer1.offsets[-1]))
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
375 if self.__buffer2:
472566340cd8 logfile: more flexible logging
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1838
diff changeset
376 logging.log(self.loglevel, "b2 %r %r" % (self.__buffer2.offsets[0], self.__buffer2.offsets[-1]))
1646
2fbb179f3518 use filepos in (not mtime of) edit-log for editlog.news, fix some bugs in logfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
377 if self.__buffer1 and self.__buffer1.offsets[0] <= position < self.__buffer1.offsets[-1]:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
378 # position is in .__buffer1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
379 self.__rel_index = self.__buffer1.offsets.index(position)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
380 self.__buffer = self.__buffer1
1646
2fbb179f3518 use filepos in (not mtime of) edit-log for editlog.news, fix some bugs in logfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1317
diff changeset
381 elif self.__buffer2 and self.__buffer2.offsets[0] <= position < self.__buffer2.offsets[-1]:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
382 # position is in .__buffer2
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
383 self.__rel_index = self.__buffer2.offsets.index(position)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
384 self.__buffer = self.__buffer2
1856
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
385 elif self.__buffer1 and self.__buffer1.offsets[-1] == position:
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
386 # we already have one buffer directly before where we want to go
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
387 self.__buffer2 = LineBuffer(self._input,
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
388 position,
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
389 self.buffer_size)
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
390 self.__buffer = self.__buffer2
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
391 self.__rel_index = 0
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
392 elif self.__buffer2 and self.__buffer2.offsets[-1] == position:
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
393 # we already have one buffer directly before where we want to go
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
394 self.__buffer1 = self.__buffer2
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
395 self.__buffer2 = LineBuffer(self._input,
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
396 position,
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
397 self.buffer_size)
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
398 self.__buffer = self.__buffer2
72ef28ba79b1 logfile: bugfixes and improved handling of some special cases
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1855
diff changeset
399 self.__rel_index = 0
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
400 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
401 # load buffers around position
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
402 self.__buffer1 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
403 position,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
404 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
405 forward=False)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
406 self.__buffer2 = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
407 position,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
408 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
409 self.__buffer = self.__buffer2
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
410 self.__rel_index = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
411 # XXX test for valid position
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
412 self.__lineno = line_no
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
413
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
414 def line_no(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
415 """@return: the current line number or None if line number is unknown"""
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
416 return self.__lineno
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
417
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
418 def calculate_line_no(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
419 """ Calculate the current line number from buffer offsets
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
420
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
421 If line number is unknown it is calculated by parsing the whole file.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
422 This may be expensive.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
423 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
424 self._input.seek(0, 0)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
425 lines = self._input.read(self.__buffer.offsets[self.__rel_index])
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
426 self.__lineno = len(lines.splitlines())
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
427 return self.__lineno
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
428
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
429 def parser(self, line):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
430 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
431 @param line: line as read from file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
432 @return: parsed line or None on error
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
433 Converts the line from file to program representation
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
434 This implementation uses TAB separated strings.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
435 This method should be overwritten by the sub classes.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
436 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
437 return line.split("\t")
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
438
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
439 def add(self, *data):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
440 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
441 add line to log file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
442 This implementation save the values as TAB separated strings.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
443 This method should be overwritten by the sub classes.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
444 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
445 line = "\t".join(data)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
446 self._add(line)
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
447
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
448 def _add(self, line):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
449 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
450 @param line: flat line
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
451 @type line: String
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
452 write on entry in the log file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
453 """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
454 if line is not None:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
455 if line[-1] != '\n':
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
456 line += '\n'
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
457 self._output.write(line)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
458