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 SyntaxException(exc.ExceptionWithCause):
22
    def __init__(self, cause):
23
        exc.ExceptionWithCause.__init__(self, 'Invalid XML function syntax: '
24
            +exc.str_(cause))
25

    
26
class FormatException(SyntaxException): pass
27

    
28
##### Functions
29

    
30
funcs = {}
31

    
32
def process(node, on_error=exc.raise_):
33
    for child in xml_dom.NodeElemIter(node): process(child, on_error)
34
    name = node.tagName
35
    if name.startswith('_') and name in funcs:
36
        try:
37
            value = funcs[name](xml_dom.NodeTextEntryIter(node))
38
            xml_dom.replace_with_text(node, value)
39
        except Exception, e: # also catch XML func internal errors
40
            # Save in case another exception raised, overwriting sys.exc_info()
41
            exc.add_traceback(e)
42
            str_ = strings.ustr(node)
43
            exc.add_msg(e, 'function:\n'+str_)
44
            xml_dom.replace(node, node.ownerDocument.createComment(
45
                '\n'+term.emph_multiline(str_).replace('--','-')))
46
                # comments can't contain '--'
47
            on_error(e)
48

    
49
def map_items(func, items):
50
    return [(name, func(value)) for name, value in items]
51

    
52
def cast(type_, val):
53
    '''Throws SyntaxException if can't cast'''
54
    try: return type_(val)
55
    except ValueError, e: raise SyntaxException(e)
56

    
57
def conv_items(type_, items):
58
    return map_items(lambda val: cast(type_, val),
59
        xml_dom.TextEntryOnlyIter(items))
60

    
61
def pop_value(items):
62
    try: last = items.pop() # last entry contains value
63
    except IndexError: return None # input is empty and no actions
64
    if last[0] != 'value': return None # input is empty
65
    return last[1]
66

    
67
##### XML functions
68

    
69
# Function names must start with _ to avoid collisions with real tags
70
# Functions take arguments (items)
71

    
72
#### General
73

    
74
def _ignore(items):
75
    '''Used to "comment out" an XML subtree'''
76
    return None
77
funcs['_ignore'] = _ignore
78

    
79
#### Conditionals
80

    
81
def _eq(items):
82
    items = dict(items)
83
    try:
84
        left = items['left']
85
        right = items['right']
86
    except KeyError: return '' # a value was None
87
    return util.bool2str(left == right)
88
funcs['_eq'] = _eq
89

    
90
def _if(items):
91
    items = dict(items)
92
    try:
93
        cond = items['cond']
94
        then = items['then']
95
    except KeyError, e: raise SyntaxException(e)
96
    else_ = items.get('else', None)
97
    cond = bool(cast(strings.ustr, cond))
98
    if cond: return then
99
    else: return else_
100
funcs['_if'] = _if
101

    
102
#### Combining values
103

    
104
def _alt(items):
105
    items = list(items)
106
    items.sort()
107
    try: return items[0][1] # value of lowest-numbered item
108
    except IndexError: return None # input got removed by e.g. SyntaxException
109
funcs['_alt'] = _alt
110

    
111
def _merge(items):
112
    items = list(conv_items(strings.ustr, items))
113
        # get *once* from iter, check types
114
    items.sort()
115
    return maps.merge_values(*[v for k, v in items])
116
funcs['_merge'] = _merge
117

    
118
def _label(items):
119
    items = dict(conv_items(strings.ustr, items))
120
        # get *once* from iter, check types
121
    try:
122
        label = items['label']
123
        value = items['value']
124
    except KeyError, e: raise SyntaxException(e)
125
    return label+': '+value
126
funcs['_label'] = _label
127

    
128
#### Transforming values
129

    
130
types_by_name = {None: strings.ustr, 'str': strings.ustr, 'float': float}
131

    
132
def _nullIf(items):
133
    items = dict(conv_items(strings.ustr, items))
134
    try: null = items['null']
135
    except KeyError, e: raise SyntaxException(e)
136
    value = items.get('value', None)
137
    type_str = items.get('type', None)
138
    
139
    try: type_ = types_by_name[type_str]
140
    except KeyError, e: raise SyntaxException(e)
141
    null = type_(null)
142
    
143
    try: return util.none_if(value, null)
144
    except ValueError: return value # value not convertible, so can't equal null
145
funcs['_nullIf'] = _nullIf
146

    
147
def repl(repls, value):
148
    '''Raises error if value not in map and no special '*' entry
149
    @param repls dict repl:with
150
        repl "*" means all other input values
151
        with "*" means keep input value the same
152
        with "" means ignore input value
153
    '''
154
    try: new_value = repls[value]
155
    except KeyError, e:
156
        # Save traceback right away in case another exception raised
157
        se = SyntaxException(e) 
158
        try: new_value = repls['*']
159
        except KeyError: raise se
160
    if new_value == '*': new_value = value # '*' means keep input value the same
161
    return new_value
162

    
163
def _map(items):
164
    '''See repl()
165
    @param items
166
        <last_entry> Value
167
        <other_entries> name=value Mappings. Special values: See repl() repls.
168
    '''
169
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
170
    value = pop_value(items)
171
    if value == None: return None # input is empty
172
    return util.none_if(repl(dict(items), value), u'') # empty value means None
173
funcs['_map'] = _map
174

    
175
def _replace(items):
176
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
177
    value = pop_value(items)
178
    if value == None: return None # input is empty
179
    try:
180
        for repl, with_ in items:
181
            if re.match(r'^\w+$', repl):
