Revision 2557
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/xml_func.py | ||
---|---|---|
52 | 52 |
|
53 | 53 |
funcs = {} |
54 | 54 |
|
55 |
structural_funcs = set() |
|
56 |
|
|
55 | 57 |
##### Public functions |
56 | 58 |
|
57 | 59 |
def is_func_name(name): |
... | ... | |
89 | 91 |
|
90 | 92 |
def strip(node, preserve=set()): |
91 | 93 |
'''Replaces every XML function with its last parameter (which is usually its |
92 |
value), except for _ignore, which is removed completely
|
|
94 |
value), except for structural functions, which are evaluated by process().
|
|
93 | 95 |
@param preserve set(str...) XML functions not to remove. |
94 | 96 |
* container can be any iterable type |
95 | 97 |
''' |
96 | 98 |
preserve = set(preserve) |
97 | 99 |
|
98 |
for child in xml_dom.NodeElemIter(node): strip(child, preserve) |
|
99 | 100 |
name = node.tagName |
100 |
if is_xml_func_name(name) and name not in preserve: |
|
101 |
if name == '_ignore': value = None |
|
102 |
else: value = pop_value(list(xml_dom.NodeTextEntryIter(node)), None) |
|
103 |
xml_dom.replace_with_text(node, value) |
|
101 |
is_func = is_xml_func_name(name) and name not in preserve |
|
102 |
if is_func and name in structural_funcs: process(node) |
|
103 |
else: |
|
104 |
for child in xml_dom.NodeElemIter(node): strip(child, preserve) |
|
105 |
if is_func: |
|
106 |
value = pop_value(list(xml_dom.NodeTextEntryIter(node)), None) |
|
107 |
xml_dom.replace_with_text(node, value) |
|
104 | 108 |
|
105 | 109 |
##### XML functions |
106 | 110 |
|
107 | 111 |
# Function names must start with _ to avoid collisions with real tags |
108 | 112 |
# Functions take arguments (items) |
109 | 113 |
|
110 |
#### General
|
|
114 |
#### Structural
|
|
111 | 115 |
|
112 | 116 |
def _ignore(items, node): |
113 | 117 |
'''Used to "comment out" an XML subtree''' |
114 | 118 |
return None |
115 | 119 |
funcs['_ignore'] = _ignore |
120 |
structural_funcs.add('_ignore') |
|
116 | 121 |
|
117 | 122 |
def _ref(items, node): |
118 | 123 |
'''Used to retrieve a value from another XML node |
... | ... | |
129 | 134 |
+str(addr))) |
130 | 135 |
return value |
131 | 136 |
funcs['_ref'] = _ref |
137 |
structural_funcs.add('_ref') |
|
132 | 138 |
|
133 | 139 |
#### Conditionals |
134 | 140 |
|
Also available in: Unified diff
xml_func.py: strip(): Evaluate structural functions like _ignore and _ref by process() instead of removing them. Store structural functions' names in structural_funcs module var. This ensures that _ref targets are still expanded in column-based import.