Project

General

Profile

1
# I/O
2

    
3
import timeouts
4

    
5
##### Wrapped streams
6

    
7
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
##### Line iteration
14

    
15
class StreamIter(WrapStream):
16
    '''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
        WrapStream.__init__(self, stream)
22
        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

    
36
def copy(from_, to):
37
    for line in StreamIter(from_): to.write(line)
38

    
39
def consume(in_):
40
    for line in StreamIter(in_): pass
41

    
42
##### Timeouts
43

    
44
class TimeoutInputStream(WrapStream):
45
    '''@param timeout sec'''
46
    def __init__(self, stream, timeout):
47
        WrapStream.__init__(self, stream)
48
        self.timeout = timeout
49
    
50
    def readline(self):
51
        return timeouts.run(lambda: self.stream.readline(), self.timeout)
52

    
53
##### Filtered/traced streams
54

    
55
class FilterStream(WrapStream):
56
    '''Wraps a stream, filtering each string read or written'''
57
    def __init__(self, filter_, stream):
58
        WrapStream.__init__(self, stream)
59
        self.filter = filter_
60
    
61
    def readline(self): return self.filter(self.stream.readline())
62
    
63
    def read(self, n): return self.readline() # forward all reads to readline()
64
    
65
    def write(self, str_): return self.stream.write(self.filter(str_))
66

    
67
class TracedStream(FilterStream):
68
    '''Wraps a stream, running a trace function on each string read or written
69
    '''
70
    def __init__(self, trace, stream):
71
        def filter_(str_):
72
            trace(str_)
73
            return str_
74
        FilterStream.__init__(self, filter_, stream)
75

    
76
class LineCountStream(TracedStream):
77
    '''Wraps a unidirectional stream, making the current line number available.
78
    Lines start counting from 1.'''
79
    def __init__(self, stream):
80
        self.line_num = 1
81
        def trace(str_): self.line_num += str_.count('\n')
82
        TracedStream.__init__(self, trace, stream)
83

    
84
class CaptureStream(TracedStream):
85
    '''Wraps a stream, capturing matching text.
86
    Matches can be retrieved from self.matches.'''
87
    def __init__(self, stream, start_str, end_str):
88
        self.recording = False
89
        self.matches = []
90
        
91
        def trace(str_):
92
            start_idx = 0
93
            if not self.recording:
94
                start_idx = str_.find(start_str)
95
                if start_idx >= 0:
96
                    self.recording = True
97
                    self.matches.append('')
98
            if self.recording:
99
                end_idx = str_.find(end_str)
100
                if end_idx >= 0:
101
                    self.recording = False
102
                    end_idx += len(end_str)
103
                else: end_idx = len(str_)
104
                
105
                self.matches[-1] += str_[start_idx:end_idx]
106
        
107
        TracedStream.__init__(self, trace, stream)
(19-19/29)