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