annotate MoinMoin/logfile/__init__.py @ 1000:6f6fcbe200b5

whitespace-only cleanup and minor style changes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 22 Jul 2006 12:47:55 +0200
parents 3dba26fcfde0
children db56bd53fc32
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
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 @copyright: 2005 by Thomas Waldmann (MoinMoin:ThomasWaldmann)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 @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
7 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 from MoinMoin.util import pysupport
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
10
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11 logfiles = pysupport.getPackageModules(__file__)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
13 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
14 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
15
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
16 class LogError(Exception):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
17 """ 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
18
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
19 class LogMissing(LogError):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
20 """ 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
21
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
22
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
23 class LineBuffer:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
24 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
25 Reads lines from a file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
26 self.lines list of lines (Strings)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
27 self.offsets list of offset for each line
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
28 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
29 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
30 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
31 @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
32 @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
33 @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
34 @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
35 @type forward: boolean
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
36 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
37 if forward:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
38 file.seek(offset)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
39 self.lines = file.readlines(size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
40 self.__calculate_offsets(offset)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
41 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
42 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
43 begin = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
44 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
45 begin = offset - size
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
46 file.seek(begin)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
47 self.lines = file.read(offset-begin).splitlines(True)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
48 if begin != 0:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
49 begin += len(self.lines[0])
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
50 self.lines = self.lines[1:]
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
51 # 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
52 self.__calculate_offsets(begin)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
53
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
54 # Decode lines after offset in file is calculated
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
55 self.lines = [unicode(line, config.charset) for line in self.lines]
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
56 self.len = len(self.lines)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
57
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
58 def __calculate_offsets(self, offset):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
59 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
60 @param offset: offset of the first line
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
61 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
62 self.offsets = map(lambda x:len(x), self.lines)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
63 self.offsets.append(0)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
64 i = 1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
65 length = len(self.offsets)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
66 tmp = offset
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
67 while i < length:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
68 result = self.offsets[i-1] + tmp
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
69 tmp = self.offsets[i]
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
70 self.offsets[i] = result
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
71 i = i + 1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
72 self.offsets[0] = offset
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
73
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
74
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
75 class LogFile:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
76 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
77 .filter: function that gets the values from .parser.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
78 must return True to keep it or False to remove it
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
79 Overwrite .parser() and .add() to customize this class to
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
80 special log files
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
81 """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
82
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
83 def __init__(self, filename, buffer_size=65536):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
84 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
85 @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
86 @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
87 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
88 self.buffer_size = buffer_size
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
89 self.__filename = filename
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
90 self.filter = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
91 self.__lineno = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
92 self.__buffer = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
93 self.__buffer1 = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
94 self.__buffer2 = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
95
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
96 def __iter__(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
97 return self
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
98
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
99 def reverse(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
100 """ @rtype: iterator
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 self.to_end()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
103 while 1:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
104 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
105 result = self.previous()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
106 except StopIteration:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
107 return
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
108 yield result
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
109
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
110 def sanityCheck(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
111 """ 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
112
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
113 TODO: os.access should not be used here.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
114
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
115 @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
116 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
117 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
118 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
119 return None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
120
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
121 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
122 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
123 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
124 """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
125 if name == "_LogFile__rel_index":
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
126 # starting iteration from begin
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
127 self.__buffer1 = LineBuffer(self._input, 0, self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
128 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
129 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
130 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
131 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
132 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
133 return 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
134 elif name == "_input":
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
135 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
136 # Open the file without codecs.open, it break our offset
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
137 # calculation. We decode it later.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
138 # Use binary mode in order to retain \r. Otherwise the offset
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
139 # calculation would fail
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
140 self._input = file(self.__filename, "rb",)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
141 except IOError:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
142 raise StopIteration
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
143 return self._input
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
144 elif name == "_output":
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
145 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
146 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
147 os.chmod(self.__filename, 0666 & config.umask)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
148 except OSError:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
149 # TODO: should not ignore errors like this!
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
150 pass
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
151 return self._output
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
152 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
153 raise AttributeError(name)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
154
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
155 def size(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
156 """ 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
157
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
158 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
159
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
160 @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
161 @rtype: Int
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
162 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
163 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
164 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
165 except OSError, err:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
166 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
167 return 0
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
168 raise
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 def lines(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
171 """ 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
172
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
173 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
174
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
175 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
176
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
177 @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
178 @rtype: Int
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 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
181 f = codecs.open(self.__filename, 'r')
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
182 try:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
183 count = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
184 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
185 count += 1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
186 return count
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
187 finally:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
188 f.close()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
189 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
190 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
191 return 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
192 raise
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
193
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
194 def date(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
195 """ 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
196 try:
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
197 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
198 except OSError, err:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
199 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
200 # 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
201 # 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
202 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
203 raise
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
204 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
205
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
206 def peek(self, lines):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
207 """ What does this method do?
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
208
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
209 @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
210 moves file position by lines.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
211 @return: True if moving more than (WHAT?) to the beginning and moving
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
212 to the end or beyond
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
213 @rtype: boolean
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
214 peek adjusts .__lineno if set
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
215 This function is not aware of filters!
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
216 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
217 self.__rel_index = self.__rel_index + lines
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
218 while 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
219 if 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
220 # change to buffer 1
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
221 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
222 self.__rel_index = self.__rel_index + self.__buffer.len
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
223 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
224 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
225 # 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
226 # XXX
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
227 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
228 self.__lineno = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
229 return True
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
230 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
231 # load previous lines
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
232 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
233 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
234 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
235 self.buffer_size,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
236 forward=False)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
237 self.__rel_index = (self.__rel_index +
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
238 self.__buffer1.len)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
239 self.__buffer = self.__buffer1
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
240
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
241 while self.__rel_index >= self.__buffer.len:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
242 if 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
243 # change to buffer 2
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
244 self.__rel_index = self.__rel_index - self.__buffer.len
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
245 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
246 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
247 # 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
248 tmpbuff = LineBuffer(self._input,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
249 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
250 self.buffer_size)
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
251 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
252 # end of file
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
253 if self.__lineno:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
254 self.__lineno = (self.__lineno + lines -
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
255 (self.__rel_index -
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
256 len(self.__buffer.offsets)))
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
257 self.__rel_index = len(self.__buffer.offsets)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
258 return True
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
259 # shift buffers
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
260 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
261 self.__buffer2 = tmpbuff
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
262 self.__rel_index = self.__rel_index - self.__buffer1.len
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
263 if self.__lineno: self.__lineno += lines
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
264 return False
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
265
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
266 def __next(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
267 """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
268 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
269 raise StopIteration
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
270 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
271 self.peek(1)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
272 return result
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
273
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
274 def next(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
275 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
276 @return: next entry
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
277 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
278 XXX It does not raise anything!
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
279 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
280 result = None
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
281 while result is None:
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
282 while result is None:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
283 result = self.__next()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
284 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
285 result = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
286 return result
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
287
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
288 def __previous(self):
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
289 if self.peek(-1):
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
290 raise StopIteration
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
291 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
292
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
293 def previous(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
294 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
295 @return: previous entry and moves file position one line back
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
296 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
297 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
298 result = None
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
299 while result is None:
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
300 while result is None:
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
301 result = self.__previous()
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
302 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
303 result = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
304 return result
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
305
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
306 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
307 """moves file position to the begin"""
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
308 if self.__buffer1.offsets[0] != 0:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
309 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
310 0,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
311 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
312 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
313 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
314 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
315 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
316 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
317 self.__lineno = 0
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
318
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
319 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
320 """moves file position to the end"""
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
321 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
322 size = self._input.tell()
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
323 if (not self.__buffer2) 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
324 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
325 size,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
326 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
327 forward=False)
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
328
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 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
331 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
332 forward=False)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
333 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
334 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
335 self.__lineno = None
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
336
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
337 def position(self):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
338 """ 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
339
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
340 This can be converted into a String using back-ticks and then
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
341 be rebuild.
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
342 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
343 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
344 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
345
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
346 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
347 """ 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
348 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
349 .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
350 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
351 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
352 if self.__buffer1.offsets[0] <= position < 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
353 # 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
354 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
355 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
356 elif (self.__buffer2.offsets[0] <= position <
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
357 self.__buffer2.offsets[-1]):
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
358 # 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
359 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
360 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
361 else:
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
362 # 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
363 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
364 position,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
365 self.buffer_size,
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
366 forward=False)
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
367 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
368 position,
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
369 self.buffer_size)
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
370 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
371 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
372 # 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
373 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
374
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
375 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
376 """@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
377 return self.__lineno
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
378
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
379 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
380 """ 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
381
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
382 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
383 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
384 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
385 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
386 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
387 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
388 return self.__lineno
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
389
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
390 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
391 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
392 @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
393 @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
394 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
395 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
396 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
397 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
398 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
399
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
400 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
401 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
402 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
403 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
404 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
405 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
406 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
407 self._add(line)
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
408
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
409 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
410 """
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
411 @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
412 @type line: String
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
413 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
414 """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
415 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
416 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
417 line += '\n'
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 0
diff changeset
418 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
419