Project

General

Profile

1 55 aaronmk
# XPath parsing
2 21 aaronmk
3 55 aaronmk
import copy
4 1009 aaronmk
import warnings
5 21 aaronmk
6
from Parser import Parser
7 892 aaronmk
import util
8 77 aaronmk
import xml_dom
9 21 aaronmk
10 1001 aaronmk
##### Path elements
11
12 21 aaronmk
class XpathElem:
13 1002 aaronmk
    def __init__(self, name='', value=None, is_attr=False):
14 21 aaronmk
        self.name = name
15
        self.value = value
16
        self.is_attr = is_attr
17 97 aaronmk
        self.is_positive = True
18 1238 aaronmk
        self.is_lookup_only = False
19 94 aaronmk
        self.is_ptr = False
20
        self.keys = []
21
        self.attrs = []
22 78 aaronmk
        self.other_branches = [] # temp implementation for split paths
23 21 aaronmk
24
    def __repr__(self):
25
        str_ = ''
26 141 aaronmk
        if not self.is_positive: str_ += '!'
27 21 aaronmk
        if self.is_attr: str_ += '@'
28 1004 aaronmk
        if self.name == '': str_ += '""'
29
        else: str_ += self.name
30 1238 aaronmk
        if self.is_lookup_only: str_ += '?'
31 94 aaronmk
        if self.keys != []: str_ += repr(self.keys)
32
        if self.attrs != []: str_ += ':'+repr(self.attrs)
33
        if self.is_ptr: str_ += '->'
34
        if self.other_branches != []: str_ += '{'+repr(self.other_branches)+'}'
35 25 aaronmk
        if self.value != None: str_ += '='+repr(self.value)
36 21 aaronmk
        return str_
37
38
    def __eq__(self, other): return self.__dict__ == other.__dict__
39
40 1002 aaronmk
empty_elem = XpathElem()
41
42
def elem_is_empty(elem): return elem == empty_elem
43
44 1004 aaronmk
def is_self(elem): return elem.name == '' or elem.name == '.'
45
46
def is_parent(elem): return elem.name == '..'
47
48 1001 aaronmk
##### Paths
49
50 1350 aaronmk
def path_is_empty(path): return path == [] or path == [empty_elem]
51
52 99 aaronmk
def is_positive(path): return path[0].is_positive
53
54 1003 aaronmk
def is_rooted(path): return elem_is_empty(path[0])
55
56 24 aaronmk
def value(path): return path[-1].value
57
58 1294 aaronmk
def set_value(path, value):
59
    '''Caller must make a shallow copy of the path to prevent modifications from
60
    propagating to other copies of the path (a deep copy is not needed)'''
61
    path[-1] = copy.copy(path[-1]) # don't modify other copies of the path
62
    path[-1].value = value
63 22 aaronmk
64 1577 aaronmk
def append(path, subpath, i=0):
65
    '''Recursively appends subpath to every leaf of a path tree'''
66
    if i >= len(path): path += subpath
67
    else:
68
        append(path, subpath, i+1)
69
        for branch in path[i].other_branches: append(branch, subpath)
70
71 32 aaronmk
def backward_id(elem):
72 171 aaronmk
    if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
73 32 aaronmk
    else: return None
74
75 1001 aaronmk
instance_level = 1 # root's grandchildren
76
77
def obj(path):
78
    obj_path = copy.deepcopy(path[:instance_level+1])
79
    obj_path[-1].is_ptr = False # prevent pointer w/o target
80
    return obj_path
81
82
def set_id(path, id_, has_types=True):
83 1294 aaronmk
    '''Caller must make a shallow copy of the path to prevent modifications from
84
    propagating to other copies of the path (a deep copy is not needed)'''
85 1001 aaronmk
    if has_types: id_level = instance_level
86
    else: id_level = 0 # root's children
87 1004 aaronmk
    if is_self(path[0]): id_level += 1 # explicit root element
88 1294 aaronmk
89
    id_elem = path[id_level] = copy.copy(path[id_level])
90
        # don't modify other copies of the path
91
    id_elem.keys = id_elem.keys[:] # don't modify other copies of the elem
92
    id_elem.keys.append([XpathElem('id', id_, True)])
93 1001 aaronmk
94
def is_id(path): return path[0].is_attr and path[0].name == 'id'
95
96 1070 aaronmk
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0])
97 1001 aaronmk
98
##### Parsing
99
100 1345 aaronmk
import xpath_func
101
102 170 aaronmk
def expand_abbr(name, repl):
103
    before, abbr, after = name.partition('*')
104
    if abbr != '': name = before+repl+after
105
    return name
106
107 154 aaronmk
_cache = {}
108
109 76 aaronmk
def parse(str_):
110 154 aaronmk
    try: return _cache[str_]
111
    except KeyError: pass
112
113 76 aaronmk
    parser = Parser(str_)
114 21 aaronmk
115 76 aaronmk
    def _path():
116 97 aaronmk
        is_positive = not parser.str_('!')
117
118 21 aaronmk
        tree = []
119
        while True:
120 38 aaronmk
            # Split path
