Project

General

Profile

1
# XPath parsing
2

    
3
import copy
4
import warnings
5

    
6
from Parser import Parser
7
import util
8
import xml_dom
9

    
10
##### Path elements
11

    
12
class XpathElem:
13
    def __init__(self, name='', value=None, is_attr=False):
14
        self.name = name
15
        self.value = value
16
        self.is_attr = is_attr
17
        self.is_positive = True
18
        self.is_lookup_only = False
19
        self.is_ptr = False
20
        self.keys = []
21
        self.attrs = []
22
        self.other_branches = [] # temp implementation for split paths
23
    
24
    def __repr__(self):
25
        str_ = ''
26
        if not self.is_positive: str_ += '!'
27
        if self.is_attr: str_ += '@'
28
        if self.name == '': str_ += '""'
29
        else: str_ += self.name
30
        if self.is_lookup_only: str_ += '?'
31
        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
        if self.value != None: str_ += '='+repr(self.value)
36
        return str_
37
    
38
    def __eq__(self, other): return self.__dict__ == other.__dict__
39

    
40
empty_elem = XpathElem()
41

    
42
def elem_is_empty(elem): return elem == empty_elem
43

    
44
def is_self(elem): return elem.name == '' or elem.name == '.'
45

    
46
def is_parent(elem): return elem.name == '..'
47

    
48
##### Paths
49

    
50
def path_is_empty(path): return path == [] or path == [empty_elem]
51

    
52
def is_positive(path): return path[0].is_positive
53

    
54
def is_rooted(path): return elem_is_empty(path[0])
55

    
56
def value(path): return path[-1].value
57

    
58
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

    
64
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
def backward_id(elem):
72
    if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
73
    else: return None
74

    
75
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
    '''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
    if has_types: id_level = instance_level
86
    else: id_level = 0 # root's children
87
    if is_self(path[0]): id_level += 1 # explicit root element
88
    
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

    
94
def is_id(path): return path[0].is_attr and path[0].name == 'id'
95

    
96
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0])
97

    
98
##### Parsing
99

    
100
import xpath_func
101

    
102
def expand_abbr(name, repl):
103
    before, abbr, after = name.partition('*')
104
    if abbr != '': name = before+repl+after
105
    return name
106

    
107
_cache = {}
108

    
109
def parse(str_):
110
    try: return _cache[str_]
111
    except KeyError: pass
112
    
113
    parser = Parser(str_)
114
    
115
    def _path():
116
        is_positive = not parser.str_('!')
117
        
118
        tree = []
119
        while True:
120
            # Split path
121
            if parser.str_('{'):
122
                try: last = tree[-1]
123
                except IndexError: parser.syntax_err('./{')
124
                last.other_branches = _paths()
125
                tree += last.other_branches.pop(0) # use first path for now
126
                parser.str_('}', required=True)
127
                if parser.str_('/'): append(tree, _path()) # subpath after {}
128
                break # anything after split path has already been parsed
129
            
130
            elem = XpathElem(is_attr=parser.str_('@'), name=_value())
131
            
132
            elem.is_lookup_only = parser.str_('?')
133
            
134
            # Keys used to match nodes
135
            if parser.str_('['):
136
                elem.keys = _paths()
137
                parser.str_(']', required=True)
138
            
139
            # 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
            elem.is_ptr = parser.str_('->')
146
            tree.append(elem)
147
            
148
            # Lookahead assertion
149
            if parser.str_('('):
150
                parser.str_('/', required=True) # next / is inside ()
151
                path = _path()
152
                parser.str_(')', required=True)
153
                elem.keys.append(path)
154
                tree += path
155
            
156
            if not parser.str_('/'): break
157
        
158
        tree[0].is_positive = is_positive
159
        
160
        # Value
161
        if parser.str_('='):
162
            if parser.str_('$'): # reference (different from a pointer)
163
                value = _path()
164
            else: value = _value() # literal value
165
            set_value(tree, value)
166
        
167
        # Expand * abbrs
168
        for i in reversed(xrange(len(tree))):
169
            elem = tree[i]
170
            if elem.is_ptr: offset = 2
171
            else: offset = 1
172
            try: repl = tree[i+offset].name
173
            except IndexError: pass # no replacement elem
174
            else: elem.name = expand_abbr(elem.name, repl)
175
        
176
        return tree
177
    
178
    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
    def _paths():
186
        paths = []
187
        path = _path()
188
        if not path_is_empty(path): # not empty list
189
            paths.append(path)
190
            while parser.str_(','): paths.append(_path())
191
            if path_is_empty(paths[-1]): paths.pop() # remove trailing ","
192
        return paths
193
    
194
    path = _path()
195
    parser.end()
196
    path = xpath_func.process(path)
197
    _cache[str_] = path
198
    return path
199

    
200
##### Querying/creating XML trees
201

    
202
def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
203
    '''Warning: The last_only optimization may put data that should be together
