Revision 1710
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/streams.py | ||
---|---|---|
41 | 41 |
def readline(self): |
42 | 42 |
return timeouts.run(lambda: self.stream.readline(), self.timeout) |
43 | 43 |
|
44 |
##### Traced streams
|
|
44 |
##### Filtered/traced streams
|
|
45 | 45 |
|
46 |
class TracedStream(WrapStream): |
|
47 |
'''Wraps a stream, running a trace function on each string read or written |
|
48 |
''' |
|
49 |
def __init__(self, trace, stream): |
|
46 |
class FilterStream(WrapStream): |
|
47 |
'''Wraps a stream, filtering each string read or written''' |
|
48 |
def __init__(self, filter_, stream): |
|
50 | 49 |
WrapStream.__init__(self, stream) |
51 |
self.trace = trace
|
|
50 |
self.filter = filter_
|
|
52 | 51 |
|
53 |
def readline(self): |
|
54 |
str_ = self.stream.readline() |
|
55 |
self.trace(str_) |
|
56 |
return str_ |
|
52 |
def readline(self): return self.filter(self.stream.readline()) |
|
57 | 53 |
|
58 |
def write(self, str_): |
|
59 |
self.trace(str_) |
|
60 |
return self.stream.write(str_) |
|
54 |
def write(self, str_): return self.stream.write(self.filter(str_)) |
|
61 | 55 |
|
56 |
class TracedStream(FilterStream): |
|
57 |
'''Wraps a stream, running a trace function on each string read or written |
|
58 |
''' |
|
59 |
def __init__(self, trace, stream): |
|
60 |
def filter_(str_): |
|
61 |
trace(str_) |
|
62 |
return str_ |
|
63 |
FilterStream.__init__(self, filter_, stream) |
|
64 |
|
|
62 | 65 |
class LineCountStream(TracedStream): |
63 | 66 |
'''Wraps a unidirectional stream, making the current line number available. |
64 | 67 |
Lines start counting from 1.''' |
Also available in: Unified diff
streams.py: Added FilterStream. Changed TracedStream to use FilterStream.