121 76 aaronmk
            if parser.str_('{'):
122 1398 aaronmk
                try: last = tree[-1]
123
                except IndexError: parser.syntax_err('./{')
124 1070 aaronmk
                last.other_branches = _paths()
125 1577 aaronmk
                tree += last.other_branches.pop(0) # use first path for now
126 76 aaronmk
                parser.str_('}', required=True)
127 1577 aaronmk
                if parser.str_('/'): append(tree, _path()) # subpath after {}
128 1070 aaronmk
                break # anything after split path has already been parsed
129 36 aaronmk
130 754 aaronmk
            elem = XpathElem(is_attr=parser.str_('@'), name=_value())
131 38 aaronmk
132 1238 aaronmk
            elem.is_lookup_only = parser.str_('?')
133
134 94 aaronmk
            # Keys used to match nodes
135 76 aaronmk
            if parser.str_('['):
136 94 aaronmk
                elem.keys = _paths()
137 76 aaronmk
                parser.str_(']', required=True)
138 36 aaronmk
139 94 aaronmk
            # Attrs created when no matching node exists
140
            if parser.str_(':'):
141
                parser.str_('[', required=True)
142
                elem.attrs = _paths()
143
                parser.str_(']', required=True)
144
145 76 aaronmk
            elem.is_ptr = parser.str_('->')
146 21 aaronmk
            tree.append(elem)
147 36 aaronmk
148
            # Lookahead assertion
149 76 aaronmk
            if parser.str_('('):
150
                parser.str_('/', required=True) # next / is inside ()
151
                path = _path()
152
                parser.str_(')', required=True)
153 94 aaronmk
                elem.keys.append(path)
154 36 aaronmk
                tree += path
155
156 76 aaronmk
            if not parser.str_('/'): break
157 32 aaronmk
158 97 aaronmk
        tree[0].is_positive = is_positive
159
160
        # Value
161 997 aaronmk
        if parser.str_('='):
162 1009 aaronmk
            if parser.str_('$'): # reference (different from a pointer)
163
                value = _path()
164
            else: value = _value() # literal value
165 997 aaronmk
            set_value(tree, value)
166 85 aaronmk
167 32 aaronmk
        # Expand * abbrs
168 70 aaronmk
        for i in reversed(xrange(len(tree))):
169
            elem = tree[i]
170 171 aaronmk
            if elem.is_ptr: offset = 2
171 32 aaronmk
            else: offset = 1
172 170 aaronmk
            try: repl = tree[i+offset].name
173 171 aaronmk
            except IndexError: pass # no replacement elem
174
            else: elem.name = expand_abbr(elem.name, repl)
175 32 aaronmk
176 21 aaronmk
        return tree
177 76 aaronmk
178 754 aaronmk
    def _value():
179
        if parser.str_('"'):
180
            value = parser.re(r'[^"]*')
181
            parser.str_('"', required=True)
182
        else: value = parser.re(r'(?:(?:\w+:)*[\w.*]+)?')
183
        return value
184
185 94 aaronmk
    def _paths():
186
        paths = []
187 1350 aaronmk
        path = _path()
188
        if not path_is_empty(path): # not empty list
189
            paths.append(path)
190
            while parser.str_(','): paths.append(_path())
191 1352 aaronmk
            if path_is_empty(paths[-1]): paths.pop() # remove trailing ","
192 94 aaronmk
        return paths
193
194 76 aaronmk
    path = _path()
195
    parser.end()
196 1342 aaronmk
    path = xpath_func.process(path)
197 154 aaronmk
    _cache[str_] = path
198 76 aaronmk
    return path
199 21 aaronmk
200 1001 aaronmk
##### Querying/creating XML trees
201 22 aaronmk
202 1005 aaronmk
def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
203 141 aaronmk
    '''Warning: The last_only optimization may put data that should be together
204
    into separate nodes'''
205 77 aaronmk
    if last_only == None: last_only = create
206 887 aaronmk
    if limit == None or limit > 1: last_only = False
207 892 aaronmk
    if util.is_str(xpath): xpath = parse(xpath)
208 99 aaronmk
209 1005 aaronmk
    # Handle edge cases
210 998 aaronmk
    if xpath == []: return [root]
211 886 aaronmk
    if create and not is_positive(xpath): return []
212 1005 aaronmk
213
    # Define vars
214 998 aaronmk
    doc = root.ownerDocument
215 1005 aaronmk
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
216 165 aaronmk
    elem = xpath[0]
217
218
    # Find possible matches
219
    children = []
220
    if elem.is_attr:
221 998 aaronmk
        child = root.getAttributeNode(elem.name)
222 165 aaronmk
        if child != None: children = [child]
223 1004 aaronmk
    elif is_self(elem): children = [root]
224
    elif is_parent(elem):
225 1000 aaronmk
        parent = xml_dom.parent(root)
226 1013 aaronmk
        if parent == None: return [] # don't try to create doc root's parent
227
        root = parent
228
        children = [root]
229 165 aaronmk
    else:
