Project

General

Profile

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