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 angles
8
import dates
9
import exc
10
import format
11
import maps
12
import strings
13
import term
14
import units
15
import util
16
import xml_dom
17
import xpath
18

    
19
##### Exceptions
20

    
21
class SyntaxError(exc.ExceptionWithCause):
22
    def __init__(self, cause):
23
        exc.ExceptionWithCause.__init__(self, 'Invalid XML function syntax',
24
            cause)
25

    
26
class FormatException(exc.ExceptionWithCause):
27
    def __init__(self, cause):
28
        exc.ExceptionWithCause.__init__(self, 'Invalid input value', cause)
29

    
30
##### Helper functions
31

    
32
def map_items(func, items):
33
    return [(name, func(value)) for name, value in items]
34

    
35
def cast(type_, val):
36
    '''Throws FormatException if can't cast'''
37
    try: return type_(val)
38
    except ValueError, e: raise FormatException(e)
39

    
40
def conv_items(type_, items):
41
    return map_items(lambda val: cast(type_, val),
42
        xml_dom.TextEntryOnlyIter(items))
43

    
44
def pop_value(items, name='value'):
45
    '''@param name Name of value param, or None to accept any name'''
46
    try: last = items.pop() # last entry contains value
47
    except IndexError: return None # input is empty and no actions
48
    if name != None and last[0] != name: return None # input is empty
49
    return last[1]
50

    
51
funcs = {}
52

    
53
##### Public functions
54

    
55
def process(node, on_error=exc.raise_):
56
    for child in xml_dom.NodeElemIter(node): process(child, on_error)
57
    name = node.tagName
58
    if name.startswith('_') and name in funcs:
59
        try:
60
            value = funcs[name](xml_dom.NodeTextEntryIter(node))
61
            xml_dom.replace_with_text(node, value)
62
        except Exception, e: # also catch non-wrapped exceptions (XML func bugs)
63
            # Save in case another exception raised, overwriting sys.exc_info()
64
            exc.add_traceback(e)
65
            str_ = strings.ustr(node)
66
            exc.add_msg(e, 'function:\n'+str_)
67
            xml_dom.replace(node, xml_dom.mk_comment(node.ownerDocument,
68
                '\n'+term.emph_multiline(str_)))
69
                
70
            on_error(e)
71

    
72
def strip(node):
73
    '''Replaces every XML function with its last parameter (which is usually its
74
    value), except for _ignore, which is removed completely'''
75
    for child in xml_dom.NodeElemIter(node): strip(child)
76
    name = node.tagName
77
    if name.startswith('_') and name in funcs:
78
        if name == '_ignore': value = None
79
        else: value = pop_value(list(xml_dom.NodeTextEntryIter(node)), None)
80
        xml_dom.replace_with_text(node, value)
81

    
82
##### XML functions
83

    
84
# Function names must start with _ to avoid collisions with real tags
85
# Functions take arguments (items)
86

    
87
#### General
88

    
89
def _ignore(items):
90
    '''Used to "comment out" an XML subtree'''
91
    return None
92
funcs['_ignore'] = _ignore
93

    
94
#### Conditionals
95

    
96
def _eq(items):
97
    items = dict(items)
98
    try:
99
        left = items['left']
100
        right = items['right']
101
    except KeyError: return '' # a value was None
102
    return util.bool2str(left == right)
103
funcs['_eq'] = _eq
104

    
105
def _if(items):
106
    items = dict(items)
107
    try:
108
        cond = items['cond']
109
        then = items['then']
110
    except KeyError, e: raise SyntaxError(e)
111
    else_ = items.get('else', None)
112
    cond = bool(cast(strings.ustr, cond))
113
    if cond: return then
114
    else: return else_
115
funcs['_if'] = _if
116

    
117
#### Combining values
118

    
119
def _alt(items):
120
    items = list(items)
121
    items.sort()
122
    try: return items[0][1] # value of lowest-numbered item
123
    except IndexError: return None # input got removed by e.g. FormatException
124
funcs['_alt'] = _alt
125

    
126
def _merge(items):
127
    items = list(conv_items(strings.ustr, items))
128
        # get *once* from iter, check types
129
    items.sort()
130
    return maps.merge_values(*[v for k, v in items])
131
funcs['_merge'] = _merge
132

    
133
def _label(items):
134
    items = dict(conv_items(strings.ustr, items))
135
        # get *once* from iter, check types
136
    try:
137
        label = items['label']
138
        value = items['value']
139
    except KeyError, e: raise SyntaxError(e)
140
    return label+': '+value
