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 write(self, str_): return self.stream.write(self.filter(str_))
64

    
65
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
class LineCountStream(TracedStream):
75
    '''Wraps a unidirectional stream, making the current line number available.
76
    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
        TracedStream.__init__(self, trace, stream)
81

    
82
class CaptureStream(TracedStream):
83
    '''Wraps a stream, capturing matching text.
84
    Matches can be retrieved from self.matches.'''
85
    def __init__(self, stream, start_str, end_str):
86
        self.recording = False
87
        self.matches = []
88
        
89
        def trace(str_):
90
            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
        
105
        TracedStream.__init__(self, trace, stream)
(15-15/25)