Project

General

Profile

1
# XML "function" nodes that evaluate their contents to text
2

    
3
import datetime
4

    
5
import ex
6
import xml_dom
7

    
8
class SyntaxException(ex.ExceptionWithCause): pass
9

    
10
def map_items(func, items):
11
    return [(name, func(value)) for name, value in items]
12

    
13
def conv_items(type_, items):
14
    try: return map_items(type_, items)
15
    except ValueError, e: raise SyntaxException(e)
16

    
17
def alt(items):
18
    items = list(items)
19
    items.sort()
20
    return items[0][1] # value of lowest-numbered item
21

    
22
def range_(items):
23
    items = dict(conv_items(float, items))
24
    return str(items['to'] - items['from'])
25

    
26
def avg(items):
27
    count = 0
28
    sum_ = 0.
29
    for name, value in conv_items(float, items):
30
        count += 1
31
        sum_ += value
32
    return str(sum_/count)
33

    
34
def date(items):
35
    items = dict(items)
36
    year = items['year']
37
    month = items.get('month', '1')
38
    day = items.get('day', '1')
39
    try:
40
        year = float(year)
41
        month = int(month)
42
        day = int(day)
43
    except ValueError, e: raise SyntaxException(e)
44
    date = (datetime.date(int(year), month, day) +
45
        datetime.timedelta(round((year % 1.)*365)))
46
    return date.strftime('%Y-%m-%d')
47

    
48
def name(items):
49
    items = dict(items)
50
    return ' '.join([items['first'], items['last']])
51

    
52
def namePart(items):
53
    items = dict(items)
54
    def to_parts(name): return items[name].split(' ')
55
    parts = []
56
    if 'first' in items: parts += to_parts('first')[:1]
57
    if 'last' in items: parts += to_parts('last')[-1:]
58
    if 'middle' in items: parts += to_parts('middle')[1:-1]
59
    return ' '.join(parts)
60

    
61
# Function names must start with _ to avoid collisions with real tags
62
# Functions take arguments (items)
63
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
64
    '_name': name, '_namePart': namePart}
65

    
66
def process(node):
67
    name = node.tagName
68
    if name.startswith('_') and name in funcs: xml_dom.replace_with_text(node,
69
        funcs[name](xml_dom.NodeTextEntryIter(node)))
70
    else:
71
        for child in xml_dom.NodeElemIter(node): process(child)
(10-10/11)