Project

General

Profile

« Previous | Next » 

Revision 328

xml_func.py: _name func doesn't require all name parts to be set. _namePart func throws SyntaxException for invalid name parts.

View differences:

lib/xml_func.py
62 62
        date = datetime.date(year, month, day)
63 63
    return date.strftime('%Y-%m-%d')
64 64

  
65
_name_parts_slices_items = [
66
    ('first', slice(None, 1)),
67
    ('middle', slice(1, -1)),
68
    ('last', slice(-1, None)),
69
]
70
name_parts_slices = dict(_name_parts_slices_items)
71
name_parts = [name for name, slice_ in _name_parts_slices_items]
72

  
65 73
def name(items):
66 74
    items = dict(items)
67
    return ' '.join([items['first'], items['last']])
68

  
69
def namePart(items):
70
    items = dict(items)
71
    def to_parts(name): return items[name].split(' ')
72 75
    parts = []
73
    if 'first' in items: parts += to_parts('first')[:1]
74
    if 'last' in items: parts += to_parts('last')[-1:]
75
    if 'middle' in items: parts += to_parts('middle')[1:-1]
76
    for part in name_parts:
77
        if part in items: parts.append(items[part])
76 78
    return ' '.join(parts)
77 79

  
80
def name_part(items):
81
    out_items = []
82
    for part, value in items:
83
        try: slice_ = name_parts_slices[part]
84
        except KeyError, e: raise SyntaxException(e)
85
        else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
86
    return name(out_items)
87

  
78 88
# Function names must start with _ to avoid collisions with real tags
79 89
# Functions take arguments (items)
80 90
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
81
    '_name': name, '_namePart': namePart}
91
    '_name': name, '_namePart': name_part}
82 92

  
83 93
def process(node):
84 94
    name = node.tagName

Also available in: Unified diff