1 |
55
|
aaronmk
|
# XPath parsing
|
2 |
21
|
aaronmk
|
|
3 |
55
|
aaronmk
|
import copy
|
4 |
21
|
aaronmk
|
|
5 |
|
|
from Parser import Parser
|
6 |
892
|
aaronmk
|
import util
|
7 |
77
|
aaronmk
|
import xml_dom
|
8 |
21
|
aaronmk
|
|
9 |
1001
|
aaronmk
|
##### Path elements
|
10 |
|
|
|
11 |
21
|
aaronmk
|
class XpathElem:
|
12 |
1002
|
aaronmk
|
def __init__(self, name='', value=None, is_attr=False):
|
13 |
21
|
aaronmk
|
self.name = name
|
14 |
|
|
self.value = value
|
15 |
|
|
self.is_attr = is_attr
|
16 |
97
|
aaronmk
|
self.is_positive = True
|
17 |
94
|
aaronmk
|
self.is_ptr = False
|
18 |
|
|
self.keys = []
|
19 |
|
|
self.attrs = []
|
20 |
78
|
aaronmk
|
self.other_branches = [] # temp implementation for split paths
|
21 |
21
|
aaronmk
|
|
22 |
|
|
def __repr__(self):
|
23 |
|
|
str_ = ''
|
24 |
141
|
aaronmk
|
if not self.is_positive: str_ += '!'
|
25 |
21
|
aaronmk
|
if self.is_attr: str_ += '@'
|
26 |
1004
|
aaronmk
|
if self.name == '': str_ += '""'
|
27 |
|
|
else: str_ += self.name
|
28 |
94
|
aaronmk
|
if self.keys != []: str_ += repr(self.keys)
|
29 |
|
|
if self.attrs != []: str_ += ':'+repr(self.attrs)
|
30 |
|
|
if self.is_ptr: str_ += '->'
|
31 |
|
|
if self.other_branches != []: str_ += '{'+repr(self.other_branches)+'}'
|
32 |
25
|
aaronmk
|
if self.value != None: str_ += '='+repr(self.value)
|
33 |
21
|
aaronmk
|
return str_
|
34 |
|
|
|
35 |
|
|
def __eq__(self, other): return self.__dict__ == other.__dict__
|
36 |
|
|
|
37 |
1002
|
aaronmk
|
empty_elem = XpathElem()
|
38 |
|
|
|
39 |
|
|
def elem_is_empty(elem): return elem == empty_elem
|
40 |
|
|
|
41 |
1004
|
aaronmk
|
def is_self(elem): return elem.name == '' or elem.name == '.'
|
42 |
|
|
|
43 |
|
|
def is_parent(elem): return elem.name == '..'
|
44 |
|
|
|
45 |
1001
|
aaronmk
|
##### Paths
|
46 |
|
|
|
47 |
99
|
aaronmk
|
def is_positive(path): return path[0].is_positive
|
48 |
|
|
|
49 |
1003
|
aaronmk
|
def is_rooted(path): return elem_is_empty(path[0])
|
50 |
|
|
|
51 |
24
|
aaronmk
|
def value(path): return path[-1].value
|
52 |
|
|
|
53 |
22
|
aaronmk
|
def set_value(path, value): path[-1].value = value
|
54 |
|
|
|
55 |
32
|
aaronmk
|
def backward_id(elem):
|
56 |
171
|
aaronmk
|
if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
|
57 |
32
|
aaronmk
|
else: return None
|
58 |
|
|
|
59 |
1001
|
aaronmk
|
instance_level = 1 # root's grandchildren
|
60 |
|
|
|
61 |
|
|
def obj(path):
|
62 |
|
|
obj_path = copy.deepcopy(path[:instance_level+1])
|
63 |
|
|
obj_path[-1].is_ptr = False # prevent pointer w/o target
|
64 |
|
|
return obj_path
|
65 |
|
|
|
66 |
|
|
def set_id(path, id_, has_types=True):
|
67 |
|
|
if has_types: id_level = instance_level
|
68 |
|
|
else: id_level = 0 # root's children
|
69 |
1004
|
aaronmk
|
if is_self(path[0]): id_level += 1 # explicit root element
|
70 |
1001
|
aaronmk
|
path[id_level].keys.append([XpathElem('id', id_, True)])
|
71 |
|
|
|
72 |
|
|
def is_id(path): return path[0].is_attr and path[0].name == 'id'
|
73 |
|
|
|
74 |
|
|
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0])
|
75 |
|
|
|
76 |
|
|
##### Parsing
|
77 |
|
|
|
78 |
170
|
aaronmk
|
def expand_abbr(name, repl):
|
79 |
|
|
before, abbr, after = name.partition('*')
|
80 |
|
|
if abbr != '': name = before+repl+after
|
81 |
|
|
return name
|
82 |
|
|
|
83 |
154
|
aaronmk
|
_cache = {}
|
84 |
|
|
|
85 |
76
|
aaronmk
|
def parse(str_):
|
86 |
154
|
aaronmk
|
try: return _cache[str_]
|
87 |
|
|
except KeyError: pass
|
88 |
|
|
|
89 |
76
|
aaronmk
|
parser = Parser(str_)
|
90 |
21
|
aaronmk
|
|
91 |
76
|
aaronmk
|
def _path():
|
92 |
97
|
aaronmk
|
is_positive = not parser.str_('!')
|
93 |
|
|
|
94 |
21
|
aaronmk
|
tree = []
|
95 |
|
|
while True:
|
96 |
38
|
aaronmk
|
# Split path
|
97 |
76
|
aaronmk
|
if parser.str_('{'):
|
98 |
94
|
aaronmk
|
tree[-1].other_branches = _paths()
|
99 |
76
|
aaronmk
|
parser.str_('}', required=True)
|
100 |
94
|
aaronmk
|
tree += tree[-1].other_branches.pop(0) # use first path for now
|
101 |
38
|
aaronmk
|
break # nothing allowed after split path
|
102 |
36
|
aaronmk
|
|
103 |
754
|
aaronmk
|
elem = XpathElem(is_attr=parser.str_('@'), name=_value())
|
104 |
38
|
aaronmk
|
|
105 |
94
|
aaronmk
|
# Keys used to match nodes
|
106 |
76
|
aaronmk
|
if parser.str_('['):
|
107 |
94
|
aaronmk
|
elem.keys = _paths()
|
108 |
76
|
aaronmk
|
parser.str_(']', required=True)
|
109 |
36
|
aaronmk
|
|
110 |
94
|
aaronmk
|
# Attrs created when no matching node exists
|
111 |
|
|
if parser.str_(':'):
|
112 |
|
|
parser.str_('[', required=True)
|
113 |
|
|
elem.attrs = _paths()
|
114 |
|
|
parser.str_(']', required=True)
|
115 |
|
|
|
116 |
76
|
aaronmk
|
elem.is_ptr = parser.str_('->')
|
117 |
21
|
aaronmk
|
tree.append(elem)
|
118 |
36
|
aaronmk
|
|
119 |
|
|
# Lookahead assertion
|
120 |
76
|
aaronmk
|
if parser.str_('('):
|
121 |
|
|
parser.str_('/', required=True) # next / is inside ()
|
122 |
|
|
path = _path()
|
123 |
|
|
parser.str_(')', required=True)
|
124 |
94
|
aaronmk
|
elem.keys.append(path)
|
125 |
36
|
aaronmk
|
tree += path
|
126 |
|
|
|
127 |
76
|
aaronmk
|
if not parser.str_('/'): break
|
128 |
32
|
aaronmk
|
|
129 |
97
|
aaronmk
|
tree[0].is_positive = is_positive
|
130 |
|
|
|
131 |
|
|
# Value
|
132 |
997
|
aaronmk
|
if parser.str_('='):
|
133 |
|
|
if parser.str_('$'): value = _path()
|
134 |
|
|
else: value = _value()
|
135 |
|
|
set_value(tree, value)
|
136 |
85
|
aaronmk
|
|
137 |
32
|
aaronmk
|
# Expand * abbrs
|
138 |
70
|
aaronmk
|
for i in reversed(xrange(len(tree))):
|
139 |
|
|
elem = tree[i]
|
140 |
171
|
aaronmk
|
if elem.is_ptr: offset = 2
|
141 |
32
|
aaronmk
|
else: offset = 1
|
142 |
170
|
aaronmk
|
try: repl = tree[i+offset].name
|
143 |
171
|
aaronmk
|
except IndexError: pass # no replacement elem
|
144 |
|
|
else: elem.name = expand_abbr(elem.name, repl)
|
145 |
32
|
aaronmk
|
|
146 |
21
|
aaronmk
|
return tree
|
147 |
76
|
aaronmk
|
|
148 |
754
|
aaronmk
|
def _value():
|
149 |
|
|
if parser.str_('"'):
|
150 |
|
|
value = parser.re(r'[^"]*')
|
151 |
|
|
parser.str_('"', required=True)
|
152 |
|
|
else: value = parser.re(r'(?:(?:\w+:)*[\w.*]+)?')
|
153 |
|
|
return value
|
154 |
|
|
|
155 |
94
|
aaronmk
|
def _paths():
|
156 |
|
|
paths = []
|
157 |
|
|
while True:
|
158 |
|
|
paths.append(_path())
|
159 |
|
|
if not parser.str_(','): break
|
160 |
|
|
return paths
|
161 |
|
|
|
162 |
76
|
aaronmk
|
path = _path()
|
163 |
|
|
parser.end()
|
164 |
154
|
aaronmk
|
_cache[str_] = path
|
165 |
76
|
aaronmk
|
return path
|
166 |
21
|
aaronmk
|
|
167 |
1001
|
aaronmk
|
##### Querying/creating XML trees
|
168 |
22
|
aaronmk
|
|
169 |
998
|
aaronmk
|
def get(root, xpath, create=False, last_only=None, limit=1):
|
170 |
141
|
aaronmk
|
'''Warning: The last_only optimization may put data that should be together
|
171 |
|
|
into separate nodes'''
|
172 |
77
|
aaronmk
|
if last_only == None: last_only = create
|
173 |
887
|
aaronmk
|
if limit == None or limit > 1: last_only = False
|
174 |
892
|
aaronmk
|
if util.is_str(xpath): xpath = parse(xpath)
|
175 |
99
|
aaronmk
|
|
176 |
998
|
aaronmk
|
if xpath == []: return [root]
|
177 |
886
|
aaronmk
|
if create and not is_positive(xpath): return []
|
178 |
998
|
aaronmk
|
doc = root.ownerDocument
|
179 |
165
|
aaronmk
|
elem = xpath[0]
|
180 |
|
|
|
181 |
|
|
# Find possible matches
|
182 |
|
|
children = []
|
183 |
|
|
if elem.is_attr:
|
184 |
998
|
aaronmk
|
child = root.getAttributeNode(elem.name)
|
185 |
165
|
aaronmk
|
if child != None: children = [child]
|
186 |
1004
|
aaronmk
|
elif is_self(elem): children = [root]
|
187 |
|
|
elif is_parent(elem):
|
188 |
1000
|
aaronmk
|
parent = xml_dom.parent(root)
|
189 |
|
|
if parent != None: children = [parent]
|
190 |
|
|
else: return [] # don't try to create the document root's parent
|
191 |
165
|
aaronmk
|
else:
|
192 |
998
|
aaronmk
|
children = xml_dom.by_tag_name(root, elem.name,
|
193 |
165
|
aaronmk
|
last_only and (elem.keys == [] or is_instance(elem)))
|
194 |
|
|
|
195 |
|
|
# Check each match
|
196 |
885
|
aaronmk
|
nodes = []
|
197 |
165
|
aaronmk
|
for child in children:
|
198 |
|
|
is_match = elem.value == None or xml_dom.value(child) == elem.value
|
199 |
|
|
for attr in elem.keys:
|
200 |
|
|
if not is_match: break
|
201 |
886
|
aaronmk
|
is_match = ((get(child, attr, False, last_only) != [])
|
202 |
|
|
== is_positive(attr))
|
203 |
885
|
aaronmk
|
if is_match:
|
204 |
|
|
nodes.append(child)
|
205 |
|
|
if limit != None and len(nodes) >= limit: break
|
206 |
165
|
aaronmk
|
|
207 |
|
|
# Create node
|
208 |
886
|
aaronmk
|
if nodes == []:
|
209 |
|
|
if not create: return []
|
210 |
77
|
aaronmk
|
if elem.is_attr:
|
211 |
998
|
aaronmk
|
root.setAttribute(elem.name, '')
|
212 |
|
|
node = root.getAttributeNode(elem.name)
|
213 |
|
|
else: node = root.appendChild(doc.createElement(elem.name))
|
214 |
165
|
aaronmk
|
if elem.value != None: xml_dom.set_value(node, elem.value)
|
215 |
886
|
aaronmk
|
nodes.append(node)
|
216 |
165
|
aaronmk
|
|
217 |
971
|
aaronmk
|
value_ = value(xpath)
|
218 |
165
|
aaronmk
|
xpath = xpath[1:] # rest of XPath
|
219 |
|
|
|
220 |
886
|
aaronmk
|
next = []
|
221 |
|
|
for node in nodes:
|
222 |
|
|
# Create attrs
|
223 |
|
|
if create:
|
224 |
|
|
for attr in elem.keys + elem.attrs:
|
225 |
|
|
get(node, attr, create, last_only)
|
226 |
|
|
|
227 |
|
|
# Follow pointer
|
228 |
|
|
if elem.is_ptr:
|
229 |
998
|
aaronmk
|
root = doc.documentElement
|
230 |
886
|
aaronmk
|
xpath = copy.deepcopy(xpath)
|
231 |
|
|
id_path = backward_id(xpath[instance_level])
|
232 |
|
|
if id_path != None: # backward (child-to-parent) pointer with ID key
|
233 |
|
|
id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
|
234 |
|
|
set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
|
235 |
|
|
else: # forward (parent-to-child) pointer
|
236 |
|
|
id_ = xml_dom.value(node)
|
237 |
|
|
obj_xpath = obj(xpath) # target object
|
238 |
|
|
if id_ == None or get(root, obj_xpath, False, True) == []:
|
239 |
|
|
# no target or target keys don't match
|
240 |
|
|
if not create: continue
|
241 |
|
|
|
242 |
|
|
# Use last target object's ID + 1
|
243 |
|
|
obj_xpath[-1].keys = [] # just get by tag name
|
244 |
|
|
last = get(root, obj_xpath, False, True)
|
245 |
|
|
if last != []: id_ = str(int(xml_dom.get_id(last[0])) + 1)
|
246 |
|
|
else: id_ = '0'
|
247 |
|
|
|
248 |
|
|
# Will append if target keys didn't match.
|
249 |
|
|
# Use lookahead assertion to avoid this.
|
250 |
|
|
xml_dom.set_value(node, id_)
|
251 |
|
|
else: last_only = False
|
252 |
|
|
set_id(xpath, id_)
|
253 |
|
|
else: root = node
|
254 |
889
|
aaronmk
|
next += get(root, xpath, create, last_only, limit)
|
255 |
886
|
aaronmk
|
|
256 |
|
|
for branch in elem.other_branches:
|
257 |
|
|
branch = copy.deepcopy(branch)
|
258 |
971
|
aaronmk
|
set_value(branch, value_)
|
259 |
889
|
aaronmk
|
next += get(node, branch, create, last_only, limit)
|
260 |
165
|
aaronmk
|
|
261 |
|
|
return next
|
262 |
82
|
aaronmk
|
|
263 |
141
|
aaronmk
|
def put_obj(root, xpath, id_, has_types, value=None):
|
264 |
892
|
aaronmk
|
if util.is_str(xpath): xpath = parse(xpath)
|
265 |
155
|
aaronmk
|
|
266 |
82
|
aaronmk
|
xpath = copy.deepcopy(xpath) # don't modify input!
|
267 |
|
|
set_id(xpath, id_, has_types)
|
268 |
85
|
aaronmk
|
if value != None: set_value(xpath, value)
|
269 |
141
|
aaronmk
|
get(root, xpath, True)
|
270 |
133
|
aaronmk
|
|
271 |
135
|
aaronmk
|
def path2xml(xpath, first_branch=True):
|
272 |
|
|
root = xml_dom.create_doc().documentElement
|
273 |
141
|
aaronmk
|
get(root, xpath, True)
|
274 |
135
|
aaronmk
|
return root
|
275 |
133
|
aaronmk
|
|
276 |
141
|
aaronmk
|
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
|