Project

General

Profile

1
# XML "function" nodes that transform their contents
2

    
3
import datetime
4
import re
5

    
6
import dates
7
import exc
8
import maps
9
import term
10
import util
11
import xml_dom
12

    
13
##### Exceptions
14

    
15
class SyntaxException(Exception):
16
    def __init__(self, cause):
17
        Exception.__init__(self, 'Invalid XML function syntax: '
18
            +exc.str_(cause))
19

    
20
class FormatException(SyntaxException): pass
21

    
22
##### Functions
23

    
24
funcs = {}
25

    
26
def process(node, on_error=exc.raise_):
27
    for child in xml_dom.NodeElemIter(node): process(child, on_error)
28
    name = node.tagName
29
    if name.startswith('_') and name in funcs:
30
        try: value = funcs[name](xml_dom.NodeTextEntryIter(node))
31
        except SyntaxException, e:
32
            str_ = str(node)
33
            exc.add_msg(e, 'function:\n'+str_)
34
            xml_dom.replace(node, node.ownerDocument.createComment(
35
                '\n'+term.emph_multiline(str_)))
36
            on_error(e)
37
        else: xml_dom.replace_with_text(node, value)
38

    
39
def map_items(func, items):
40
    return [(name, func(value)) for name, value in items]
41

    
42
def conv_items(type_, items):
43
    def conv(val):
44
        try: return type_(val)
45
        except ValueError, e: raise SyntaxException(e)
46
    return map_items(conv, xml_dom.TextEntryOnlyIter(items))
47

    
48
#### XML functions
49

    
50
# Function names must start with _ to avoid collisions with real tags
51
# Functions take arguments (items)
52

    
53
def _ignore(items):
54
    '''Used to "comment out" an XML subtree'''
55
    return None
56
funcs['_ignore'] = _ignore
57

    
58
def _alt(items):
59
    items = list(items)
60
    items.sort()
61
    return items[0][1] # value of lowest-numbered item
62
funcs['_alt'] = _alt
63

    
64
def _merge(items):
65
    items = list(items)
66
    items.sort()
67
    return maps.merge_values(*[v for k, v in items])
68
funcs['_merge'] = _merge
69

    
70
def _label(items):
71
    items = dict(conv_items(str, items)) # get *once* from iter and check types
72
    try:
73
        label = items['label']
74
        value = items['value']
75
    except KeyError, e: raise SyntaxException(e)
76
    return label+': '+value
77
funcs['_label'] = _label
78

    
79
def _nullIf(items):
80
    items = dict(conv_items(str, items))
81
    try:
82
        null = items['null']
83
        value = items['value']
84
    except KeyError, e: raise SyntaxException(e)
85
    else:
86
        type_str = items.get('type', None)
87
        type_ = str
88
        if type_str == 'float': type_ = float
89
        return util.none_if(value, type_(null))
90
funcs['_nullIf'] = _nullIf
91

    
92
def _range(items):
93
    items = dict(conv_items(float, items))
94
    from_ = items.get('from', None)
95
    to = items.get('to', None)
96
    if from_ == None or to == None: return None
97
    return str(to - from_)
98
funcs['_range'] = _range
99

    
100
def _avg(items):
101
    count = 0
102
    sum_ = 0.
103
    for name, value in conv_items(float, items):
104
        count += 1
105
        sum_ += value
106
    return str(sum_/count)
107
funcs['_avg'] = _avg
108

    
109
class CvException(Exception):
110
    def __init__(self):
111
        Exception.__init__(self, 'CV (coefficient of variation) values are only'
112
            ' allowed for ratio scale data '
113
            '(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
114

    
115
def _noCV(items):
116
    try: name, value = items.next()
117
    except StopIteration: return None
118
    if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
119
    return value
120
funcs['_noCV'] = _noCV
121

    
122
def _date(items):
123
    items = conv_items(str, items) # get *once* from iter and check types
124
    try: str_ = dict(items)['date']
125
    except KeyError:
126
        items = dict(filter(lambda (k, v): v != 0, conv_items(int, items)))
127
        items.setdefault('year', 1900)
128
        items.setdefault('month', 1)
129
        items.setdefault('day', 1)
130
        try: date = datetime.date(**items)
131
        except ValueError, e: raise SyntaxException(e)
132
    else:
133
        try: year = float(str_)
134
        except ValueError:
135
            try: import dateutil.parser
136
            except ImportError: return str_
137
            try: date = dateutil.parser.parse(str_)
138
            except ValueError, e: raise SyntaxException(e)
139
        else: date = (datetime.date(int(year), 1, 1) +
140
            datetime.timedelta(round((year % 1.)*365)))
141
    try: return dates.strftime('%Y-%m-%d', date)
142
    except ValueError, e: raise FormatException(e)
143
funcs['_date'] = _date
144

    
145
_name_parts_slices_items = [
146
    ('first', slice(None, 1)),
147
    ('middle', slice(1, -1)),
148
    ('last', slice(-1, None)),
149
]
150
name_parts_slices = dict(_name_parts_slices_items)
151
name_parts = [name for name, slice_ in _name_parts_slices_items]
152

    
153
def _name(items):
154
    items = dict(items)
155
    parts = []
156
    for part in name_parts:
157
        if part in items: parts.append(items[part])
158
    return ' '.join(parts)
159
funcs['_name'] = _name
160

    
161
def _namePart(items):
162
    out_items = []
163
    for part, value in items:
164
        try: slice_ = name_parts_slices[part]
165
        except KeyError, e: raise SyntaxException(e)
166
        else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
167
    return _name(out_items)
168
funcs['_namePart'] = _namePart
169

    
(15-15/16)