forked from ossobv/vcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsvreader.py
284 lines (231 loc) · 8.42 KB
/
wsvreader.py
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
# vim: set ts=8 sw=4 sts=4 et ai tw=79:
# WJD/2013
# WSV = Whitespace Separated Values
# TODO: add docs here
# TODO: replace "extra0" column name with "_columnN" where N is the Nth
# column. that makes more sense.
import re
import unittest
class WsvReader(object):
'''
Whitespace-separated values reader.
Usage::
file = StringIO(
'# This is a comment\\n'
'column_name1 column_name2 optional_column\\n'
'data1a data1b\\n'
'"data2a" "data2b" "data2c"\\n'
'#"data3a" "data3b" "data23" commented out column\\n'
'"" "column1 is empty, column2 has spaces and """\\n'
'data4a data4b data4c data4d "column_names expand as necessary"\\n'
)
reader = WsvReader(file)
for row in reader:
print row
# Yields this:
# {'column_name1': 'data1a',
# 'column_name2': 'data1b'}
# {'column_name1': 'data2a',
# 'column_name2': 'data2b',
# 'optional_column': 'data2c'}
# {'column_name1': '',
# 'column_name2': 'column1 is empty, column2 has spaces and "'}
# {'column_name1': 'data4a',
# 'column_name2': 'data4b',
# 'optional_column': 'data4c',
# 'extra0': 'data4d',
# 'extra1': 'column_names expand as necessary'}
'''
def __init__(self, file, dict=dict):
self.file = file
self.need_seek = False
self.dict = dict
self.findre = re.compile(r'\s([^"\s]*|"([^"]|"")*")\s')
def __iter__(self):
if self.need_seek:
# Don't seek the first time. If we did, we couldn't read
# unbuffered pipes.
self.file.seek(0)
else:
self.need_seek = True
self.fileiter = iter(self.file)
self.read_header()
return self
def next(self):
columns = self.split_line(self.get_line())
# Ensure that the header is long enough.
i = 0
while len(columns) > len(self.columnnames):
extra_columnname = 'extra%d' % (i,)
if extra_columnname not in self.columnnames:
self.columnnames.append(extra_columnname)
i += 1
return self.dict(zip(self.columnnames, columns))
def read_header(self):
self.columnnames = self.split_line(self.get_line())
if len(self.columnnames) != len(set(self.columnnames)):
raise ValueError('duplicate column names!')
def get_line(self):
# Fetch a new line. Skip all blank lines and comments.
for line in self.fileiter:
line = line.strip()
if not line:
continue
if line[0] == '#':
continue
return line
raise StopIteration()
def unquote(self, column):
# The column is never empty. Empty values should be enclosed
# in double quotes.
if column[0] == '"':
assert column[-1] == '"'
column = column[1:-1].replace('""', '"')
return column
def split_line(self, line):
# We add space to both ends as sentinels.
line = ' ' + line + ' '
# Find the columns.
columns = []
pos = 0
while True:
match = self.findre.search(line, pos=pos)
if not match:
break
value = match.groups()[0]
if value:
columns.append(self.unquote(value))
pos = match.end() - 1
return columns
class TestCase(unittest.TestCase):
def test_init_0(self):
"Require a single argument."
self.assertRaises(TypeError, WsvReader)
def test_init_1(self):
"The most basic single argument example."
from collections import OrderedDict
file = self.get_file('col1 col2\ndata1 data2\n')
# Pass only file, we get a regular dict as default.
reader = WsvReader(file)
data = [i for i in reader]
self.assertEquals(data, [{'col1': 'data1', 'col2': 'data2'}])
self.assertTrue(isinstance(data[0], dict))
self.assertFalse(isinstance(data[0], OrderedDict))
def test_init_2(self):
"Supply a different dictionary type."
from collections import OrderedDict
file = self.get_file('col1 col2\ndata1 data2\n')
# Pass file and dictionary type.
reader = WsvReader(file, dict=OrderedDict)
data = [i for i in reader]
self.assertEquals(data, [{'col1': 'data1', 'col2': 'data2'}])
self.assertTrue(isinstance(data[0], OrderedDict))
def test_reiteration(self):
"Iterate over the reader more than once."
file = self.get_file('col1 col2\ndata1 data2\n')
reader = WsvReader(file)
data = [i for i in reader]
self.assertEquals(data, [{'col1': 'data1', 'col2': 'data2'}])
data2 = [i for i in reader]
self.assertEquals(data, data2)
def test_illegal_seek(self):
"It works for the first run over a non-seekable file."
file = self.get_file('col1 col2\ndata1 data2\n', seekable=False)
reader = WsvReader(file)
data = [i for i in reader]
self.assertEquals(data, [{'col1': 'data1', 'col2': 'data2'}])
try:
data2 = [i for i in reader]
except IOError:
pass
else:
self.assertFalse('expected an Illegal seek, got %r' % (data2,))
def test_spaces(self):
"Spaces do not matter."
self.compare_output(
'''
1st\t2nd 3rd
data1 data2 \tdata3\t
more data ok
''',
[{'1st': 'data1', '2nd': 'data2', '3rd': 'data3'},
{'1st': 'more', '2nd': 'data', '3rd': 'ok'}]
)
def test_blanks_and_comments(self):
"Blanks and comments are ignored."
self.compare_output(
'''
1st 2nd 3rd
# this is a comment
more #data #ok
"#more" data #ok
''',
[{'1st': 'more', '2nd': '#data', '3rd': '#ok'},
{'1st': '#more', '2nd': 'data', '3rd': '#ok'}]
)
def test_double_quotes(self):
"Double quotes can be used if values need spaces."
self.compare_output(
'''
1st "2nd column" 3rd
"" "data1 data2" "data3"
"""special"" data" is ok
''',
[{'1st': '', '2nd column': 'data1 data2', '3rd': 'data3'},
{'1st': '"special" data', '2nd column': 'is', '3rd': 'ok'}]
)
def test_too_few_values(self):
"Missing columns aren't added in the dict."
self.compare_output(
'''
1st "2nd column" 3rd
only_first
first "and second"
''',
[{'1st': 'only_first'},
{'1st': 'first', '2nd column': 'and second'}]
)
def test_too_many_values(self):
"Extra columns are added as needed."
self.compare_output(
'''
col1 extra1 col3
d1 d2 d3
d1 d2 d3 d4 d5 d6
d1 d2 d3 d4
''',
[{'col1': 'd1', 'extra1': 'd2', 'col3': 'd3'},
{'col1': 'd1', 'extra1': 'd2', 'col3': 'd3',
'extra0': 'd4', 'extra2': 'd5', 'extra3': 'd6'},
{'col1': 'd1', 'extra1': 'd2', 'col3': 'd3',
'extra0': 'd4'}]
)
def get_file(self, string, seekable=True):
from StringIO import StringIO
if seekable:
return StringIO(string)
class NoSeekStringIO(StringIO):
def seek(self, *args):
raise IOError(29, 'Illegal seek')
return NoSeekStringIO(string)
def compare_output(self, string, dict_list):
file = self.get_file(string)
reader = WsvReader(file)
data = [i for i in reader]
return self.assertEquals(data, dict_list)
if __name__ == '__main__':
from collections import OrderedDict
import sys
if len(sys.argv) == 1:
files = [sys.stdin]
elif sys.argv[1:] == ['TestCase']:
unittest.main()
else:
files = [open(filename) for filename in sys.argv[1:]]
for file in files:
reader = WsvReader(file, dict=OrderedDict)
if len(files) > 1:
print '\n==> %s' % (file.name,)
for row in reader:
print row
file.close()