Revision 1609
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xml_func.py | ||
---|---|---|
50 | 50 |
return [(name, func(value)) for name, value in items] |
51 | 51 |
|
52 | 52 |
def cast(type_, val): |
53 |
'''Throws SyntaxException if can't cast'''
|
|
53 |
'''Throws FormatException if can't cast'''
|
|
54 | 54 |
try: return type_(val) |
55 |
except ValueError, e: raise SyntaxException(e)
|
|
55 |
except ValueError, e: raise FormatException(e)
|
|
56 | 56 |
|
57 | 57 |
def conv_items(type_, items): |
58 | 58 |
return map_items(lambda val: cast(type_, val), |
... | ... | |
105 | 105 |
items = list(items) |
106 | 106 |
items.sort() |
107 | 107 |
try: return items[0][1] # value of lowest-numbered item |
108 |
except IndexError: return None # input got removed by e.g. SyntaxException
|
|
108 |
except IndexError: return None # input got removed by e.g. FormatException
|
|
109 | 109 |
funcs['_alt'] = _alt |
110 | 110 |
|
111 | 111 |
def _merge(items): |
... | ... | |
154 | 154 |
try: new_value = repls[value] |
155 | 155 |
except KeyError, e: |
156 | 156 |
# Save traceback right away in case another exception raised |
157 |
se = SyntaxException(e)
|
|
157 |
fe = FormatException(e)
|
|
158 | 158 |
try: new_value = repls['*'] |
159 |
except KeyError: raise se
|
|
159 |
except KeyError: raise fe
|
|
160 | 160 |
if new_value == '*': new_value = value # '*' means keep input value the same |
161 | 161 |
return new_value |
162 | 162 |
|
... | ... | |
199 | 199 |
if action == 'default': units.set_default_units(quantity, units_) |
200 | 200 |
elif action == 'to': |
201 | 201 |
try: quantity = units.convert(quantity, units_) |
202 |
except ValueError, e: raise SyntaxException(e)
|
|
202 |
except ValueError, e: raise FormatException(e)
|
|
203 | 203 |
else: raise SyntaxException(ValueError('Invalid action: '+action)) |
204 |
except units.MissingUnitsException, e: raise SyntaxException(e)
|
|
204 |
except units.MissingUnitsException, e: raise FormatException(e)
|
|
205 | 205 |
return units.quantity2str(quantity) |
206 | 206 |
funcs['_units'] = _units |
207 | 207 |
|
... | ... | |
253 | 253 |
def _noCV(items): |
254 | 254 |
try: name, value = items.next() |
255 | 255 |
except StopIteration: return None |
256 |
if re.match('^(?i)CV *\d+$', value): raise SyntaxException(CvException())
|
|
256 |
if re.match('^(?i)CV *\d+$', value): raise FormatException(CvException())
|
|
257 | 257 |
return value |
258 | 258 |
funcs['_noCV'] = _noCV |
259 | 259 |
|
... | ... | |
268 | 268 |
try: items['year'] |
269 | 269 |
except KeyError, e: |
270 | 270 |
if items == {}: return None # entire date is empty |
271 |
else: raise SyntaxException(e)
|
|
271 |
else: raise FormatException(e)
|
|
272 | 272 |
|
273 | 273 |
# Convert month name to number |
274 | 274 |
try: month = items['month'] |
... | ... | |
276 | 276 |
else: |
277 | 277 |
if not month.isdigit(): # month is name |
278 | 278 |
try: items['month'] = str(dates.strtotime(month).month) |
279 |
except ValueError, e: raise SyntaxException(e)
|
|
279 |
except ValueError, e: raise FormatException(e)
|
|
280 | 280 |
|
281 | 281 |
items = dict(conv_items(format.str2int, items.iteritems())) |
282 | 282 |
items.setdefault('month', 1) |
... | ... | |
287 | 287 |
date = datetime.date(**items) |
288 | 288 |
break |
289 | 289 |
except ValueError, e: |
290 |
if try_num > 0: raise SyntaxException(e)
|
|
290 |
if try_num > 0: raise FormatException(e)
|
|
291 | 291 |
# exception still raised after retry |
292 | 292 |
msg = strings.ustr(e) |
293 | 293 |
if msg == 'month must be in 1..12': # try swapping month and day |
294 | 294 |
items['month'], items['day'] = items['day'], items['month'] |
295 |
else: raise SyntaxException(e)
|
|
295 |
else: raise FormatException(e)
|
|
296 | 296 |
else: |
297 | 297 |
try: year = float(str_) |
298 | 298 |
except ValueError: |
299 | 299 |
try: date = dates.strtotime(str_) |
300 | 300 |
except ImportError: return str_ |
301 |
except ValueError, e: raise SyntaxException(e)
|
|
301 |
except ValueError, e: raise FormatException(e)
|
|
302 | 302 |
else: date = (datetime.date(int(year), 1, 1) + |
303 | 303 |
datetime.timedelta(round((year % 1.)*365))) |
304 | 304 |
try: return dates.strftime('%Y-%m-%d', date) |
Also available in: Unified diff
xml_func.py: Changed SyntaxException to FormatException where the error was with the input data format rather than the mapping syntax