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 and (elem.keys == [] or is_instance(elem)))
232
    
233
    # Retrieve elem value
234
    value_ = elem.value
235
    if util.is_list(value_): # reference (different from a pointer)
236
        targets = get(root, value_)
237
        try: target = targets[0]
238
        except IndexError:
239
            warnings.warn(UserWarning('XPath reference target missing: '
240
                +str(value_)+'\nXPath: '+str(xpath)))
241
            value_ = None
242
        else: value_ = xml_dom.value(target)
243
    
244
    # Check each match
245
    nodes = []
246
    for child in children:
247
        is_match = value_ == None or xml_dom.value(child) == value_
248
        for attr in elem.keys:
249
            if not is_match: break
250
            is_match = ((get(child, attr, False, last_only,
251
                allow_rooted=False) != []) == is_positive(attr))
252
        if is_match:
253
            nodes.append(child)
254
            if limit != None and len(nodes) >= limit: break
255
    
256
    # Create node
257
    if nodes == []:
258
        if not create or elem.is_lookup_only: return []
259
        if elem.is_attr:
260
            root.setAttribute(elem.name, '')
261
            node = root.getAttributeNode(elem.name)
262
        elif util.list_eq_is(children, [root]): node = root
263
        else: node = root.appendChild(doc.createElement(elem.name))
264
        if value_ != None: xml_dom.set_value(node, value_)
265
        nodes.append(node)
266
    
267
    path_value = value(xpath)
268
    xpath = xpath[1:] # rest of XPath
269
    
270
    next = []
271
    for node in nodes:
272
        # Create attrs
273
        if create:
274
            for attr in elem.keys + elem.attrs:
275
                get(node, attr, create, last_only, allow_rooted=False)
276
        
277
        # Follow pointer
278
        if elem.is_ptr:
279
            root = doc.documentElement
280
            xpath = copy.deepcopy(xpath)
281
            id_path = backward_id(xpath[instance_level])
282
            if id_path != None: # backward (child-to-parent) pointer with ID key
283
                id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
284
                set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
285
            else: # forward (parent-to-child) pointer
286
                id_ = xml_dom.value(node)
287
                obj_xpath = obj(xpath) # target object
288
                if id_ == None or get(root, obj_xpath, False, True) == []:
289
                    # no target or target keys don't match
290
                    if not create: continue
291
                    
292
                    # Use last target object's ID + 1
293
                    obj_xpath[-1].keys = [] # just get by tag name
294
                    last = get(root, obj_xpath, False, True)
295
                    if last != []: id_ = str(int(xml_dom.get_id(last[0])) + 1)
296
                    else: id_ = '0'
297
                    
298
                    # Will append if target keys didn't match.
299
                    # Use lookahead assertion to avoid this.
300
                    xml_dom.set_value(node, id_)
301
                else: last_only = False
302
                set_id(xpath, id_)
303
        else: root = node
304
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
305
        
306
        for branch in elem.other_branches:
307
            branch = branch[:] # don't modify input!
308
            set_value(branch, path_value)
309
            next += get(node, branch, create, last_only, limit,
310
                allow_rooted=False)
311
    
312
    return next
313

    
314
def get_1(*args, **kw_args):
315
    '''Runs get() and returns the first result'''
316
    return util.list_get(get(*args, **kw_args), 0)
317

    
318
def put_obj(root, xpath, id_, has_types, value=None):
319
    if util.is_str(xpath): xpath = parse(xpath)
320
    
321
    xpath = xpath[:] # don't modify input!
322
    set_id(xpath, id_, has_types)
323
    if value != None: set_value(xpath, value)
324
    get(root, xpath, True)
325

    
326
def path2xml(xpath, first_branch=True):
327
    root = xml_dom.create_doc().documentElement
328
    get(root, xpath, True)
329
    return root
330

    
331
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
(19-19/20)