182
                repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
183
            value = re.sub(repl, with_, value)
184
    except sre_constants.error, e: raise SyntaxException(e)
185
    return util.none_if(value, u'') # empty strings always mean None
186
funcs['_replace'] = _replace
187

    
188
#### Quantities
189

    
190
def _units(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
    
195
    quantity = units.str2quantity(value)
196
    try:
197
        for action, units_ in items:
198
            units_ = util.none_if(units_, u'')
199
            if action == 'default': units.set_default_units(quantity, units_)
200
            elif action == 'to':
201
                try: quantity = units.convert(quantity, units_)
202
                except ValueError, e: raise SyntaxException(e)
203
            else: raise SyntaxException(ValueError('Invalid action: '+action))
204
    except units.MissingUnitsException, e: raise SyntaxException(e)
205
    return units.quantity2str(quantity)
206
funcs['_units'] = _units
207

    
208
def parse_range(str_, range_sep='-'):
209
    default = (str_, None)
210
    start, sep, end = str_.partition(range_sep)
211
    if sep == '': return default # not a range
212
    if start == '' and range_sep == '-': return default # negative number
213
    return tuple(d.strip() for d in (start, end))
214

    
215
def _rangeStart(items):
216
    items = dict(conv_items(strings.ustr, items))
217
    try: value = items['value']
218
    except KeyError: return None # input is empty
219
    return parse_range(value)[0]
220
funcs['_rangeStart'] = _rangeStart
221

    
222
def _rangeEnd(items):
223
    items = dict(conv_items(strings.ustr, items))
224
    try: value = items['value']
225
    except KeyError: return None # input is empty
226
    return parse_range(value)[1]
227
funcs['_rangeEnd'] = _rangeEnd
228

    
229
def _range(items):
230
    items = dict(conv_items(float, items))
231
    from_ = items.get('from', None)
232
    to = items.get('to', None)
233
    if from_ == None or to == None: return None
234
    return str(to - from_)
235
funcs['_range'] = _range
236

    
237
def _avg(items):
238
    count = 0
239
    sum_ = 0.
240
    for name, value in conv_items(float, items):
241
        count += 1
242
        sum_ += value
243
    if count == 0: return None # input is empty
244
    else: return str(sum_/count)
245
funcs['_avg'] = _avg
246

    
247
class CvException(Exception):
248
    def __init__(self):
249
        Exception.__init__(self, 'CV (coefficient of variation) values are only'
250
            ' allowed for ratio scale data '
251
            '(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')
252

    
253
def _noCV(items):
254
    try: name, value = items.next()
255
    except StopIteration: return None
256
    if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
257
    return value
258
funcs['_noCV'] = _noCV
259

    
260
#### Dates
261

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

    
308
def _dateRangeStart(items):
309
    items = dict(conv_items(strings.ustr, items))
310
    try: value = items['value']
311
    except KeyError: return None # input is empty
312
    return dates.parse_date_range(value)[0]
313
funcs['_dateRangeStart'] = _dateRangeStart
314

    
315
def _dateRangeEnd(items):
316
    items = dict(conv_items(strings.ustr, items))
317
    try: value = items['value']
318
    except KeyError: return None # input is empty
319
    return dates.parse_date_range(value)[1]
320
funcs['_dateRangeEnd'] = _dateRangeEnd
321

    
322
#### Names
323

    
324
_name_parts_slices_items = [
325
    ('first', slice(None, 1)),
326
    ('middle', slice(1, -1)),
327
    ('last', slice(-1, None)),
328
]
329
name_parts_slices = dict(_name_parts_slices_items)
330
name_parts = [name for name, slice_ in _name_parts_slices_items]
331

    
332
def _name(items):
333
    items = dict(items)
334
    parts = []
335
    for part in name_parts:
336
        if part in items: parts.append(items[part])
337
    return ' '.join(parts)
338
funcs['_name'] = _name
339

    
340
def _namePart(items):
341
    out_items = []
342
    for part, value in items:
343
        try: slice_ = name_parts_slices[part]
344
        except KeyError, e: raise SyntaxException(e)
345
        out_items.append((part, ' '.join(value.split(' ')[slice_])))
346
    return _name(out_items)
347
funcs['_namePart'] = _namePart
348

    
349
#### Angles
350

    
351
def _compass(items):
352
    '''Converts a compass direction (N, NE, NNE, etc.) into a degree heading'''
353
    items = dict(conv_items(strings.ustr, items))
354
    try: value = items['value']
355
    except KeyError: return None # input is empty
356
    
357
    if not value.isupper(): return value # pass through other coordinate formats
358
    try: return util.cast(str, angles.compass2heading(value)) # ignore None
359
    except KeyError, e: raise FormatException(e)
360
funcs['_compass'] = _compass
361

    
362
#### Paths
363

    
364
def _simplifyPath(items):
365
    items = dict(items)
366
    try:
367
        next = cast(strings.ustr, items['next'])
368
        require = cast(strings.ustr, items['require'])
369
        root = items['path']
370
    except KeyError, e: raise SyntaxException(e)
371
    
372
    node = root
373
    while node != None:
374
        new_node = xpath.get_1(node, next, allow_rooted=False)
375
        if xpath.get_1(node, require, allow_rooted=False) == None: # empty elem
376
            xml_dom.replace(node, new_node) # remove current elem
377
            if node is root: root = new_node # also update root
378
        node = new_node
379
    return root
380
funcs['_simplifyPath'] = _simplifyPath
(18-18/20)