141
funcs['_label'] = _label
142

    
143
#### Transforming values
144

    
145
types_by_name = {None: strings.ustr, 'str': strings.ustr, 'float': float}
146

    
147
def _nullIf(items):
148
    items = dict(conv_items(strings.ustr, items))
149
    try: null = items['null']
150
    except KeyError, e: raise SyntaxError(e)
151
    value = items.get('value', None)
152
    type_str = items.get('type', None)
153
    
154
    try: type_ = types_by_name[type_str]
155
    except KeyError, e: raise SyntaxError(e)
156
    null = type_(null)
157
    
158
    try: return util.none_if(value, null)
159
    except ValueError: return value # value not convertible, so can't equal null
160
funcs['_nullIf'] = _nullIf
161

    
162
def repl(repls, value):
163
    '''Raises error if value not in map and no special '*' entry
164
    @param repls dict repl:with
165
        repl "*" means all other input values
166
        with "*" means keep input value the same
167
        with "" means ignore input value
168
    '''
169
    try: new_value = repls[value]
170
    except KeyError, e:
171
        # Save traceback right away in case another exception raised
172
        fe = FormatException(e) 
173
        try: new_value = repls['*']
174
        except KeyError: raise fe
175
    if new_value == '*': new_value = value # '*' means keep input value the same
176
    return new_value
177

    
178
def _map(items):
179
    '''See repl()
180
    @param items
181
        <last_entry> Value
182
        <other_entries> name=value Mappings. Special values: See repl() repls.
183
    '''
184
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
185
    value = pop_value(items)
186
    if value == None: return None # input is empty
187
    return util.none_if(repl(dict(items), value), u'') # empty value means None
188
funcs['_map'] = _map
189

    
190
def _replace(items):
191
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
192
    value = pop_value(items)
193
    if value == None: return None # input is empty
194
    try:
195
        for repl, with_ in items:
196
            if re.match(r'^\w+$', repl):
197
                repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
198
            value = re.sub(repl, with_, value)
199
    except sre_constants.error, e: raise SyntaxError(e)
200
    return util.none_if(value.strip(), u'') # empty strings always mean None
201
funcs['_replace'] = _replace
202

    
203
#### Quantities
204

    
205
def _units(items):
206
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
207
    value = pop_value(items)
208
    if value == None: return None # input is empty
209
    
210
    quantity = units.str2quantity(value)
211
    try:
212
        for action, units_ in items:
213
            units_ = util.none_if(units_, u'')
214
            if action == 'default': units.set_default_units(quantity, units_)
215
            elif action == 'to':
216
                try: quantity = units.convert(quantity, units_)
217
                except ValueError, e: raise FormatException(e)
218
            else: raise SyntaxError(ValueError('Invalid action: '+action))
219
    except units.MissingUnitsException, e: raise FormatException(e)
220
    return units.quantity2str(quantity)
221
funcs['_units'] = _units
222

    
223
def parse_range(str_, range_sep='-'):
224
    default = (str_, None)
225
    start, sep, end = str_.partition(range_sep)
226
    if sep == '': return default # not a range
227
    if start == '' and range_sep == '-': return default # negative number
228
    return tuple(d.strip() for d in (start, end))
229

    
230
def _rangeStart(items):
231
    items = dict(conv_items(strings.ustr, items))
232
    try: value = items['value']
233
    except KeyError: return None # input is empty
234
    return parse_range(value)[0]
235
funcs['_rangeStart'] = _rangeStart
236

    
237
def _rangeEnd(items):
238
    items = dict(conv_items(strings.ustr, items))
239
    try: value = items['value']
240
    except KeyError: return None # input is empty
241
    return parse_range(value)[1]
242
funcs['_rangeEnd'] = _rangeEnd
243

    
244
def _range(items):
245
    items = dict(conv_items(float, items))
246
    from_ = items.get('from', None)
247
    to = items.get('to', None)
248
    if from_ == None or to == None: return None
249
    return str(to - from_)
250
funcs['_range'] = _range
251

    
252
def _avg(items):
253
    count = 0
254
    sum_ = 0.
255
    for name, value in conv_items(float, items):
256
        count += 1
257
        sum_ += value
258
    if count == 0: return None # input is empty
259
    else: return str(sum_/count)
260
funcs['_avg'] = _avg
261

    
262
class CvException(Exception):
263
    def __init__(self):
