1
|
# XML "function" nodes that transform their contents
|
2
|
|
3
|
import datetime
|
4
|
import re
|
5
|
import sre_constants
|
6
|
import sys
|
7
|
|
8
|
import dates
|
9
|
import exc
|
10
|
import maps
|
11
|
import strings
|
12
|
import term
|
13
|
import util
|
14
|
import xml_dom
|
15
|
import xpath
|
16
|
|
17
|
##### Exceptions
|
18
|
|
19
|
class SyntaxException(Exception):
|
20
|
def __init__(self, cause):
|
21
|
Exception.__init__(self, 'Invalid XML function syntax: '
|
22
|
+exc.str_(cause))
|
23
|
|
24
|
class FormatException(SyntaxException): pass
|
25
|
|
26
|
##### Functions
|
27
|
|
28
|
funcs = {}
|
29
|
|
30
|
def process(node, on_error=exc.raise_):
|
31
|
for child in xml_dom.NodeElemIter(node): process(child, on_error)
|
32
|
name = node.tagName
|
33
|
if name.startswith('_') and name in funcs:
|
34
|
try:
|
35
|
value = funcs[name](xml_dom.NodeTextEntryIter(node))
|
36
|
xml_dom.replace_with_text(node, value)
|
37
|
except Exception, e: # also catch XML func internal errors
|
38
|
# Save in case another exception raised, overwriting sys.exc_info()
|
39
|
exc.add_traceback(e)
|
40
|
str_ = str(node)
|
41
|
exc.add_msg(e, 'function:\n'+str_)
|
42
|
xml_dom.replace(node, node.ownerDocument.createComment(
|
43
|
'\n'+term.emph_multiline(str_).replace('--','-')))
|
44
|
# comments can't contain '--'
|
45
|
on_error(e)
|
46
|
|
47
|
def map_items(func, items):
|
48
|
return [(name, func(value)) for name, value in items]
|
49
|
|
50
|
def cast(type_, val):
|
51
|
'''Throws SyntaxException if can't cast'''
|
52
|
try: return type_(val)
|
53
|
except ValueError, e: raise SyntaxException(e)
|
54
|
|
55
|
def conv_items(type_, items):
|
56
|
return map_items(lambda val: cast(type_, val),
|
57
|
xml_dom.TextEntryOnlyIter(items))
|
58
|
|
59
|
#### XML functions
|
60
|
|
61
|
# Function names must start with _ to avoid collisions with real tags
|
62
|
# Functions take arguments (items)
|
63
|
|
64
|
def _ignore(items):
|
65
|
'''Used to "comment out" an XML subtree'''
|
66
|
return None
|
67
|
funcs['_ignore'] = _ignore
|
68
|
|
69
|
def _eq(items):
|
70
|
items = dict(items)
|
71
|
try:
|
72
|
left = items['left']
|
73
|
right = items['right']
|
74
|
except KeyError: return '' # a value was None
|
75
|
return util.bool2str(left == right)
|
76
|
funcs['_eq'] = _eq
|
77
|
|
78
|
def _if(items):
|
79
|
items = dict(items)
|
80
|
try:
|
81
|
cond = items['cond']
|
82
|
then = items['then']
|
83
|
except KeyError, e: raise SyntaxException(e)
|
84
|
else_ = items.get('else', None)
|
85
|
cond = bool(cast(str, cond))
|
86
|
if cond: return then
|
87
|
else: return else_
|
88
|
funcs['_if'] = _if
|
89
|
|
90
|
def _alt(items):
|
91
|
items = list(items)
|
92
|
items.sort()
|
93
|
try: return items[0][1] # value of lowest-numbered item
|
94
|
except IndexError: return None # input got removed by e.g. SyntaxException
|
95
|
funcs['_alt'] = _alt
|
96
|
|
97
|
def _merge(items):
|
98
|
items = list(conv_items(strings.ustr, items))
|
99
|
# get *once* from iter and check types
|
100
|
items.sort()
|
101
|
return maps.merge_values(*[v for k, v in items])
|
102
|
funcs['_merge'] = _merge
|
103
|
|
104
|
def _label(items):
|
105
|
items = dict(conv_items(str, items)) # get *once* from iter and check types
|
106
|
try:
|
107
|
label = items['label']
|
108
|
value = items['value']
|
109
|
except KeyError, e: raise SyntaxException(e)
|
110
|
return label+': '+value
|
111
|
funcs['_label'] = _label
|
112
|
|
113
|
def _nullIf(items):
|
114
|
items = dict(conv_items(str, items))
|
115
|
try:
|
116
|
null = items['null']
|
117
|
value = items['value']
|
118
|
except KeyError, e: raise SyntaxException(e)
|
119
|
type_str = items.get('type', None)
|
120
|
type_ = str
|
121
|
if type_str == 'float': type_ = float
|
122
|
return util.none_if(value, type_(null))
|
123
|
funcs['_nullIf'] = _nullIf
|
124
|
|
125
|
def _map(items):
|
126
|
items = conv_items(str, items) # get *once* from iter and check types
|
127
|
try: value = items.pop()[1] # value is last entry's value
|
128
|
except IndexError, e: raise SyntaxException(e)
|
129
|
map_ = dict(items)
|
130
|
closed = bool(map_.pop('_closed', False))
|
131
|
try: return map_[value]
|
132
|
except KeyError, e:
|
133
|
if closed: raise SyntaxException(e)
|
134
|
else: return value
|
135
|
funcs['_map'] = _map
|
136
|
|
137
|
def _replace(items):
|
138
|
items = conv_items(str, items) # get *once* from iter and check types
|
139
|
try: value = items.pop() # value is last entry
|
140
|
except IndexError, e: raise SyntaxException(e)
|
141
|
try:
|
142
|
for repl, with_ in items:
|
143
|
if re.match(r'^\w+$', repl):
|
144
|
repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
|
145
|
value = re.sub(repl, with_, value)
|
146
|
except sre_constants.error, e: raise SyntaxException(e)
|
147
|
return value
|
148
|
funcs['_replace'] = _replace
|
149
|
|
150
|
def _units(items):
|
151
|
items = dict(conv_items(str, items))
|
152
|
try:
|
153
|
units = items['units']
|
154
|
value = items['value']
|
155
|
except KeyError, e: raise SyntaxException(e)
|
156
|
return value#+' '+units # don't add yet because unit conversion isn't ready
|
157
|
funcs['_units'] = _units
|
158
|
|
159
|
def _range(items):
|
160
|
items = dict(conv_items(float, items))
|
161
|
from_ = items.get('from', None)
|
162
|
to = items.get('to', None)
|
163
|
if from_ == None or to == None: return None
|
164
|
return str(to - from_)
|
165
|
funcs['_range'] = _range
|
166
|
|
167
|
def parse_range(str_, range_sep='-'):
|
168
|
default = (str_, None)
|
169
|
start, sep, end = str_.partition(range_sep)
|
170
|
if sep == '': return default # not a range
|
171
|
return tuple(d.strip() for d in (start, end))
|
172
|
|
173
|
def _rangeStart(items):
|
174
|
items = dict(conv_items(str, items))
|
175
|
try: value = items['value']
|
176
|
except KeyError: return None # input is empty
|
177
|
return parse_range(value)[0]
|
178
|
funcs['_rangeStart'] = _rangeStart
|
179
|
|
180
|
def _rangeEnd(items):
|
181
|
items = dict(conv_items(str, items))
|
182
|
try: value = items['value']
|
183
|
except KeyError: return None # input is empty
|
184
|
return parse_range(value)[1]
|
185
|
funcs['_rangeEnd'] = _rangeEnd
|
186
|
|
187
|
def _avg(items):
|
188
|
count = 0
|
189
|
sum_ = 0.
|
190
|
for name, value in conv_items(float, items):
|
191
|
count += 1
|
192
|
sum_ += value
|
193
|
return str(sum_/count)
|
194
|
funcs['_avg'] = _avg
|
195
|
|
196
|
class CvException(Exception):
|
197
|
def __init__(self):
|
198
|
Exception.__init__(self, 'CV (coefficient of variation) values are only'
|
199
|
' allowed for ratio scale data '
|
200
|
'(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
|
201
|
|
202
|
def _noCV(items):
|
203
|
try: name, value = items.next()
|
204
|
except StopIteration: return None
|
205
|
if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
|
206
|
return value
|
207
|
funcs['_noCV'] = _noCV
|
208
|
|
209
|
def _date(items):
|
210
|
items = conv_items(str, items) # get *once* from iter and check types
|
211
|
try: str_ = dict(items)['date']
|
212
|
except KeyError:
|
213
|
items = dict(conv_items(int, items))
|
214
|
try: items['year'] # year is required
|
215
|
except KeyError, e:
|
216
|
if items == {}: return None # entire date is empty
|
217
|
else: raise SyntaxException(e)
|
218
|
items.setdefault('month', 1)
|
219
|
items.setdefault('day', 1)
|
220
|
try: date = datetime.date(**items)
|
221
|
except ValueError, e: raise SyntaxException(e)
|
222
|
else:
|
223
|
try: year = float(str_)
|
224
|
except ValueError:
|
225
|
try: date = dates.strtotime(str_)
|
226
|
except ImportError: return str_
|
227
|
except ValueError, e: raise SyntaxException(e)
|
228
|
else: date = (datetime.date(int(year), 1, 1) +
|
229
|
datetime.timedelta(round((year % 1.)*365)))
|
230
|
try: return dates.strftime('%Y-%m-%d', date)
|
231
|
except ValueError, e: raise FormatException(e)
|
232
|
funcs['_date'] = _date
|
233
|
|
234
|
def _dateRangeStart(items):
|
235
|
items = dict(conv_items(str, items))
|
236
|
try: value = items['value']
|
237
|
except KeyError: return None # input is empty
|
238
|
return dates.parse_date_range(value)[0]
|
239
|
funcs['_dateRangeStart'] = _dateRangeStart
|
240
|
|
241
|
def _dateRangeEnd(items):
|
242
|
items = dict(conv_items(str, items))
|
243
|
try: value = items['value']
|
244
|
except KeyError: return None # input is empty
|
245
|
return dates.parse_date_range(value)[1]
|
246
|
funcs['_dateRangeEnd'] = _dateRangeEnd
|
247
|
|
248
|
_name_parts_slices_items = [
|
249
|
('first', slice(None, 1)),
|
250
|
('middle', slice(1, -1)),
|
251
|
('last', slice(-1, None)),
|
252
|
]
|
253
|
name_parts_slices = dict(_name_parts_slices_items)
|
254
|
name_parts = [name for name, slice_ in _name_parts_slices_items]
|
255
|
|
256
|
def _name(items):
|
257
|
items = dict(items)
|
258
|
parts = []
|
259
|
for part in name_parts:
|
260
|
if part in items: parts.append(items[part])
|
261
|
return ' '.join(parts)
|
262
|
funcs['_name'] = _name
|
263
|
|
264
|
def _namePart(items):
|
265
|
out_items = []
|
266
|
for part, value in items:
|
267
|
try: slice_ = name_parts_slices[part]
|
268
|
except KeyError, e: raise SyntaxException(e)
|
269
|
out_items.append((part, ' '.join(value.split(' ')[slice_])))
|
270
|
return _name(out_items)
|
271
|
funcs['_namePart'] = _namePart
|
272
|
|
273
|
def _simplifyPath(items):
|
274
|
items = dict(items)
|
275
|
try:
|
276
|
next = cast(str, items['next'])
|
277
|
require = cast(str, items['require'])
|
278
|
root = items['path']
|
279
|
except KeyError, e: raise SyntaxException(e)
|
280
|
|
281
|
node = root
|
282
|
while node != None:
|
283
|
new_node = xpath.get_1(node, next, allow_rooted=False)
|
284
|
if xpath.get_1(node, require, allow_rooted=False) == None: # empty elem
|
285
|
xml_dom.replace(node, new_node) # remove current elem
|
286
|
if node is root: root = new_node # also update root
|
287
|
node = new_node
|
288
|
return root
|
289
|
funcs['_simplifyPath'] = _simplifyPath
|