Project

General

Profile

1
# I/O
2

    
3
import timeouts
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):
12
    '''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
        WrapStream.__init__(self, stream)
18
        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

    
32
def copy(from_, to):
33
    for line in StreamIter(from_): to.write(line)
34

    
35
class TimeoutInputStream(WrapStream):
36
    '''@param timeout sec'''
37
    def __init__(self, stream, timeout):
38
        WrapStream.__init__(self, stream)
39
        self.timeout = timeout
40
    
41
    def readline(self):
42
        return timeouts.run(lambda: self.stream.readline(), self.timeout)
43

    
44
##### Traced streams
45

    
46
class TracedStream(WrapStream):
47
    '''Wraps a stream, running a trace function on each string read or written
48
    '''
49
    def __init__(self, trace, stream):
50
        WrapStream.__init__(self, stream)
51
        self.trace = trace
52
    
53
    def readline(self):
54
        str_ = self.stream.readline()
55
        self.trace(str_)
56
        return str_
57
    
58
    def write(self, str_):
59
        self.trace(str_)
60
        return self.stream.write(str_)
61

    
62
class LineCountStream(TracedStream):
63
    '''Wraps a unidirectional stream, making the current line number available.
64
    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
        TracedStream.__init__(self, trace, stream)
69

    
70
class CaptureStream(TracedStream):
71
    '''Wraps a stream, capturing matching text.
72
    Matches can be retrieved from self.matches.'''
73
    def __init__(self, stream, start_str, end_str):
74
        self.recording = False
75
        self.matches = []
76
        
77
        def trace(str_):
78
            start_idx = str_.find(start_str)
79
            if start_idx >= 0:
80
                self.recording = True
81
                self.matches.append('')
82
            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
            if recording: self.matches[-1] += str_[start_idx:end_idx]
93
        
94
        TracedStream.__init__(self, trace, stream)
(14-14/23)