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
import xpath_func
10

    
11
##### Path elements
12

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

    
41
empty_elem = XpathElem()
42

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

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

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

    
49
##### Paths
50

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

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

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

    
57
def set_value(path, value):
58
    '''Caller must make a shallow copy of the path to prevent modifications from
59
    propagating to other copies of the path (a deep copy is not needed)'''
60
    path[-1] = copy.copy(path[-1]) # don't modify other copies of the path
61
    path[-1].value = value
62

    
63
def backward_id(elem):
64
    if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
65
    else: return None
66

    
67
instance_level = 1 # root's grandchildren
68

    
69
def obj(path):
70
    obj_path = copy.deepcopy(path[:instance_level+1])
71
    obj_path[-1].is_ptr = False # prevent pointer w/o target
72
    return obj_path
73

    
74
def set_id(path, id_, has_types=True):
75
    '''Caller must make a shallow copy of the path to prevent modifications from
76
    propagating to other copies of the path (a deep copy is not needed)'''
77
    if has_types: id_level = instance_level
78
    else: id_level = 0 # root's children
79
    if is_self(path[0]): id_level += 1 # explicit root element
80
    
81
    id_elem = path[id_level] = copy.copy(path[id_level])
82
        # don't modify other copies of the path
83
    id_elem.keys = id_elem.keys[:] # don't modify other copies of the elem
84
    id_elem.keys.append([XpathElem('id', id_, True)])
85

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

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

    
90
##### Parsing
91

    
92
def expand_abbr(name, repl):
93
    before, abbr, after = name.partition('*')
94
    if abbr != '': name = before+repl+after
95
    return name
96

    
97
_cache = {}
98

    
99
def parse(str_):
100
    try: return _cache[str_]
101
    except KeyError: pass
102
    
103
    parser = Parser(str_)
104
    
105
    def _path():
106
        is_positive = not parser.str_('!')
107
        
108
        tree = []
109
        while True:
110
            # Split path
111
            if parser.str_('{'):
112
                last = tree[-1]
113
                last.other_branches = _paths()
114
                parser.str_('}', required=True)
115
                if parser.str_('/'): # common subpath after {}
116
                    subpath = _path()
117
                    for branch in last.other_branches: branch += subpath
118
                tree += last.other_branches.pop(0) # use first path for now
119
                break # anything after split path has already been parsed
120
            
121
            elem = XpathElem(is_attr=parser.str_('@'), name=_value())
122
            
123
            elem.is_lookup_only = parser.str_('?')
124
            
125
            # Keys used to match nodes
126
            if parser.str_('['):
127
                elem.keys = _paths()
128
                parser.str_(']', required=True)
129
            
130
            # Attrs created when no matching node exists
131
            if parser.str_(':'):
132
                parser.str_('[', required=True)
133
                elem.attrs = _paths()
134
                parser.str_(']', required=True)
135
            
136
            elem.is_ptr = parser.str_('->')
137
            tree.append(elem)
138
            
139
            # Lookahead assertion
140
            if parser.str_('('):
141
                parser.str_('/', required=True) # next / is inside ()
142
                path = _path()
143
                parser.str_(')', required=True)
144
                elem.keys.append(path)
145
                tree += path
146
            
147
            if not parser.str_('/'): break
148
        
149
        tree[0].is_positive = is_positive
150
        
151
        # Value
152
        if parser.str_('='):
153
            if parser.str_('$'): # reference (different from a pointer)
154
                value = _path()
155
            else: value = _value() # literal value
156
            set_value(tree, value)
157
        
158
        # Expand * abbrs
159
        for i in reversed(xrange(len(tree))):
160
            elem = tree[i]
161
            if elem.is_ptr: offset = 2
162
            else: offset = 1
163
            try: repl = tree[i+offset].name
164
            except IndexError: pass # no replacement elem
165
            else: elem.name = expand_abbr(elem.name, repl)
166
        
167
        return tree
168
    
169
    def _value():
170
        if parser.str_('"'):
171
            value = parser.re(r'[^"]*')
172
            parser.str_('"', required=True)
173
        else: value = parser.re(r'(?:(?:\w+:)*[\w.*]+)?')
174
        return value
175
    
176
    def _paths():
177
        paths = []
178
        while True:
179
            paths.append(_path())
180
            if not parser.str_(','): break
181
        return paths
182
    
183
    path = _path()
184
    parser.end()
185
    path = xpath_func.process(path)
186
    _cache[str_] = path
187
    return path
188

    
189
##### Querying/creating XML trees
190

    
191
def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
192
    '''Warning: The last_only optimization may put data that should be together
193
    into separate nodes'''
194
    if last_only == None: last_only = create
