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 |
287
|
aaronmk
|
if 'date' in items:
|
40 |
|
|
str_ = items['date']
|
41 |
|
|
try: import dateutil.parser
|
42 |
|
|
except ImportError: return str_
|
43 |
|
|
try: date = dateutil.parser.parse(str_)
|
44 |
|
|
except ValueError, e: raise SyntaxException(e)
|
45 |
|
|
else:
|
46 |
|
|
year = items['year']
|
47 |
|
|
month = items.get('month', '1')
|
48 |
|
|
day = items.get('day', '1')
|
49 |
|
|
try:
|
50 |
|
|
year = float(year)
|
51 |
|
|
month = int(month)
|
52 |
|
|
day = int(day)
|
53 |
|
|
except ValueError, e: raise SyntaxException(e)
|
54 |
|
|
date = (datetime.date(int(year), month, day) +
|
55 |
|
|
datetime.timedelta(round((year % 1.)*365)))
|
56 |
111
|
aaronmk
|
return date.strftime('%Y-%m-%d')
|
57 |
86
|
aaronmk
|
|
58 |
89
|
aaronmk
|
def name(items):
|
59 |
|
|
items = dict(items)
|
60 |
|
|
return ' '.join([items['first'], items['last']])
|
61 |
|
|
|
62 |
102
|
aaronmk
|
def namePart(items):
|
63 |
|
|
items = dict(items)
|
64 |
|
|
def to_parts(name): return items[name].split(' ')
|
65 |
|
|
parts = []
|
66 |
|
|
if 'first' in items: parts += to_parts('first')[:1]
|
67 |
|
|
if 'last' in items: parts += to_parts('last')[-1:]
|
68 |
|
|
if 'middle' in items: parts += to_parts('middle')[1:-1]
|
69 |
|
|
return ' '.join(parts)
|
70 |
|
|
|
71 |
86
|
aaronmk
|
# Function names must start with _ to avoid collisions with real tags
|
72 |
144
|
aaronmk
|
# Functions take arguments (items)
|
73 |
113
|
aaronmk
|
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
|
74 |
138
|
aaronmk
|
'_name': name, '_namePart': namePart}
|
75 |
86
|
aaronmk
|
|
76 |
142
|
aaronmk
|
def process(node):
|
77 |
139
|
aaronmk
|
name = node.tagName
|
78 |
280
|
aaronmk
|
if name.startswith('_') and name in funcs:
|
79 |
|
|
try: value = funcs[name](xml_dom.NodeTextEntryIter(node))
|
80 |
286
|
aaronmk
|
except SyntaxException, e: ex.repl_msg(e, func=name); raise
|
81 |
280
|
aaronmk
|
else: xml_dom.replace_with_text(node, value)
|
82 |
86
|
aaronmk
|
else:
|
83 |
142
|
aaronmk
|
for child in xml_dom.NodeElemIter(node): process(child)
|