Revision 3664
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/xml_func.py | ||
---|---|---|
181 | 181 |
else: return else_ |
182 | 182 |
funcs['_if'] = _if |
183 | 183 |
|
184 |
#### Combining values |
|
185 |
|
|
186 |
def _alt(items, node): |
|
187 |
items = list(items) |
|
188 |
items.sort() |
|
189 |
try: return items[0][1] # value of lowest-numbered item |
|
190 |
except IndexError: return None # input got removed by e.g. FormatException |
|
191 |
funcs['_alt'] = _alt |
|
192 |
|
|
193 |
def _merge(items, node): |
|
194 |
items = list(conv_items(strings.ustr, items)) |
|
195 |
# get *once* from iter, check types |
|
196 |
items.sort() |
|
197 |
return maps.merge_values(*[v for k, v in items]) |
|
198 |
funcs['_merge'] = _merge |
|
199 |
|
|
200 |
def _label(items, node): |
|
201 |
items = dict(conv_items(strings.ustr, items)) |
|
202 |
# get *once* from iter, check types |
|
203 |
value = items.get('value', None) |
|
204 |
if value == None: return None # input is empty |
|
205 |
try: label = items['label'] |
|
206 |
except KeyError, e: raise SyntaxError(e) |
|
207 |
return label+': '+value |
|
208 |
funcs['_label'] = _label |
|
209 |
|
|
210 | 184 |
#### Transforming values |
211 | 185 |
|
212 | 186 |
def _collapse(items, node): |
... | ... | |
220 | 194 |
else: return value |
221 | 195 |
funcs['_collapse'] = _collapse |
222 | 196 |
|
223 |
types_by_name = {None: strings.ustr, 'str': strings.ustr, 'float': float} |
|
224 |
|
|
225 |
def _nullIf(items, node): |
|
226 |
items = dict(conv_items(strings.ustr, items)) |
|
227 |
try: null = items['null'] |
|
228 |
except KeyError, e: raise SyntaxError(e) |
|
229 |
value = items.get('value', None) |
|
230 |
type_str = items.get('type', None) |
|
231 |
|
|
232 |
try: type_ = types_by_name[type_str] |
|
233 |
except KeyError, e: raise SyntaxError(e) |
|
234 |
null = type_(null) |
|
235 |
|
|
236 |
try: return util.none_if(value, null) |
|
237 |
except ValueError: return value # value not convertible, so can't equal null |
|
238 |
funcs['_nullIf'] = _nullIf |
|
239 |
|
|
240 | 197 |
def repl(repls, value): |
241 | 198 |
'''Raises error if value not in map and no special '*' entry |
242 | 199 |
@param repls dict repl:with |
... | ... | |
351 | 308 |
return value |
352 | 309 |
funcs['_noCV'] = _noCV |
353 | 310 |
|
354 |
#### Dates |
|
355 |
|
|
356 |
def _date(items, node): |
|
357 |
items = dict(conv_items(strings.ustr, items)) |
|
358 |
# get *once* from iter, check types |
|
359 |
try: str_ = items['date'] |
|
360 |
except KeyError: |
|
361 |
# Year is required |
|
362 |
try: items['year'] |
|
363 |
except KeyError, e: |
|
364 |
if items == {}: return None # entire date is empty |
|
365 |
else: raise FormatException(e) |
|
366 |
|
|
367 |
# Convert month name to number |
|
368 |
try: month = items['month'] |
|
369 |
except KeyError: pass |
|
370 |
else: |
|
371 |
if not month.isdigit(): # month is name |
|
372 |
try: items['month'] = str(dates.strtotime(month).month) |
|
373 |
except ValueError, e: raise FormatException(e) |
|
374 |
|
|
375 |
items = dict(conv_items(format.str2int, items.iteritems())) |
|
376 |
items.setdefault('month', 1) |
|
377 |
items.setdefault('day', 1) |
|
378 |
|
|
379 |
for try_num in xrange(2): |
|
380 |
try: |
|
381 |
date = datetime.date(**items) |
|
382 |
break |
|
383 |
except ValueError, e: |
|
384 |
if try_num > 0: raise FormatException(e) |
|
385 |
# exception still raised after retry |
|
386 |
msg = strings.ustr(e) |
|
387 |
if msg == 'month must be in 1..12': # try swapping month and day |
|
388 |
items['month'], items['day'] = items['day'], items['month'] |
|
389 |
else: raise FormatException(e) |
|
390 |
else: |
|
391 |
try: year = float(str_) |
|
392 |
except ValueError: |
|
393 |
try: date = dates.strtotime(str_) |
|
394 |
except ImportError: return str_ |
|
395 |
except ValueError, e: raise FormatException(e) |
|
396 |
else: date = (datetime.date(int(year), 1, 1) + |
|
397 |
datetime.timedelta(round((year % 1.)*365))) |
|
398 |
try: return dates.strftime('%Y-%m-%d', date) |
|
399 |
except ValueError, e: raise FormatException(e) |
|
400 |
funcs['_date'] = _date |
|
401 |
|
|
402 |
def _dateRangeStart(items, node): |
|
403 |
items = dict(conv_items(strings.ustr, items)) |
|
404 |
try: value = items['value'] |
|
405 |
except KeyError: return None # input is empty |
|
406 |
return dates.parse_date_range(value)[0] |
|
407 |
funcs['_dateRangeStart'] = _dateRangeStart |
|
408 |
|
|
409 |
def _dateRangeEnd(items, node): |
|
410 |
items = dict(conv_items(strings.ustr, items)) |
|
411 |
try: value = items['value'] |
|
412 |
except KeyError: return None # input is empty |
|
413 |
return dates.parse_date_range(value)[1] |
|
414 |
funcs['_dateRangeEnd'] = _dateRangeEnd |
|
415 |
|
|
416 | 311 |
#### Names |
417 | 312 |
|
418 | 313 |
_name_parts_slices_items = [ |
... | ... | |
431 | 326 |
return ' '.join(parts) |
432 | 327 |
funcs['_name'] = _name |
433 | 328 |
|
434 |
def _namePart(items, node): |
|
435 |
items = list(conv_items(strings.ustr, items)) |
|
436 |
out_items = [] |
|
437 |
for part, value in items: |
|
438 |
try: slice_ = name_parts_slices[part] |
|
439 |
except KeyError, e: raise SyntaxError(e) |
|
440 |
out_items.append((part, ' '.join(value.split(' ')[slice_]))) |
|
441 |
return _name(out_items, node) |
|
442 |
funcs['_namePart'] = _namePart |
|
443 |
|
|
444 | 329 |
#### Angles |
445 | 330 |
|
446 | 331 |
def _compass(items, node): |
Also available in: Unified diff
xml_func.py: Removed no longer needed local XML functions that have been translated to SQL functions