Project

General

Profile

« Previous | Next » 

Revision 1219

xml_func.py: Added _map and _replace

View differences:

lib/xml_func.py
2 2

  
3 3
import datetime
4 4
import re
5
import sre_constants
5 6

  
6 7
import dates
7 8
import exc
......
83 84
        null = items['null']
84 85
        value = items['value']
85 86
    except KeyError, e: raise SyntaxException(e)
86
    else:
87
        type_str = items.get('type', None)
88
        type_ = str
89
        if type_str == 'float': type_ = float
90
        return util.none_if(value, type_(null))
87
    type_str = items.get('type', None)
88
    type_ = str
89
    if type_str == 'float': type_ = float
90
    return util.none_if(value, type_(null))
91 91
funcs['_nullIf'] = _nullIf
92 92

  
93
def _map(items):
94
    items = conv_items(str, items) # get *once* from iter and check types
95
    try: value = items.pop()[1] # value is last entry's value
96
    except IndexError, e: raise SyntaxException(e)
97
    map_ = dict(items)
98
    try: return map_[value]
99
    except KeyError: return value
100
funcs['_map'] = _map
101

  
102
def _replace(items):
103
    items = conv_items(str, items) # get *once* from iter and check types
104
    try: value = items.pop() # value is last entry
105
    except IndexError, e: raise SyntaxException(e)
106
    try:
107
        for repl, with_ in items:
108
            if re.match(r'^\w+$', repl):
109
                repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
110
            value = re.sub(repl, with_, value)
111
    except sre_constants.error, e: raise SyntaxException(e)
112
    return value
113
funcs['_replace'] = _replace
114

  
93 115
def _range(items):
94 116
    items = dict(conv_items(float, items))
95 117
    from_ = items.get('from', None)
......
164 186
    for part, value in items:
165 187
        try: slice_ = name_parts_slices[part]
166 188
        except KeyError, e: raise SyntaxException(e)
167
        else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
189
        out_items.append((part, ' '.join(value.split(' ')[slice_])))
168 190
    return _name(out_items)
169 191
funcs['_namePart'] = _namePart
170 192

  

Also available in: Unified diff