-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSourceIO.py
More file actions
233 lines (172 loc) · 5.42 KB
/
SourceIO.py
File metadata and controls
233 lines (172 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import io
import os
import re
from StringIO import StringIO
from collections import deque
class SourceIO(object):
"""Base class for scanning string/textfile source with lookback support.
Also provides a stream based output mechanism with an output lookback buffer"""
def __init__(self, source, output):
super(SourceIO, self).__init__()
source_type = type(source)
self.re_whitespace = re.compile(r"^\s+$")
if(source_type == str or source_type == unicode):
self.input_stream = StringInput(source)
elif(source_type == file):
self.input_stream = FileInput(source)
else:
raise ValueError("'source' arument must be a string/unicode or file handle")
self.output_stream = output
self.lbb_out = LookbackBuffer()
self.lbb_in = LookbackBuffer()
self.written = 0
def read(self):
"""read a single character from the input stream"""
if(not self.input_stream.ready()):
return None
else:
c = self.input_stream.read()
self.lbb_in.write(c) # add the character to the input stream lookback buffer
return c
def ch(self):
"""return the current/last character read from the input stream"""
return self.input_stream.ch()
def can_read(self):
"""returns True if the input stream has remaining characters available to be read, else False"""
return self.input_stream.ready()
def write(self, s):
"""write a string to the output stream and update the output lookback-buffer"""
self.written += len(s) # increment the throughput counter
self.lbb_out.write(s) # add character sequence to the output lookback buffer
self.output_stream.write(s)
def clear_trailing_whitespace(self):
"""clear all whitespace characters from the tail of the output stream's current position"""
pre_pos = self.output_stream.tell()
back = 1
while(self.re_whitespace.match(self.lbb_out.peek_back(back))):
back += 1
if(back > 1):
self.lbb_out.pop(back) #truncate the lookback buffer to keep it accurate
self.output_stream.truncate(self.output_stream.tell() - back + 1) # get rid of the whitespace
self.output_stream.seek(pre_pos - back + 1) # reset the output stream to the correct position
def flush_output(self):
"""flush the output and then call fsync if possible"""
self.output_stream.flush()
if(type(self.output_stream) == file):
os.fsync(self.output_stream.fileno())
def close_input(self):
"""closes the input stream"""
self.input_stream.close()
def close_output(self):
self.output_stream.close()
def close(self):
self.close_input()
self.close_output()
class InputSource(object):
"""base text input class definition/interface"""
def __init__(self):
super(InputSource, self).__init__()
def read(self):
"""read a single character from the input source"""
raise NotImplementerError()
def ch(self):
"""return the current character/last character read"""
raise NotImplementerError()
def peek(self, amount=1):
"""peek forward in the input stream by X amount"""
raise NotImplementerError()
def ready(self):
"""input stream has remaining characters to read"""
raise NotImplementerError()
def close(self):
"""close the input stream"""
pass
class StringInput(InputSource):
"""input class for in memory strings"""
def __init__(self, source, max_look_behind=50):
super(StringInput, self).__init__()
self.source = source
self.source_length = len(source)
self.pos = -1
def read(self):
if(not self.ready()):
return None
self.pos += 1
next = self.source[self.pos]
return next
def ch(self):
return self.source[self.pos]
def peek(self, amount=1):
if(not self.ready()):
return None
return self.source[self.pos+1:self.pos+amount+1]
def ready(self):
return self.pos < self.source_length-1
def close(self):
pass
class FileInput(InputSource):
"""input class for file input sources"""
def __init__(self, input_stream, max_look_behind=50):
super(FileInput, self).__init__()
self.source = input_stream
self.source_length = os.fstat(input_stream.fileno()).st_size
self.pos = -1
self.cur_char = None
def read(self):
if(not self.ready()):
return None
self.cur_char = self.source.read(1)
return self.cur_char
def ch(self):
return self.cur_char
def peek(self, amount=1):
if(not self.ready()):
return None
start_pos = self.source.tell()
peek = self.source.read(amount)
self.source.seek(start_pos)
return peek
def ready(self):
return self.source.tell() < self.source_length
def close(self):
self.source.close()
class LookbackBuffer(object):
"""lookback buffer"""
def __init__(self, src="", max_len = 5000):
super(LookbackBuffer, self).__init__()
self.max_len = max_len
l = len(src)
if(l > self.max_len):
src = src[l-max_len:]
self.q = deque(list(src))
self.history_size = len(self.q)
def write(self, s):
for char in s:
self.write_char(char)
def write_char(self, s):
self.history_size = self.history_size + 1
if(self.size() == self.max_len):
self.q.popleft()
self.q.append(s)
def to_string(self):
return "".join(self.q)
def lookback(self, s, back=None):
if(back == None):
back = len(s)
peek = self.peek_back(back)
return s == peek
def peek_back(self, back=1):
if(back < 1):
raise ValueError("back value must be >= 1")
s = self.size()
if(s < back):
back = s
return "".join(list(self.q)[-back:])
def pop(self, back):
for x in range(0,back):
self.q.pop()
self.history_size -= 1
def size(self):
return len(self.q)
def throughput(self):
return self.history_size