Project

General

Profile

« Previous | Next » 

Revision 1581

xml_func.py: XML functions that assume their last argument is a value (_map, etc.): Use new helper function pop_value() to retrieve this value. Return None if value is None because this indicates the input is empty.

View differences:

xml_func.py
57 57
    return map_items(lambda val: cast(type_, val),
58 58
        xml_dom.TextEntryOnlyIter(items))
59 59

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

  
60 66
##### XML functions
61 67

  
62 68
# Function names must start with _ to avoid collisions with real tags
......
147 153
            value "" means ignore input value
148 154
    '''
149 155
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
150
    try: value = items.pop()[1] # last entry contains value
151
    except IndexError, e: raise SyntaxException(e)
156
    value = pop_value(items)
157
    if value == None: return None # input is empty
152 158
    map_ = dict(items)
153 159
    
154 160
    try: new_value = map_[value]
......
163 169

  
164 170
def _replace(items):
165 171
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
166
    try: value = items.pop()[1] # last entry contains value
167
    except IndexError, e: raise SyntaxException(e)
172
    value = pop_value(items)
173
    if value == None: return None # input is empty
168 174
    try:
169 175
        for repl, with_ in items:
170 176
            if re.match(r'^\w+$', repl):
......
178 184

  
179 185
def _units(items):
180 186
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
181
    try: last = items.pop() # last entry contains value
182
    except IndexError: return None # input is empty and no actions
183
    if last[0] != 'value': return None # input is empty
184
    str_ = last[1]
187
    value = pop_value(items)
188
    if value == None: return None # input is empty
185 189
    
186
    quantity = units.str2quantity(str_)
190
    quantity = units.str2quantity(value)
187 191
    try:
188 192
        for action, units_ in items:
189 193
            units_ = util.none_if(units_, u'')

Also available in: Unified diff