Revision 1612
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xml_func.py | ||
---|---|---|
18 | 18 |
|
19 | 19 |
##### Exceptions |
20 | 20 |
|
21 |
class SyntaxException(exc.ExceptionWithCause):
|
|
21 |
class SyntaxError(exc.ExceptionWithCause):
|
|
22 | 22 |
def __init__(self, cause): |
23 | 23 |
exc.ExceptionWithCause.__init__(self, 'Invalid XML function syntax', |
24 | 24 |
cause) |
25 | 25 |
|
26 |
class FormatException(SyntaxException): pass
|
|
26 |
class FormatException(SyntaxError): pass
|
|
27 | 27 |
|
28 | 28 |
##### Functions |
29 | 29 |
|
... | ... | |
92 | 92 |
try: |
93 | 93 |
cond = items['cond'] |
94 | 94 |
then = items['then'] |
95 |
except KeyError, e: raise SyntaxException(e)
|
|
95 |
except KeyError, e: raise SyntaxError(e)
|
|
96 | 96 |
else_ = items.get('else', None) |
97 | 97 |
cond = bool(cast(strings.ustr, cond)) |
98 | 98 |
if cond: return then |
... | ... | |
121 | 121 |
try: |
122 | 122 |
label = items['label'] |
123 | 123 |
value = items['value'] |
124 |
except KeyError, e: raise SyntaxException(e)
|
|
124 |
except KeyError, e: raise SyntaxError(e)
|
|
125 | 125 |
return label+': '+value |
126 | 126 |
funcs['_label'] = _label |
127 | 127 |
|
... | ... | |
132 | 132 |
def _nullIf(items): |
133 | 133 |
items = dict(conv_items(strings.ustr, items)) |
134 | 134 |
try: null = items['null'] |
135 |
except KeyError, e: raise SyntaxException(e)
|
|
135 |
except KeyError, e: raise SyntaxError(e)
|
|
136 | 136 |
value = items.get('value', None) |
137 | 137 |
type_str = items.get('type', None) |
138 | 138 |
|
139 | 139 |
try: type_ = types_by_name[type_str] |
140 |
except KeyError, e: raise SyntaxException(e)
|
|
140 |
except KeyError, e: raise SyntaxError(e)
|
|
141 | 141 |
null = type_(null) |
142 | 142 |
|
143 | 143 |
try: return util.none_if(value, null) |
... | ... | |
181 | 181 |
if re.match(r'^\w+$', repl): |
182 | 182 |
repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word |
183 | 183 |
value = re.sub(repl, with_, value) |
184 |
except sre_constants.error, e: raise SyntaxException(e)
|
|
184 |
except sre_constants.error, e: raise SyntaxError(e)
|
|
185 | 185 |
return util.none_if(value, u'') # empty strings always mean None |
186 | 186 |
funcs['_replace'] = _replace |
187 | 187 |
|
... | ... | |
200 | 200 |
elif action == 'to': |
201 | 201 |
try: quantity = units.convert(quantity, units_) |
202 | 202 |
except ValueError, e: raise FormatException(e) |
203 |
else: raise SyntaxException(ValueError('Invalid action: '+action))
|
|
203 |
else: raise SyntaxError(ValueError('Invalid action: '+action))
|
|
204 | 204 |
except units.MissingUnitsException, e: raise FormatException(e) |
205 | 205 |
return units.quantity2str(quantity) |
206 | 206 |
funcs['_units'] = _units |
... | ... | |
341 | 341 |
out_items = [] |
342 | 342 |
for part, value in items: |
343 | 343 |
try: slice_ = name_parts_slices[part] |
344 |
except KeyError, e: raise SyntaxException(e)
|
|
344 |
except KeyError, e: raise SyntaxError(e)
|
|
345 | 345 |
out_items.append((part, ' '.join(value.split(' ')[slice_]))) |
346 | 346 |
return _name(out_items) |
347 | 347 |
funcs['_namePart'] = _namePart |
... | ... | |
367 | 367 |
next = cast(strings.ustr, items['next']) |
368 | 368 |
require = cast(strings.ustr, items['require']) |
369 | 369 |
root = items['path'] |
370 |
except KeyError, e: raise SyntaxException(e)
|
|
370 |
except KeyError, e: raise SyntaxError(e)
|
|
371 | 371 |
|
372 | 372 |
node = root |
373 | 373 |
while node != None: |
Also available in: Unified diff
xml_func.py: Renamed SyntaxException to SyntaxError because it's a user error signaling invalid mappings syntax