204
    into separate nodes'''
205
    if last_only == None: last_only = create
206
    if limit == None or limit > 1: last_only = False
207
    if util.is_str(xpath): xpath = parse(xpath)
208
    
209
    # Handle edge cases
210
    if xpath == []: return [root]
211
    if create and not is_positive(xpath): return []
212
    
213
    # Define vars
214
    doc = root.ownerDocument
215
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
216
    elem = xpath[0]
217
    
218
    # Find possible matches
219
    children = []
220
    if elem.is_attr:
221
        child = root.getAttributeNode(elem.name)
222
        if child != None: children = [child]
223
    elif is_self(elem): children = [root]
224
    elif is_parent(elem):
225
        parent = xml_dom.parent(root)
226
        if parent == None: return [] # don't try to create doc root's parent
227
        root = parent
228
        children = [root]
229
    else:
230
        children = xml_dom.by_tag_name(root, elem.name,
231
            last_only=(last_only and (elem.keys == [] or is_instance(elem))),
232
            ignore_namespace=False) # later set ignore_namespace to True?
233
    
234
    # 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
    # Check each match
246
    nodes = []
247
    for child in children:
248
        is_match = value_ == None or xml_dom.value(child) == value_
249
        for attr in elem.keys:
250
            if not is_match: break
251
            is_match = ((get(child, attr, False, last_only,
252
                allow_rooted=False) != []) == is_positive(attr))
253
        if is_match:
254
            nodes.append(child)
255
            if limit != None and len(nodes) >= limit: break
256
    
257
    # Create node
258
    if nodes == []:
259
        if not create or elem.is_lookup_only: return []
260
        if elem.is_attr:
261
            root.setAttribute(elem.name, '')
262
            node = root.getAttributeNode(elem.name)
263
        elif util.list_eq_is(children, [root]): node = root
264
        else: node = root.appendChild(doc.createElement(elem.name))
265
        if value_ != None: xml_dom.set_value(node, value_)
266
        nodes.append(node)
267
    
268
    path_value = value(xpath)
269
    xpath = xpath[1:] # rest of XPath
270
    
271
    next = []
272
    for node in nodes:
273
        # Create attrs
274
        if create:
275
            for attr in elem.keys + elem.attrs:
276
                get(node, attr, create, last_only, allow_rooted=False)
277
        
278
        # Follow pointer
279
        if elem.is_ptr:
280
            root = doc.documentElement
281
            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
                    if last != []: id_ = str(int(util.coalesce(
297
                        xml_dom.get_id(last[0]), 0)) + 1)
298
                    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
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
307
        
308
        for branch in elem.other_branches:
309
            branch = branch[:] # don't modify input!
310
            set_value(branch, path_value)
311
            next += get(node, branch, create, last_only, limit,
312
                allow_rooted=False)
313
    
314
    return next
315

    
316
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
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
def put_obj(root, xpath, id_, has_types, value=None):
325
    if util.is_str(xpath): xpath = parse(xpath)
326
    
327
    xpath = xpath[:] # don't modify input!
328
    set_id(xpath, id_, has_types)
329
    if value != None: set_value(xpath, value)
330
    get(root, xpath, True)
331

    
332
def path2xml(xpath, first_branch=True):
333
    root = xml_dom.create_doc().documentElement
334
    get(root, xpath, True)
335
    return root
336

    
337
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
(27-27/28)