Project

General

Profile

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