Revision 1928
Added by Aaron Marcuse-Kubitza about 12 years ago
lib/streams.py | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import timeouts |
4 | 4 |
|
5 |
##### Exceptions |
|
6 |
|
|
7 |
class InputStreamsOnlyException(Exception): |
|
8 |
def __init__(self): Exception.__init__(self, 'Only input streams allowed') |
|
9 |
|
|
5 | 10 |
##### Wrapped streams |
6 | 11 |
|
7 | 12 |
class WrapStream: |
... | ... | |
49 | 54 |
|
50 | 55 |
def readline(self): |
51 | 56 |
return timeouts.run(lambda: self.stream.readline(), self.timeout) |
57 |
|
|
58 |
def write(self, str_): raise InputStreamsOnlyException() |
|
52 | 59 |
|
53 | 60 |
##### Filtered/traced streams |
54 | 61 |
|
... | ... | |
81 | 88 |
def trace(str_): self.line_num += str_.count('\n') |
82 | 89 |
TracedStream.__init__(self, trace, stream) |
83 | 90 |
|
91 |
class LineCountInputStream(TracedStream): |
|
92 |
'''Wraps an input stream, making the current line number available. |
|
93 |
Lines start counting from 1. |
|
94 |
This is faster than LineCountStream for input streams, because all reading |
|
95 |
is done through readline() so newlines don't need to be explicitly counted. |
|
96 |
''' |
|
97 |
def __init__(self, stream): |
|
98 |
self.line_num = 1 |
|
99 |
def trace(str_): self.line_num += 1 |
|
100 |
TracedStream.__init__(self, trace, stream) |
|
101 |
|
|
102 |
def write(self, str_): raise InputStreamsOnlyException() |
|
103 |
|
|
84 | 104 |
class CaptureStream(TracedStream): |
85 | 105 |
'''Wraps a stream, capturing matching text. |
86 | 106 |
Matches can be retrieved from self.matches.''' |
Also available in: Unified diff
streams.py: Added LineCountInputStream, which is faster than LineCountStream for input streams. Added InputStreamsOnlyException and raise it in all *InputStream classes' write() methods.