-
Notifications
You must be signed in to change notification settings - Fork 4
/
pcaxis.py
283 lines (246 loc) · 9.64 KB
/
pcaxis.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
"""
usage:
from pcaxis import axis_parser
axis_parser.parseString('...')
axis_parser.parseFile(open(fn))
or run with python axis.py <dataset>
"""
import codecs
import pyparsing
from pyparsing import Keyword, Word, alphas, nums, alphanums, Regex, Literal, OneOrMore, Suppress, Group, Dict, Optional, White
NEWLINE = Literal("\n") | Literal("\r\n")
EQUAL = Suppress(Literal('='))
QUOTE = Suppress(Literal('"'))
SEMI = Suppress(Literal(";"))
BPAREN = Suppress(Literal("("))
EPAREN = Suppress(Literal(")"))
COMMA = Suppress(Literal(","))
YES = Literal("YES")
NO = Literal("NO")
INTEGER = Word(nums+'-', nums)
INTEGER.setParseAction(lambda x: int(x[0]))
FLOAT = Regex('-?[\d\.]+')
FLOAT.setParseAction(lambda x: float(x[0]))
NONQUOTE = Regex('[^"]+')
QSTRING = QUOTE + NONQUOTE + QUOTE # quoted string
MLQSTRING = OneOrMore(QSTRING | NEWLINE) # multiline quoted string
DATE = QUOTE + Word(nums + ': ', exact=14) + QUOTE
BQ = EQUAL + QUOTE
EQ = QUOTE + SEMI
BPQ = BPAREN + QUOTE
EPQ = QUOTE + EPAREN
tlist = Keyword("TLIST") + Group(BPAREN + Word("AHQMW1") + ((COMMA + QSTRING + Literal("-") + QSTRING + EPAREN) | (EPAREN + OneOrMore(QSTRING | COMMA))))
axis_version = Keyword("AXIS-VERSION") + BQ + Word(nums) + EQ
charset = Keyword("CHARSET") + BQ + Literal("ANSI") + EQ
codes = Keyword("CODES") + BPAREN + QSTRING + EPAREN + EQUAL + Group(OneOrMore(QSTRING | NEWLINE | COMMA)) + SEMI
contact = Keyword("CONTACT") + Optional(BPAREN + QSTRING + EPAREN) + EQUAL + MLQSTRING + SEMI
contents = Keyword("CONTENTS") + EQUAL + QSTRING + SEMI
copyright = Keyword("COPYRIGHT") + EQUAL + (YES | NO) + SEMI
creation_date = Keyword("CREATION-DATE") + EQUAL + DATE + SEMI
DATA_VALUES = Regex('[^;]+')
#DATA_VALUES.setParseAction(parseData)
data = Keyword("DATA") + EQUAL + Optional(Suppress(White())) + DATA_VALUES + SEMI
database = Keyword("DATABASE") + EQUAL + QSTRING + SEMI
datanotesum = Keyword("DATANOTESUM") + EQUAL + QSTRING + SEMI
datasymbol1 = Keyword("DATASYMBOL1") + EQUAL + QSTRING + SEMI
datasymbol2 = Keyword("DATASYMBOL2") + EQUAL + QSTRING + SEMI
datasymbol3 = Keyword("DATASYMBOL3") + EQUAL + QSTRING + SEMI
datasymbol4 = Keyword("DATASYMBOL4") + EQUAL + QSTRING + SEMI
datasymbol5 = Keyword("DATASYMBOL5") + EQUAL + QSTRING + SEMI
datasymbol6 = Keyword("DATASYMBOL6") + EQUAL + QSTRING + SEMI
datasymbolsum = Keyword("DATASYMBOLSUM") + EQUAL + QSTRING + SEMI
datasymbolnil = Keyword("DATASYMBOLNIL") + EQUAL + QSTRING + SEMI
decimals = Keyword("DECIMALS") + EQUAL + INTEGER + SEMI
description = Keyword("DESCRIPTION") + EQUAL + QSTRING + SEMI
descriptiondefault = Keyword("DESCRIPTIONDEFAULT") + EQUAL + YES + SEMI
directory_path = Keyword("DIRECTORY-PATH") + EQUAL + QSTRING + SEMI
domain = Keyword("DOMAIN") + BPAREN + QSTRING + EPAREN + EQUAL + QSTRING + SEMI
elimination = Keyword("ELIMINATION") + BPAREN + QSTRING + EPAREN + EQUAL + (QSTRING | YES) + SEMI
euro = Keyword("EURO") + EQUAL + QSTRING + SEMI
heading = Keyword("HEADING") + EQUAL + OneOrMore(QSTRING | COMMA) + SEMI
infofile = Keyword("INFOFILE") + EQUAL + QSTRING + SEMI
language = Keyword("LANGUAGE") + BQ + Word(alphas, exact=2) + EQ
last_updated = Keyword("LAST-UPDATED") + EQUAL + DATE + SEMI
matrix = Keyword("MATRIX") + EQUAL + QSTRING + SEMI
note = Keyword("NOTE") + EQUAL + MLQSTRING + SEMI
notex = Keyword("NOTEX") + Optional(BPAREN + QSTRING + EPAREN) + EQUAL + MLQSTRING + SEMI
precision = Keyword("PRECISION") + BPAREN + QSTRING + COMMA + QSTRING + EPAREN + EQUAL + INTEGER + SEMI
showdecimals = Keyword("SHOWDECIMALS") + EQUAL + INTEGER + SEMI
source = Keyword("SOURCE") + EQUAL + QSTRING + SEMI
stub = Keyword("STUB") + EQUAL + OneOrMore(QSTRING | COMMA) + SEMI
subject_area = Keyword("SUBJECT-AREA") + EQUAL + QSTRING + SEMI
subject_code = Keyword("SUBJECT-CODE") + EQUAL + QSTRING + SEMI
timeval = Keyword("TIMEVAL") + BPAREN + QSTRING + EPAREN + EQUAL + tlist + SEMI
title = Keyword("TITLE") + EQUAL + QSTRING + SEMI
units = Keyword("UNITS") + EQUAL + QSTRING + SEMI
valuenotex = Keyword("VALUENOTEX") + BPAREN + QSTRING + COMMA + QSTRING + EPAREN + EQUAL + MLQSTRING + SEMI
values = Keyword("VALUES") + BPAREN + QSTRING + EPAREN + EQUAL + Group(OneOrMore(QSTRING | NEWLINE | COMMA)) + SEMI
variable_type = Keyword("VARIABLE-TYPE") + BPAREN + QSTRING + EPAREN + EQUAL + QSTRING + SEMI
pcaxis_parser = OneOrMore(
Group(axis_version("AXIS-VERSION")) | \
Group(charset("CHARSET")) | \
Group(codes("CODES")) | \
Group(contact("CONTACT")) | \
Group(contents("CONTENTS")) | \
Group(copyright("COPYRIGHT")) | \
Group(creation_date("CREATION-DATE")) | \
Group(data("DATA")) | \
Group(database("DATABASE")) | \
Group(datanotesum("DATANOTESUM")) | \
Group(datasymbol1("DATASYMBOL1")) | \
Group(datasymbol2("DATASYMBOL2")) | \
Group(datasymbol3("DATASYMBOL3")) | \
Group(datasymbol4("DATASYMBOL4")) | \
Group(datasymbol5("DATASYMBOL5")) | \
Group(datasymbol6("DATASYMBOL6")) | \
Group(datasymbolsum("DATASYMBOLSUM")) | \
Group(datasymbolnil("DATASYMBOLNIL")) | \
Group(decimals("DECIMALS")) | \
Group(description("DESCRIPTION")) | \
Group(descriptiondefault("DESCRIPTIONDEFAULT")) | \
Group(directory_path("DIRECTORY-PATH")) | \
Group(domain("DOMAIN")) | \
Group(elimination("ELIMINATION")) | \
Group(euro("EURO")) | \
Group(heading("HEADING")) | \
Group(infofile("INFOFILE")) | \
Group(language("LANGUAGE")) | \
Group(last_updated("LAST-UPDATED")) | \
Group(matrix("MATRIX")) | \
Group(note("NOTE")) | \
Group(notex("NOTEX")) | \
Group(precision("PRECISION")) | \
Group(showdecimals("SHOWDECIMALS")) | \
Group(source("SOURCE")) | \
Group(stub("STUB")) | \
Group(subject_area("SUBJECT-AREA")) | \
Group(subject_code("SUBJECT-CODE")) | \
Group(timeval("TIMEVAL")) | \
Group(title("TITLE")) | \
Group(units("UNITS")) | \
Group(valuenotex("VALUENOTEX")) | \
Group(values("VALUES")) | \
Group(variable_type("VARIABLE-TYPE")) | \
NEWLINE
)
def parseDataLine(line, struct_len):
dline2 = []
total = 1
for i in struct_len:
total = total * i
assert total == len(line), 'Line length does not match?'
struct2 = list(reversed(struct_len))
def divide(elements, substruct):
if len(substruct) == 1:
yield elements
else:
n = float(len(elements)) / substruct[0]
assert int(n) == n, 'Should be integer'
n = int(n)
for i in xrange(substruct[0]):
yield elements[i*n:(i+1)*n]
return list(divide(line, struct2))
def parseData(d, structure):
lines = []
cur = 0
total = len(d)
#print structure
struct_len = [len(i) for i in structure]
struct_len.reverse()
data_array = []
dline = []
while cur < total:
#print cur
if d[cur] == '"':
next = cur + 1
while next < total and d[next] != '"':
next += 1
dline.append(d[cur+1:next])
cur = next + 1 + 1
elif d[cur] == " ":
#pass
cur += 1
elif d[cur] in '0123456789.-':
next = cur
while next < total and d[next] in '0123456789.-':
next += 1
dline.append(float(d[cur:next]))
cur = next + 1
#print dline[-1]
elif d[cur] in '\r\n':
while d[cur] in '\r\n':
cur += 1
#print dline
# parse dline
dline2 = parseDataLine(dline, struct_len)
data_array.append(dline2)
dline = []
return data_array
def parsePX(data, encoding=None):
parseresults = pcaxis_parser.parseString(data)
if encoding is None:
# detect encoding
for item in parseresults:
if item[0] == 'CHARSET':
encoding = item[1]
try:
c = codecs.lookup(encoding)
except LookupError:
raise ValueError('Invalid encoding, please set charset manually by passing encoding parameter to parsePX', encoding)
parsed_data = {}
for item in parseresults:
key = item[0]
if key in ['LANGUAGE', 'CHARSET', 'AXIS-VERSION', 'CREATION-DATE',
'SUBJECT-AREA', 'SUBJECT-CODE', 'DESCRIPTION', 'LAST-UPDATED',
'SOURCE', 'CONTACT', 'MATRIX', 'TITLE', 'CONTENTS', 'UNITS',
'STUB', 'DATASYMBOL1', 'DATASYMBOL2', 'DATASYMBOL3', 'DATASYMBOL4',
'COPYRIGHT', 'DATABASE']:
assert len(item) == 2
parsed_data[key] = item[1].decode(encoding)
elif key in ['DECIMALS']:
parsed_data[key] = item[1]
elif key in ['HEADING']:
parsed_data[key] = item[1:]
elif key in ['CODES']:
codes = parsed_data.setdefault(key, {})
codes[item[1]] = tuple(item[2])
#print item
elif key in ['VALUES']:
codes = parsed_data.setdefault(key, {})
codes[item[1]] = [i.decode(encoding) for i in tuple(item[2])]
#print item
elif key in ['NOTEX', 'NOTE']:
parsed_data[key] = ''.join(item[1:])
elif key == 'DATA':
structure = []
for k in parsed_data['HEADING']:
nexts = parsed_data['CODES'].get(k, parsed_data['VALUES'].get(k))
structure.append(nexts)
pdata = parseData(item[1], structure)
#print pdata
parsed_data[key] = pdata
else:
print item
print item[0], len(item)
return parsed_data
if __name__ == "__main__":
tests = """CHARSET="ANSI";
AXIS-VERSION="2004";
LANGUAGE="sl";
CREATION-DATE="20050311 10:30";
SUBJECT-AREA="TRG DELA";
SUBJECT-CODE="07010";
MATRIX="0701011";
TIMEVAL("MESEC")=TLIST(M1,"200801"-"201108");"""
#for testcase in tests.split("\n"):
#print [testcase]
#print axis_parser.parseString(testcase)
import sys
outfile = hasattr(sys, 'pypy_version_info') and 'pypy' or 'cpython'
fn = sys.argv[1]
d = open(fn).read()
result = pcaxis_parser.parseString(d)
#print >> open(outfile + '_repr.txt', 'w'), repr(result)
#print >> open(outfile + '_str.txt', 'w'), str(result)
#print >> open(outfile + '.txt', 'w'), result
print result