230 998 aaronmk
        children = xml_dom.by_tag_name(root, elem.name,
231 1815 aaronmk
            last_only=(last_only and (elem.keys == [] or is_instance(elem))),
232
            ignore_namespace=False) # later set ignore_namespace to True?
233 165 aaronmk
234 1009 aaronmk
    # Retrieve elem value
235
    value_ = elem.value
236
    if util.is_list(value_): # reference (different from a pointer)
237
        targets = get(root, value_)
238
        try: target = targets[0]
239
        except IndexError:
240
            warnings.warn(UserWarning('XPath reference target missing: '
241
                +str(value_)+'\nXPath: '+str(xpath)))
242
            value_ = None
243
        else: value_ = xml_dom.value(target)
244
245 165 aaronmk
    # Check each match
246 885 aaronmk
    nodes = []
247 165 aaronmk
    for child in children:
248 1009 aaronmk
        is_match = value_ == None or xml_dom.value(child) == value_
249 165 aaronmk
        for attr in elem.keys:
250
            if not is_match: break
251 1011 aaronmk
            is_match = ((get(child, attr, False, last_only,
252
                allow_rooted=False) != []) == is_positive(attr))
253 885 aaronmk
        if is_match:
254
            nodes.append(child)
255
            if limit != None and len(nodes) >= limit: break
256 165 aaronmk
257
    # Create node
258 886 aaronmk
    if nodes == []:
259 1239 aaronmk
        if not create or elem.is_lookup_only: return []
260 77 aaronmk
        if elem.is_attr:
261 998 aaronmk
            root.setAttribute(elem.name, '')
262
            node = root.getAttributeNode(elem.name)
263 1013 aaronmk
        elif util.list_eq_is(children, [root]): node = root
264 998 aaronmk
        else: node = root.appendChild(doc.createElement(elem.name))
265 1009 aaronmk
        if value_ != None: xml_dom.set_value(node, value_)
266 886 aaronmk
        nodes.append(node)
267 165 aaronmk
268 1009 aaronmk
    path_value = value(xpath)
269 165 aaronmk
    xpath = xpath[1:] # rest of XPath
270
271 886 aaronmk
    next = []
272
    for node in nodes:
273
        # Create attrs
274
        if create:
275
            for attr in elem.keys + elem.attrs:
276 1011 aaronmk
                get(node, attr, create, last_only, allow_rooted=False)
277 886 aaronmk
278
        # Follow pointer
279
        if elem.is_ptr:
280 998 aaronmk
            root = doc.documentElement
281 886 aaronmk
            xpath = copy.deepcopy(xpath)
282
            id_path = backward_id(xpath[instance_level])
283
            if id_path != None: # backward (child-to-parent) pointer with ID key
284
                id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
285
                set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
286
            else: # forward (parent-to-child) pointer
287
                id_ = xml_dom.value(node)
288
                obj_xpath = obj(xpath) # target object
289
                if id_ == None or get(root, obj_xpath, False, True) == []:
290
                    # no target or target keys don't match
291
                    if not create: continue
292
293
                    # Use last target object's ID + 1
294
                    obj_xpath[-1].keys = [] # just get by tag name
295
                    last = get(root, obj_xpath, False, True)
296 1840 aaronmk
                    if last != []: id_ = str(int(util.coalesce(
297
                        xml_dom.get_id(last[0]), 0)) + 1)
298 886 aaronmk
                    else: id_ = '0'
299
300
                    # Will append if target keys didn't match.
301
                    # Use lookahead assertion to avoid this.
302
                    xml_dom.set_value(node, id_)
303
                else: last_only = False
304
                set_id(xpath, id_)
305
        else: root = node
306 1005 aaronmk
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
307 886 aaronmk
308
        for branch in elem.other_branches:
309 1294 aaronmk
            branch = branch[:] # don't modify input!
310 1009 aaronmk
            set_value(branch, path_value)
311 1005 aaronmk
            next += get(node, branch, create, last_only, limit,
312
                allow_rooted=False)
313 165 aaronmk
314
    return next
315 82 aaronmk
316 1320 aaronmk
def get_1(*args, **kw_args):
317
    '''Runs get() and returns the first result'''
318
    return util.list_get(get(*args, **kw_args), 0)
319
320 1689 aaronmk
def get_value(*args, **kw_args):
321
    '''Runs get_1() and returns the value of any result node'''
322
    return util.do_ignore_none(xml_dom.value, get_1(*args, **kw_args))
323
324 141 aaronmk
def put_obj(root, xpath, id_, has_types, value=None):
325 892 aaronmk
    if util.is_str(xpath): xpath = parse(xpath)
326 155 aaronmk
327 1294 aaronmk
    xpath = xpath[:] # don't modify input!
328 82 aaronmk
    set_id(xpath, id_, has_types)
329 85 aaronmk
    if value != None: set_value(xpath, value)
330 141 aaronmk
    get(root, xpath, True)
331 133 aaronmk
332 135 aaronmk
def path2xml(xpath, first_branch=True):
333
    root = xml_dom.create_doc().documentElement
334 141 aaronmk
    get(root, xpath, True)
335 135 aaronmk
    return root
336 133 aaronmk
337 141 aaronmk
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)