Project

General

Profile

« Previous | Next » 

Revision 1939

Added ProgressInputStream

View differences:

lib/streams.py
101 101
    
102 102
    def write(self, str_): raise InputStreamsOnlyException()
103 103

  
104
class ProgressInputStream(TracedStream):
105
    '''Wraps an input stream, reporting the # lines read every n lines and after
106
    the last line is read.
107
    @param log The output stream for progress messages
108
    '''
109
    def __init__(self, stream, log, msg='Read %d line(s)', n=100):
110
        self.eof = False
111
        
112
        def trace(str_):
113
            msg_ = msg # may be modified, so can't have same name as outer var
114
            if str_ == None: # closed
115
                if self.eof: return # not closed prematurely
116
                str_ = '' # closed prematurely, so signal EOF
117
                msg_ += ' (not all input read)'
118
            
119
            self.eof = eof = str_ == ''
120
            if eof: line_ending = '\n'
121
            else: line_ending = '\r'
122
            
123
            line_ct = self.stream.line_num - 1 # make it 0-based
124
            if eof or line_ct % n == 0: log.write((msg_ % line_ct)+line_ending)
125
        self.trace = trace
126
        
127
        TracedStream.__init__(self, trace, LineCountInputStream(stream))
128
    
129
    def close(self):
130
        self.trace(None) # signal closed
131
        TracedStream.close(self)
132

  
104 133
class CaptureStream(TracedStream):
105 134
    '''Wraps a stream, capturing matching text.
106 135
    Matches can be retrieved from self.matches.'''

Also available in: Unified diff