264
        Exception.__init__(self, 'CV (coefficient of variation) values are only'
265
            ' allowed for ratio scale data '
266
            '(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
267

    
268
def _noCV(items):
269
    try: name, value = items.next()
270
    except StopIteration: return None
271
    if re.match('^(?i)CV *\d+$', value): raise FormatException(CvException())
272
    return value
273
funcs['_noCV'] = _noCV
274

    
275
#### Dates
276

    
277
def _date(items):
278
    items = dict(conv_items(strings.ustr, items))
279
        # get *once* from iter, check types
280
    try: str_ = items['date']
281
    except KeyError:
282
        # Year is required
283
        try: items['year']
284
        except KeyError, e:
285
            if items == {}: return None # entire date is empty
286
            else: raise FormatException(e)
287
        
288
        # Convert month name to number
289
        try: month = items['month']
290
        except KeyError: pass
291
        else:
292
            if not month.isdigit(): # month is name
293
                try: items['month'] = str(dates.strtotime(month).month)
294
                except ValueError, e: raise FormatException(e)
295
        
296
        items = dict(conv_items(format.str2int, items.iteritems()))
297
        items.setdefault('month', 1)
298
        items.setdefault('day', 1)
299
        
300
        for try_num in xrange(2):
301
            try:
302
                date = datetime.date(**items)
303
                break
304
            except ValueError, e:
305
                if try_num > 0: raise FormatException(e)
306
                    # exception still raised after retry
307
                msg = strings.ustr(e)
308
                if msg == 'month must be in 1..12': # try swapping month and day
309
                    items['month'], items['day'] = items['day'], items['month']
310
                else: raise FormatException(e)
311
    else:
312
        try: year = float(str_)
313
        except ValueError:
314
            try: date = dates.strtotime(str_)
315
            except ImportError: return str_
316
            except ValueError, e: raise FormatException(e)
317
        else: date = (datetime.date(int(year), 1, 1) +
318
            datetime.timedelta(round((year % 1.)*365)))
319
    try: return dates.strftime('%Y-%m-%d', date)
320
    except ValueError, e: raise FormatException(e)
321
funcs['_date'] = _date
322

    
323
def _dateRangeStart(items):
324
    items = dict(conv_items(strings.ustr, items))
325
    try: value = items['value']
326
    except KeyError: return None # input is empty
327
    return dates.parse_date_range(value)[0]
328
funcs['_dateRangeStart'] = _dateRangeStart
329

    
330
def _dateRangeEnd(items):
331
    items = dict(conv_items(strings.ustr, items))
332
    try: value = items['value']
333
    except KeyError: return None # input is empty
334
    return dates.parse_date_range(value)[1]
335
funcs['_dateRangeEnd'] = _dateRangeEnd
336

    
337
#### Names
338

    
339
_name_parts_slices_items = [
340
    ('first', slice(None, 1)),
341
    ('middle', slice(1, -1)),
342
    ('last', slice(-1, None)),
343
]
344
name_parts_slices = dict(_name_parts_slices_items)
345
name_parts = [name for name, slice_ in _name_parts_slices_items]
346

    
347
def _name(items):
348
    items = dict(items)
349
    parts = []
350
    for part in name_parts:
351
        if part in items: parts.append(items[part])
352
    return ' '.join(parts)
353
funcs['_name'] = _name
354

    
355
def _namePart(items):
356
    out_items = []
357
    for part, value in items:
358
        try: slice_ = name_parts_slices[part]
359
        except KeyError, e: raise SyntaxError(e)
360
        out_items.append((part, ' '.join(value.split(' ')[slice_])))
361
    return _name(out_items)
362
funcs['_namePart'] = _namePart
363

    
364
#### Angles
365

    
366
def _compass(items):
367
    '''Converts a compass direction (N, NE, NNE, etc.) into a degree heading'''
368
    items = dict(conv_items(strings.ustr, items))
369
    try: value = items['value']
370
    except KeyError: return None # input is empty
371
    
372
    if not value.isupper(): return value # pass through other coordinate formats
373
    try: return util.cast(str, angles.compass2heading(value)) # ignore None
374
    except KeyError, e: raise FormatException(e)
375
funcs['_compass'] = _compass
376

    
377
#### Paths
378

    
379
def _simplifyPath(items):
380
    items = dict(items)
381
    try:
382
        next = cast(strings.ustr, items['next'])
383
        require = cast(strings.ustr, items['require'])
384
        root = items['path']
385
    except KeyError, e: raise SyntaxError(e)
386
    
387
    node = root
388
    while node != None:
389
        new_node = xpath.get_1(node, next, allow_rooted=False)
390
        if xpath.get_1(node, require, allow_rooted=False) == None: # empty elem
391
            xml_dom.replace(node, new_node) # remove current elem
392
            if node is root: root = new_node # also update root
393
        node = new_node
394
    return root
395
funcs['_simplifyPath'] = _simplifyPath
(30-30/33)