1
|
# XML "function" nodes that transform their contents
|
2
|
|
3
|
import datetime
|
4
|
import re
|
5
|
import sre_constants
|
6
|
|
7
|
import dates
|
8
|
import exc
|
9
|
import maps
|
10
|
import term
|
11
|
import util
|
12
|
import xml_dom
|
13
|
|
14
|
##### Exceptions
|
15
|
|
16
|
class SyntaxException(Exception):
|
17
|
def __init__(self, cause):
|
18
|
Exception.__init__(self, 'Invalid XML function syntax: '
|
19
|
+exc.str_(cause))
|
20
|
|
21
|
class FormatException(SyntaxException): pass
|
22
|
|
23
|
##### Functions
|
24
|
|
25
|
funcs = {}
|
26
|
|
27
|
def process(node, on_error=exc.raise_):
|
28
|
for child in xml_dom.NodeElemIter(node): process(child, on_error)
|
29
|
name = node.tagName
|
30
|
if name.startswith('_') and name in funcs:
|
31
|
try: value = funcs[name](xml_dom.NodeTextEntryIter(node))
|
32
|
except SyntaxException, e:
|
33
|
str_ = str(node)
|
34
|
exc.add_msg(e, 'function:\n'+str_)
|
35
|
xml_dom.replace(node, node.ownerDocument.createComment(
|
36
|
'\n'+term.emph_multiline(str_)))
|
37
|
on_error(e)
|
38
|
else: xml_dom.replace_with_text(node, value)
|
39
|
|
40
|
def map_items(func, items):
|
41
|
return [(name, func(value)) for name, value in items]
|
42
|
|
43
|
def conv_items(type_, items):
|
44
|
def conv(val):
|
45
|
try: return type_(val)
|
46
|
except ValueError, e: raise SyntaxException(e)
|
47
|
return map_items(conv, xml_dom.TextEntryOnlyIter(items))
|
48
|
|
49
|
#### XML functions
|
50
|
|
51
|
# Function names must start with _ to avoid collisions with real tags
|
52
|
# Functions take arguments (items)
|
53
|
|
54
|
def _ignore(items):
|
55
|
'''Used to "comment out" an XML subtree'''
|
56
|
return None
|
57
|
funcs['_ignore'] = _ignore
|
58
|
|
59
|
def _alt(items):
|
60
|
items = list(items)
|
61
|
items.sort()
|
62
|
try: return items[0][1] # value of lowest-numbered item
|
63
|
except IndexError: return None # input got removed by e.g. SyntaxException
|
64
|
funcs['_alt'] = _alt
|
65
|
|
66
|
def _merge(items):
|
67
|
items = list(items)
|
68
|
items.sort()
|
69
|
return maps.merge_values(*[v for k, v in items])
|
70
|
funcs['_merge'] = _merge
|
71
|
|
72
|
def _label(items):
|
73
|
items = dict(conv_items(str, items)) # get *once* from iter and check types
|
74
|
try:
|
75
|
label = items['label']
|
76
|
value = items['value']
|
77
|
except KeyError, e: raise SyntaxException(e)
|
78
|
return label+': '+value
|
79
|
funcs['_label'] = _label
|
80
|
|
81
|
def _nullIf(items):
|
82
|
items = dict(conv_items(str, items))
|
83
|
try:
|
84
|
null = items['null']
|
85
|
value = items['value']
|
86
|
except KeyError, e: raise SyntaxException(e)
|
87
|
type_str = items.get('type', None)
|
88
|
type_ = str
|
89
|
if type_str == 'float': type_ = float
|
90
|
return util.none_if(value, type_(null))
|
91
|
funcs['_nullIf'] = _nullIf
|
92
|
|
93
|
def _map(items):
|
94
|
items = conv_items(str, items) # get *once* from iter and check types
|
95
|
try: value = items.pop()[1] # value is last entry's value
|
96
|
except IndexError, e: raise SyntaxException(e)
|
97
|
map_ = dict(items)
|
98
|
try: return map_[value]
|
99
|
except KeyError: return value
|
100
|
funcs['_map'] = _map
|
101
|
|
102
|
def _replace(items):
|
103
|
items = conv_items(str, items) # get *once* from iter and check types
|
104
|
try: value = items.pop() # value is last entry
|
105
|
except IndexError, e: raise SyntaxException(e)
|
106
|
try:
|
107
|
for repl, with_ in items:
|
108
|
if re.match(r'^\w+$', repl):
|
109
|
repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
|
110
|
value = re.sub(repl, with_, value)
|
111
|
except sre_constants.error, e: raise SyntaxException(e)
|
112
|
return value
|
113
|
funcs['_replace'] = _replace
|
114
|
|
115
|
def _units(items):
|
116
|
items = dict(conv_items(str, items))
|
117
|
try:
|
118
|
units = items['units']
|
119
|
value = items['value']
|
120
|
except KeyError, e: raise SyntaxException(e)
|
121
|
return value#+' '+units # don't add yet because unit conversion isn't ready
|
122
|
funcs['_units'] = _units
|
123
|
|
124
|
def _range(items):
|
125
|
items = dict(conv_items(float, items))
|
126
|
from_ = items.get('from', None)
|
127
|
to = items.get('to', None)
|
128
|
if from_ == None or to == None: return None
|
129
|
return str(to - from_)
|
130
|
funcs['_range'] = _range
|
131
|
|
132
|
def _avg(items):
|
133
|
count = 0
|
134
|
sum_ = 0.
|
135
|
for name, value in conv_items(float, items):
|
136
|
count += 1
|
137
|
sum_ += value
|
138
|
return str(sum_/count)
|
139
|
funcs['_avg'] = _avg
|
140
|
|
141
|
class CvException(Exception):
|
142
|
def __init__(self):
|
143
|
Exception.__init__(self, 'CV (coefficient of variation) values are only'
|
144
|
' allowed for ratio scale data '
|
145
|
'(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
|
146
|
|
147
|
def _noCV(items):
|
148
|
try: name, value = items.next()
|
149
|
except StopIteration: return None
|
150
|
if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
|
151
|
return value
|
152
|
funcs['_noCV'] = _noCV
|
153
|
|
154
|
def _date(items):
|
155
|
items = conv_items(str, items) # get *once* from iter and check types
|
156
|
try: str_ = dict(items)['date']
|
157
|
except KeyError:
|
158
|
items = dict(filter(lambda (k, v): v != 0, conv_items(int, items)))
|
159
|
items.setdefault('year', 1900)
|
160
|
items.setdefault('month', 1)
|
161
|
items.setdefault('day', 1)
|
162
|
try: date = datetime.date(**items)
|
163
|
except ValueError, e: raise SyntaxException(e)
|
164
|
else:
|
165
|
try: year = float(str_)
|
166
|
except ValueError:
|
167
|
try: import dateutil.parser
|
168
|
except ImportError: return str_
|
169
|
try: date = dateutil.parser.parse(str_)
|
170
|
except ValueError, e: raise SyntaxException(e)
|
171
|
else: date = (datetime.date(int(year), 1, 1) +
|
172
|
datetime.timedelta(round((year % 1.)*365)))
|
173
|
try: return dates.strftime('%Y-%m-%d', date)
|
174
|
except ValueError, e: raise FormatException(e)
|
175
|
funcs['_date'] = _date
|
176
|
|
177
|
_name_parts_slices_items = [
|
178
|
('first', slice(None, 1)),
|
179
|
('middle', slice(1, -1)),
|
180
|
('last', slice(-1, None)),
|
181
|
]
|
182
|
name_parts_slices = dict(_name_parts_slices_items)
|
183
|
name_parts = [name for name, slice_ in _name_parts_slices_items]
|
184
|
|
185
|
def _name(items):
|
186
|
items = dict(items)
|
187
|
parts = []
|
188
|
for part in name_parts:
|
189
|
if part in items: parts.append(items[part])
|
190
|
return ' '.join(parts)
|
191
|
funcs['_name'] = _name
|
192
|
|
193
|
def _namePart(items):
|
194
|
out_items = []
|
195
|
for part, value in items:
|
196
|
try: slice_ = name_parts_slices[part]
|
197
|
except KeyError, e: raise SyntaxException(e)
|
198
|
out_items.append((part, ' '.join(value.split(' ')[slice_])))
|
199
|
return _name(out_items)
|
200
|
funcs['_namePart'] = _namePart
|
201
|
|