Project

General

Profile

1 1630 aaronmk
# I/O
2
3 1673 aaronmk
import timeouts
4 1635 aaronmk
5 1759 aaronmk
##### Wrapped streams
6
7 1635 aaronmk
class WrapStream:
8
    '''Forwards close() to the underlying stream'''
9
    def __init__(self, stream): self.stream = stream
10
11
    def close(self): self.stream.close()
12
13 1759 aaronmk
##### Line iteration
14
15 1635 aaronmk
class StreamIter(WrapStream):
16 1630 aaronmk
    '''Iterates over the lines in a stream.
17
    Unlike stream.__iter__(), doesn't wait for EOF to start returning lines.
18
    Offers curr() method.
19
    '''
20
    def __init__(self, stream):
21 1635 aaronmk
        WrapStream.__init__(self, stream)
22 1630 aaronmk
        self.line = None
23
24
    def __iter__(self): return self
25
26
    def curr(self):
27
        if self.line == None: self.line = self.stream.readline()
28
        if self.line == '': raise StopIteration
29
        return self.line
30
31
    def next(self):
32
        line = self.curr()
33
        self.line = None
34
        return line
35 1635 aaronmk
36 1686 aaronmk
def copy(from_, to):
37
    for line in StreamIter(from_): to.write(line)
38
39 1759 aaronmk
def consume(in_):
40
    for line in StreamIter(in_): pass
41
42
##### Timeouts
43
44 1635 aaronmk
class TimeoutInputStream(WrapStream):
45 1673 aaronmk
    '''@param timeout sec'''
46
    def __init__(self, stream, timeout):
47 1635 aaronmk
        WrapStream.__init__(self, stream)
48 1673 aaronmk
        self.timeout = timeout
49 1630 aaronmk
50 1635 aaronmk
    def readline(self):
51 1673 aaronmk
        return timeouts.run(lambda: self.stream.readline(), self.timeout)
52 1630 aaronmk
53 1710 aaronmk
##### Filtered/traced streams
54 1686 aaronmk
55 1710 aaronmk
class FilterStream(WrapStream):
56
    '''Wraps a stream, filtering each string read or written'''
57
    def __init__(self, filter_, stream):
58 1682 aaronmk
        WrapStream.__init__(self, stream)
59 1710 aaronmk
        self.filter = filter_
60 1630 aaronmk
61 1710 aaronmk
    def readline(self): return self.filter(self.stream.readline())
62 1682 aaronmk
63 1710 aaronmk
    def write(self, str_): return self.stream.write(self.filter(str_))
64 1630 aaronmk
65 1710 aaronmk
class TracedStream(FilterStream):
66
    '''Wraps a stream, running a trace function on each string read or written
67
    '''
68
    def __init__(self, trace, stream):
69
        def filter_(str_):
70
            trace(str_)
71
            return str_
72
        FilterStream.__init__(self, filter_, stream)
73
74 1684 aaronmk
class LineCountStream(TracedStream):
75
    '''Wraps a unidirectional stream, making the current line number available.
76 1630 aaronmk
    Lines start counting from 1.'''
77
    def __init__(self, stream):
78
        self.line_num = 1
79
        def trace(str_): self.line_num += str_.count('\n')
80 1682 aaronmk
        TracedStream.__init__(self, trace, stream)
81
82
class CaptureStream(TracedStream):
83
    '''Wraps a stream, capturing matching text.
84 1706 aaronmk
    Matches can be retrieved from self.matches.'''
85 1682 aaronmk
    def __init__(self, stream, start_str, end_str):
86
        self.recording = False
87 1706 aaronmk
        self.matches = []
88 1682 aaronmk
89
        def trace(str_):
90 1707 aaronmk
            start_idx = 0
91
            if not self.recording:
92
                start_idx = str_.find(start_str)
93
                if start_idx >= 0:
94
                    self.recording = True
95
                    self.matches.append('')
96
            if self.recording:
97
                end_idx = str_.find(end_str)
98
                if end_idx >= 0:
99
                    self.recording = False
100
                    end_idx += len(end_str)
101
                else: end_idx = len(str_)
102
103
                self.matches[-1] += str_[start_idx:end_idx]
104 1682 aaronmk
105
        TracedStream.__init__(self, trace, stream)