195
    if limit == None or limit > 1: last_only = False
196
    if util.is_str(xpath): xpath = parse(xpath)
197
    
198
    # Handle edge cases
199
    if xpath == []: return [root]
200
    if create and not is_positive(xpath): return []
201
    
202
    # Define vars
203
    doc = root.ownerDocument
204
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
205
    elem = xpath[0]
206
    
207
    # Find possible matches
208
    children = []
209
    if elem.is_attr:
210
        child = root.getAttributeNode(elem.name)
211
        if child != None: children = [child]
212
    elif is_self(elem): children = [root]
213
    elif is_parent(elem):
214
        parent = xml_dom.parent(root)
215
        if parent == None: return [] # don't try to create doc root's parent
216
        root = parent
217
        children = [root]
218
    else:
219
        children = xml_dom.by_tag_name(root, elem.name,
220
            last_only and (elem.keys == [] or is_instance(elem)))
221
    
222
    # Retrieve elem value
223
    value_ = elem.value
224
    if util.is_list(value_): # reference (different from a pointer)
225
        targets = get(root, value_)
226
        try: target = targets[0]
227
        except IndexError:
228
            warnings.warn(UserWarning('XPath reference target missing: '
229
                +str(value_)+'\nXPath: '+str(xpath)))
230
            value_ = None
231
        else: value_ = xml_dom.value(target)
232
    
233
    # Check each match
234
    nodes = []
235
    for child in children:
236
        is_match = value_ == None or xml_dom.value(child) == value_
237
        for attr in elem.keys:
238
            if not is_match: break
239
            is_match = ((get(child, attr, False, last_only,
240
                allow_rooted=False) != []) == is_positive(attr))
241
        if is_match:
242
            nodes.append(child)
243
            if limit != None and len(nodes) >= limit: break
244
    
245
    # Create node
246
    if nodes == []:
247
        if not create or elem.is_lookup_only: return []
248
        if elem.is_attr:
249
            root.setAttribute(elem.name, '')
250
            node = root.getAttributeNode(elem.name)
251
        elif util.list_eq_is(children, [root]): node = root
252
        else: node = root.appendChild(doc.createElement(elem.name))
253
        if value_ != None: xml_dom.set_value(node, value_)
254
        nodes.append(node)
255
    
256
    path_value = value(xpath)
257
    xpath = xpath[1:] # rest of XPath
258
    
259
    next = []
260
    for node in nodes:
261
        # Create attrs
262
        if create:
263
            for attr in elem.keys + elem.attrs:
264
                get(node, attr, create, last_only, allow_rooted=False)
265
        
266
        # Follow pointer
267
        if elem.is_ptr:
268
            root = doc.documentElement
269
            xpath = copy.deepcopy(xpath)
270
            id_path = backward_id(xpath[instance_level])
271
            if id_path != None: # backward (child-to-parent) pointer with ID key
272
                id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
273
                set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
274
            else: # forward (parent-to-child) pointer
275
                id_ = xml_dom.value(node)
276
                obj_xpath = obj(xpath) # target object
277
                if id_ == None or get(root, obj_xpath, False, True) == []:
278
                    # no target or target keys don't match
279
                    if not create: continue
280
                    
281
                    # Use last target object's ID + 1
282
                    obj_xpath[-1].keys = [] # just get by tag name
283
                    last = get(root, obj_xpath, False, True)
284
                    if last != []: id_ = str(int(xml_dom.get_id(last[0])) + 1)
285
                    else: id_ = '0'
286
                    
287
                    # Will append if target keys didn't match.
288
                    # Use lookahead assertion to avoid this.
289
                    xml_dom.set_value(node, id_)
290
                else: last_only = False
291
                set_id(xpath, id_)
292
        else: root = node
293
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
294
        
295
        for branch in elem.other_branches:
296
            branch = branch[:] # don't modify input!
297
            set_value(branch, path_value)
298
            next += get(node, branch, create, last_only, limit,
299
                allow_rooted=False)
300
    
301
    return next
302

    
303
def get_1(*args, **kw_args):
304
    '''Runs get() and returns the first result'''
305
    return util.list_get(get(*args, **kw_args), 0)
306

    
307
def put_obj(root, xpath, id_, has_types, value=None):
308
    if util.is_str(xpath): xpath = parse(xpath)
309
    
310
    xpath = xpath[:] # don't modify input!
311
    set_id(xpath, id_, has_types)
312
    if value != None: set_value(xpath, value)
313
    get(root, xpath, True)
314

    
315
def path2xml(xpath, first_branch=True):
316
    root = xml_dom.create_doc().documentElement
317
    get(root, xpath, True)
318
    return root
319

    
320
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
(16-16/17)