Project

General

Profile

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

    
3
import datetime
4
import re
5
import sre_constants
6

    
7
import dates
8
import exc
9
import maps
10
import strings
11
import term
12
import util
13
import xml_dom
14
import xpath
15

    
16
##### Exceptions
17

    
18
class SyntaxException(Exception):
19
    def __init__(self, cause):
20
        Exception.__init__(self, 'Invalid XML function syntax: '
21
            +exc.str_(cause))
22

    
23
class FormatException(SyntaxException): pass
24

    
25
##### Functions
26

    
27
funcs = {}
28

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

    
43
def map_items(func, items):
44
    return [(name, func(value)) for name, value in items]
45

    
46
def cast(type_, val):
47
    '''Throws SyntaxException if can't cast'''
48
    try: return type_(val)
49
    except ValueError, e: raise SyntaxException(e)
50

    
51
def conv_items(type_, items):
52
    return map_items(lambda val: cast(type_, val),
53
        xml_dom.TextEntryOnlyIter(items))
54

    
55
#### XML functions
56

    
57
# Function names must start with _ to avoid collisions with real tags
58
# Functions take arguments (items)
59

    
60
def _ignore(items):
61
    '''Used to "comment out" an XML subtree'''
62
    return None
63
funcs['_ignore'] = _ignore
64

    
65
def _eq(items):
66
    items = dict(items)
67
    try:
68
        left = items['left']
69
        right = items['right']
70
    except KeyError: return '' # a value was None
71
    return util.bool2str(left == right)
72
funcs['_eq'] = _eq
73

    
74
def _if(items):
75
    items = dict(items)
76
    try:
77
        cond = items['cond']
78
        then = items['then']
79
    except KeyError, e: raise SyntaxException(e)
80
    else_ = items.get('else', None)
81
    cond = bool(cast(str, cond))
82
    if cond: return then
83
    else: return else_
84
funcs['_if'] = _if
85

    
86
def _alt(items):
87
    items = list(items)
88
    items.sort()
89
    try: return items[0][1] # value of lowest-numbered item
90
    except IndexError: return None # input got removed by e.g. SyntaxException
91
funcs['_alt'] = _alt
92

    
93
def _merge(items):
94
    items = list(conv_items(strings.ustr, items))
95
        # get *once* from iter and check types
96
    items.sort()
97
    return maps.merge_values(*[v for k, v in items])
98
funcs['_merge'] = _merge
99

    
100
def _label(items):
101
    items = dict(conv_items(str, items)) # get *once* from iter and check types
102
    try:
103
        label = items['label']
104
        value = items['value']
105
    except KeyError, e: raise SyntaxException(e)
106
    return label+': '+value
107
funcs['_label'] = _label
108

    
109
def _nullIf(items):
110
    items = dict(conv_items(str, items))
111
    try:
112
        null = items['null']
113
        value = items['value']
114
    except KeyError, e: raise SyntaxException(e)
115
    type_str = items.get('type', None)
116
    type_ = str
117
    if type_str == 'float': type_ = float
118
    return util.none_if(value, type_(null))
119
funcs['_nullIf'] = _nullIf
120

    
121
def _map(items):
122
    items = conv_items(str, items) # get *once* from iter and check types
123
    try: value = items.pop()[1] # value is last entry's value
124
    except IndexError, e: raise SyntaxException(e)
125
    map_ = dict(items)
126
    closed = bool(map_.pop('_closed', False))
127
    try: return map_[value]
128
    except KeyError, e:
129
        if closed: raise SyntaxException(e)
130
        else: return value
131
funcs['_map'] = _map
132

    
133
def _replace(items):
134
    items = conv_items(str, items) # get *once* from iter and check types
135
    try: value = items.pop() # value is last entry
136
    except IndexError, e: raise SyntaxException(e)
137
    try:
138
        for repl, with_ in items:
139
            if re.match(r'^\w+$', repl):
140
                repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
141
            value = re.sub(repl, with_, value)
142
    except sre_constants.error, e: raise SyntaxException(e)
143
    return value
144
funcs['_replace'] = _replace
145

    
146
def _units(items):
147
    items = dict(conv_items(str, items))
148
    try:
149
        units = items['units']
150
        value = items['value']
151
    except KeyError, e: raise SyntaxException(e)
152
    return value#+' '+units # don't add yet because unit conversion isn't ready
