Revision 1635
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/streams.py | ||
---|---|---|
1 | 1 |
# I/O |
2 | 2 |
|
3 |
class StreamIter: |
|
3 |
import timeout |
|
4 |
|
|
5 |
class WrapStream: |
|
6 |
'''Forwards close() to the underlying stream''' |
|
7 |
def __init__(self, stream): self.stream = stream |
|
8 |
|
|
9 |
def close(self): self.stream.close() |
|
10 |
|
|
11 |
class StreamIter(WrapStream): |
|
4 | 12 |
'''Iterates over the lines in a stream. |
5 | 13 |
Unlike stream.__iter__(), doesn't wait for EOF to start returning lines. |
6 | 14 |
Offers curr() method. |
7 |
Forwards close() to the underlying stream. |
|
8 | 15 |
''' |
9 | 16 |
def __init__(self, stream): |
10 |
self.stream = stream
|
|
17 |
WrapStream.__init__(self, stream)
|
|
11 | 18 |
self.line = None |
12 | 19 |
|
13 | 20 |
def __iter__(self): return self |
... | ... | |
21 | 28 |
line = self.curr() |
22 | 29 |
self.line = None |
23 | 30 |
return line |
31 |
|
|
32 |
class TimeoutInputStream(WrapStream): |
|
33 |
'''@param timeout_ sec''' |
|
34 |
def __init__(self, stream, timeout_): |
|
35 |
WrapStream.__init__(self, stream) |
|
36 |
self.timeout = timeout_ |
|
24 | 37 |
|
25 |
def close(self): self.stream.close() |
|
38 |
def readline(self): |
|
39 |
return timeout.run(lambda: self.stream.readline(), self.timeout) |
|
26 | 40 |
|
27 | 41 |
class TracedOutputStream: |
28 | 42 |
'''Wraps an output stream, running a trace function on each string written |
Also available in: Unified diff
streams.py: Added TimeoutInputStream and WrapStream. Changed StreamIter to use new WrapStream.