-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSeqFileIO.py
More file actions
executable file
·288 lines (221 loc) · 8.36 KB
/
SeqFileIO.py
File metadata and controls
executable file
·288 lines (221 loc) · 8.36 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
"""
I/O routines for reading column-oriented and text delimitted data files
"""
import sys
DEF_DELIM = ""
DEF_SKIP_PREF = ""
DEF_MIN_WORDS = 0
DEF_PATH = "/dev/null"
DEF_STRIP = False
USE_STDIN = "-"
def pair_list_to_dictionary(line, start_pos):
"""Transform a list of word pairs to a dictionary."""
__pairs = dict()
__word_list = line.split()
__word_count = len(__word_list)
for __key_pos in range(start_pos - 1, __word_count, 2):
__pairs[__word_list[__key_pos]] = __word_list[__key_pos+1]
return __pairs
class SeqFileIO(object):
"""Utility routines to handle file I/O"""
def __init__(self):
self.lineparts = dict()
self.linewords = 0
self.buff = ""
self.is_open = False
self.min_words = DEF_MIN_WORDS
self.skip_line = DEF_SKIP_PREF
self.raw_lines_read = 0
self.queued_lines = []
self.delim = DEF_DELIM
self.strip = DEF_STRIP
self.debug = False
# For pylint only...
self.pnt_fd = file(DEF_PATH, "r")
def __iter__(self):
"""Standard iterator"""
return self
def next(self):
"""Fetch the next line from the file"""
self.read_line()
return self.buff
def queue_line(self, line):
"""Remember a line of data to be used for the next 'read'"""
self.queued_lines.append(line)
# pylint: disable=R0913
def open_file(self, path=DEF_PATH, min_words=DEF_MIN_WORDS,
skip_line=DEF_SKIP_PREF, delim=DEF_DELIM, strip=DEF_STRIP):
"""Open the specified file and stash away basic status info"""
if path == USE_STDIN:
self.pnt_fd = sys.stdin
self.is_open = True
else:
try:
self.pnt_fd = open(path)
self.is_open = True
except IOError:
self.is_open = False
self.min_words = min_words
self.skip_line = skip_line
self.delim = delim
self.strip = strip
# pylint: enable=R0913
def get_word(self, which):
"""Return the specific word from the current parsed logical record"""
if which < self.linewords:
__word = self.lineparts[which]
else:
__word = ""
return __word
def close_file(self):
"""Used when the caller want to skip the rest of the file."""
if self.is_open:
self.is_open = False
self.pnt_fd.close()
return
def read_line(self):
"""Read/Parse the next line in the open file."""
self.lineparts = dict()
self.linewords = 0
self.buff = ""
if not self.is_open:
if self.debug:
print "dbg:: SeqFileIO: read_line(): File already closed"
raise StopIteration
else:
if len(self.queued_lines) > 0:
if self.debug:
print "dbg:: SeqFileIO: read_line(): Pop line off queue"
__raw_buff = self.queued_lines.pop(0)
self.buff = __raw_buff
else:
try:
__raw_buff = self.pnt_fd.readline()
self.buff = __raw_buff
if self.strip and self.buff[-1:] == "\n":
self.buff = self.buff[:-1]
self.raw_lines_read += 1
if self.debug:
print "dbg:: SeqFileIO: read_line(): Read #{nl} {ll} \
bytes from file".format(nl=self.raw_lines_read, ll=len(self.buff))
except IOError:
if self.debug:
print "dbg:: SeqFileIO: read_line(): I/O error on \
read, wrap up."
self.pnt_fd.close()
self.is_open = False
raise StopIteration
try:
__min_words = self.min_words
except AttributeError:
__min_words = 0
try:
__skip_line = self.skip_line
except AttributeError:
__skip_line = ""
if __raw_buff == "":
if self.debug:
print "dbg:: SeqFileIO: read_line(): Got blank line, \
assuming EOF"
self.pnt_fd.close()
self.is_open = False
raise StopIteration
else:
if self.delim != "":
self.lineparts = self.buff.split(self.delim)
else:
self.lineparts = self.buff.split()
self.linewords = len(self.lineparts)
if self.linewords < __min_words:
if self.debug:
print "dbg:: SeqFileIO: read_line(): Skip, \
wanted >= {mw} words, got {lw}".format(mw=__min_words, lw=self.linewords)
self.read_line()
elif __skip_line != "":
if self.lineparts[0] == __skip_line:
if self.debug:
print "dbg:: SeqFileIO: read_line(): Skip, \
wanted >= {mw} words, got {lw}".format(mw=__min_words, lw=self.linewords)
self.read_line()
elif self.debug:
print "dbg:: SeqFileIO: read_line(): Keep line, found \
{lw} words".format(lw=self.linewords)
return self.is_open
def read_all_lines(self):
"""Read all the lines in the file and return them all at once"""
__lines = ()
if not self.is_open:
raise StopIteration
if self.is_open:
__lines = self.pnt_fd.readlines()
try:
__skip_pref = self.skip_line
except AttributeError:
__skip_pref = ""
if __lines != "":
__skip_pref_len = len(__skip_pref) + 1
for __off in range(len(__lines)-1, -1, -1):
if __lines[__off][-1:] == "\n":
__lines[__off] = __lines[__off][:-1]
if __skip_pref_len > 1:
if __lines[__off].startswith(__skip_pref):
__lines[__off:__off+1] = []
elif __lines[__off] == "":
__lines[__off:__off+1] = []
self.is_open = False
return __lines
def read_labelled_pair_list_file(self, handler, label_set):
"""
Parse files formatted such that each pair of lines in the file
is a set of variable names (line 1) and associated values
(line 2)
"""
if not self.is_open:
raise StopIteration
try:
__result = set()
__unknown_label = set()
while self.read_line():
__sock_type = self.lineparts[0]
if __sock_type in label_set:
__result.add(__sock_type)
handler.field[__sock_type] = pair_list_to_dictionary(
self.buff, 2)
else:
__unknown_label.add(__sock_type)
except StopIteration:
self.is_open = False
return(__result, __unknown_label)
def read_twoline_logical_record(self, handler, prot_field):
"""
Read and parse files with two-physical line per logical line format
"""
handler.field = dict()
self.lineparts = dict()
self.linewords = 0
if not self.is_open:
raise StopIteration
try:
self.read_line()
__pss_varlist = self.lineparts
__pss_varcount = self.linewords
self.read_line()
if self.linewords != __pss_varcount:
self.lineparts = dict()
self.linewords = 0
raise StopIteration
else:
handler.field[prot_field] = self.lineparts[0][0:-1]
handler.hit_order = dict()
for __varnum in range(0, self.linewords, 1):
__field_name = __pss_varlist[__varnum]
handler.field[__field_name] = self.lineparts[__varnum]
handler.hit_order[__varnum] = __field_name
except StopIteration:
self.lineparts = dict()
self.linewords = 0
self.is_open = False
return
if __name__ == "__main__":
print "A library providing access to text files via Python iterators"