1 |
86
|
aaronmk
|
# XML "function" nodes that evaluate their contents to text
|
2 |
|
|
|
3 |
111
|
aaronmk
|
import datetime
|
4 |
|
|
|
5 |
300
|
aaronmk
|
import exc
|
6 |
86
|
aaronmk
|
import xml_dom
|
7 |
|
|
|
8 |
280
|
aaronmk
|
class SyntaxException(Exception):
|
9 |
|
|
def __init__(self, cause=None):
|
10 |
288
|
aaronmk
|
Exception.__init__(self, 'Invalid syntax in XML function: '+str(cause))
|
11 |
278
|
aaronmk
|
|
12 |
86
|
aaronmk
|
def map_items(func, items):
|
13 |
|
|
return [(name, func(value)) for name, value in items]
|
14 |
|
|
|
15 |
278
|
aaronmk
|
def conv_items(type_, items):
|
16 |
|
|
try: return map_items(type_, items)
|
17 |
|
|
except ValueError, e: raise SyntaxException(e)
|
18 |
|
|
|
19 |
113
|
aaronmk
|
def alt(items):
|
20 |
|
|
items = list(items)
|
21 |
|
|
items.sort()
|
22 |
|
|
return items[0][1] # value of lowest-numbered item
|
23 |
|
|
|
24 |
86
|
aaronmk
|
def range_(items):
|
25 |
278
|
aaronmk
|
items = dict(conv_items(float, items))
|
26 |
86
|
aaronmk
|
return str(items['to'] - items['from'])
|
27 |
|
|
|
28 |
|
|
def avg(items):
|
29 |
|
|
count = 0
|
30 |
|
|
sum_ = 0.
|
31 |
278
|
aaronmk
|
for name, value in conv_items(float, items):
|
32 |
86
|
aaronmk
|
count += 1
|
33 |
|
|
sum_ += value
|
34 |
|
|
return str(sum_/count)
|
35 |
|
|
|
36 |
|
|
def date(items):
|
37 |
|
|
items = dict(items)
|
38 |
287
|
aaronmk
|
if 'date' in items:
|
39 |
|
|
str_ = items['date']
|
40 |
|
|
try: import dateutil.parser
|
41 |
|
|
except ImportError: return str_
|
42 |
|
|
try: date = dateutil.parser.parse(str_)
|
43 |
|
|
except ValueError, e: raise SyntaxException(e)
|
44 |
|
|
else:
|
45 |
|
|
year = items['year']
|
46 |
|
|
month = items.get('month', '1')
|
47 |
|
|
day = items.get('day', '1')
|
48 |
|
|
try:
|
49 |
|
|
year = float(year)
|
50 |
|
|
month = int(month)
|
51 |
|
|
day = int(day)
|
52 |
|
|
except ValueError, e: raise SyntaxException(e)
|
53 |
|
|
date = (datetime.date(int(year), month, day) +
|
54 |
|
|
datetime.timedelta(round((year % 1.)*365)))
|
55 |
111
|
aaronmk
|
return date.strftime('%Y-%m-%d')
|
56 |
86
|
aaronmk
|
|
57 |
89
|
aaronmk
|
def name(items):
|
58 |
|
|
items = dict(items)
|
59 |
|
|
return ' '.join([items['first'], items['last']])
|
60 |
|
|
|
61 |
102
|
aaronmk
|
def namePart(items):
|
62 |
|
|
items = dict(items)
|
63 |
|
|
def to_parts(name): return items[name].split(' ')
|
64 |
|
|
parts = []
|
65 |
|
|
if 'first' in items: parts += to_parts('first')[:1]
|
66 |
|
|
if 'last' in items: parts += to_parts('last')[-1:]
|
67 |
|
|
if 'middle' in items: parts += to_parts('middle')[1:-1]
|
68 |
|
|
return ' '.join(parts)
|
69 |
|
|
|
70 |
86
|
aaronmk
|
# Function names must start with _ to avoid collisions with real tags
|
71 |
144
|
aaronmk
|
# Functions take arguments (items)
|
72 |
113
|
aaronmk
|
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
|
73 |
138
|
aaronmk
|
'_name': name, '_namePart': namePart}
|
74 |
86
|
aaronmk
|
|
75 |
142
|
aaronmk
|
def process(node):
|
76 |
139
|
aaronmk
|
name = node.tagName
|
77 |
280
|
aaronmk
|
if name.startswith('_') and name in funcs:
|
78 |
|
|
try: value = funcs[name](xml_dom.NodeTextEntryIter(node))
|
79 |
288
|
aaronmk
|
except SyntaxException, e:
|
80 |
300
|
aaronmk
|
exc.add_msg(e, 'function: '+str(node))
|
81 |
288
|
aaronmk
|
raise
|
82 |
280
|
aaronmk
|
else: xml_dom.replace_with_text(node, value)
|
83 |
86
|
aaronmk
|
else:
|
84 |
142
|
aaronmk
|
for child in xml_dom.NodeElemIter(node): process(child)
|