153
funcs['_units'] = _units
154

    
155
def _range(items):
156
    items = dict(conv_items(float, items))
157
    from_ = items.get('from', None)
158
    to = items.get('to', None)
159
    if from_ == None or to == None: return None
160
    return str(to - from_)
161
funcs['_range'] = _range
162

    
163
def _avg(items):
164
    count = 0
165
    sum_ = 0.
166
    for name, value in conv_items(float, items):
167
        count += 1
168
        sum_ += value
169
    return str(sum_/count)
170
funcs['_avg'] = _avg
171

    
172
class CvException(Exception):
173
    def __init__(self):
174
        Exception.__init__(self, 'CV (coefficient of variation) values are only'
175
            ' allowed for ratio scale data '
176
            '(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
177

    
178
def _noCV(items):
179
    try: name, value = items.next()
180
    except StopIteration: return None
181
    if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
182
    return value
183
funcs['_noCV'] = _noCV
184

    
185
def _date(items):
186
    items = conv_items(str, items) # get *once* from iter and check types
187
    try: str_ = dict(items)['date']
188
    except KeyError:
189
        items = dict(conv_items(int, items))
190
        try: items['year'] # year is required
191
        except KeyError, e:
192
            if items == {}: return None # entire date is empty
193
            else: raise SyntaxException(e)
194
        items.setdefault('month', 1)
195
        items.setdefault('day', 1)
196
        try: date = datetime.date(**items)
197
        except ValueError, e: raise SyntaxException(e)
198
    else:
199
        try: year = float(str_)
200
        except ValueError:
201
            try: date = dates.strtotime(str_)
202
            except ImportError: return str_
203
            except ValueError, e: raise SyntaxException(e)
204
        else: date = (datetime.date(int(year), 1, 1) +
205
            datetime.timedelta(round((year % 1.)*365)))
206
    try: return dates.strftime('%Y-%m-%d', date)
207
    except ValueError, e: raise FormatException(e)
208
funcs['_date'] = _date
209

    
210
date_range_sep = '-'
211
date_range_date_part_sep = ' '
212

    
213
def _dateRangePart(items):
214
    items = dict(conv_items(str, items))
215
    try:
216
        part = items['part']
217
        value = items['value']
218
    except KeyError, e: raise SyntaxException(e)
219
    value = value.strip()
220
    if value.find(date_range_date_part_sep) < 0: return value
221
        # date_range_sep might be used as date part separator instead
222
    
223
    start, sep, end = value.partition(date_range_sep)
224
    if sep == '': return value # not a range
225
    start = start.split(date_range_date_part_sep)
226
    end = end.split(date_range_date_part_sep)
227
    #if len(end) < 3
228
    #if len(start) < len(end): # range 
229
funcs['_dateRangePart'] = _dateRangePart
230

    
231
_name_parts_slices_items = [
232
    ('first', slice(None, 1)),
233
    ('middle', slice(1, -1)),
234
    ('last', slice(-1, None)),
235
]
236
name_parts_slices = dict(_name_parts_slices_items)
237
name_parts = [name for name, slice_ in _name_parts_slices_items]
238

    
239
def _name(items):
240
    items = dict(items)
241
    parts = []
242
    for part in name_parts:
243
        if part in items: parts.append(items[part])
244
    return ' '.join(parts)
245
funcs['_name'] = _name
246

    
247
def _namePart(items):
248
    out_items = []
249
    for part, value in items:
250
        try: slice_ = name_parts_slices[part]
251
        except KeyError, e: raise SyntaxException(e)
252
        out_items.append((part, ' '.join(value.split(' ')[slice_])))
253
    return _name(out_items)
254
funcs['_namePart'] = _namePart
255

    
256
def _simplifyPath(items):
257
    items = dict(items)
258
    try:
259
        next = cast(str, items['next'])
260
        require = cast(str, items['require'])
261
        root = items['path']
262
    except KeyError, e: raise SyntaxException(e)
263
    
264
    node = root
265
    while node != None:
266
        new_node = xpath.get_1(node, next, allow_rooted=False)
267
        if xpath.get_1(node, require, allow_rooted=False) == None: # empty elem
268
            xml_dom.replace(node, new_node) # remove current elem
269
            if node is root: root = new_node # also update root
270
        node = new_node
271
    return root
272
funcs['_simplifyPath'] = _simplifyPath
(15-15/16)