Project

General

Profile

« Previous | Next » 

Revision 141

xpath.py: Refactored to avoid needing a doc parameter for the XML document

View differences:

xpath.py
18 18
    
19 19
    def __repr__(self):
20 20
        str_ = ''
21
        if self.is_positive: str_ += '!'
21
        if not self.is_positive: str_ += '!'
22 22
        if self.is_attr: str_ += '@'
23 23
        str_ += self.name
24 24
        if self.keys != []: str_ += repr(self.keys)
......
135 135

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

  
138
def get(doc, xpath, create=False, last_only=None, parent=None):
139
    # Warning: The last_only optimization may put data that should be together
140
    # into separate nodes
141
    if parent == None: parent = doc.documentElement
138
def get(parent, xpath, create=False, last_only=None):
139
    '''Warning: The last_only optimization may put data that should be together
140
    into separate nodes'''
142 141
    if last_only == None: last_only = create
143 142
    
144 143
    if create and not is_positive(xpath): return None
145 144
    doc = parent.ownerDocument
145
    root = doc.documentElement
146 146
    for elem_idx, elem in enumerate(xpath):
147 147
        # Find possible matches
148 148
        children = []
......
160 160
            is_match = elem.value == None or xml_dom.value(child) == elem.value
161 161
            for attr in elem.keys:
162 162
                if not is_match: break
163
                is_match = (get(doc, attr, False, last_only, child) != None)\
163
                is_match = (get(child, attr, False, last_only) != None)\
164 164
                == is_positive(attr)
165 165
            if is_match: node = child; break
166 166
        
......
174 174
            if elem.value != None: xml_dom.set_value(doc, node, elem.value)
175 175
        if create:
176 176
            for attr in elem.keys + elem.attrs:
177
                get(doc, attr, create, last_only, node)
177
                get(node, attr, create, last_only)
178 178
        
179 179
        for branch in elem.other_branches:
180 180
            branch = copy.deepcopy(branch)
181 181
            set_value(branch, value(xpath))
182
            get(doc, branch, create, last_only, node)
182
            get(node, branch, create, last_only)
183 183
        
184 184
        # Follow pointer
185 185
        if elem.is_ptr:
......
191 191
            else: # forward (parent-to-child) pointer
192 192
                id_ = xml_dom.value(node)
193 193
                obj_xpath = obj(xpath) # target object
194
                if id_ == None or get(doc, obj_xpath, False, True) == None:
194
                if id_ == None or get(root, obj_xpath, False, True) == None:
195 195
                    # no target or target keys don't match
196 196
                    if not create: return None
197 197
                    
198 198
                    # Use last target object's ID + 1
199 199
                    obj_xpath[-1].keys = [] # just get by tag name
200
                    last = get(doc, obj_xpath, False, True)
200
                    last = get(root, obj_xpath, False, True)
201 201
                    if last != None: id_ = str(int(xml_dom.get_id(last)) + 1)
202 202
                    else: id_ = '0'
203 203
                    
......
206 206
                    xml_dom.set_value(doc, node, id_)
207 207
                else: last_only = False
208 208
                set_id(xpath, id_)
209
            return get(doc, xpath, create, last_only)
209
            return get(root, xpath, create, last_only)
210 210
        
211 211
        parent = node
212 212
    return parent
213 213

  
214
def put_obj(doc, xpath, id_, has_types, value=None):
214
def put_obj(root, xpath, id_, has_types, value=None):
215 215
    xpath = copy.deepcopy(xpath) # don't modify input!
216 216
    set_id(xpath, id_, has_types)
217 217
    if value != None: set_value(xpath, value)
218
    get(doc, xpath, True)
218
    get(root, xpath, True)
219 219

  
220 220
def path2xml(xpath, first_branch=True):
221 221
    root = xml_dom.create_doc().documentElement
222
    get(root.ownerDocument, xpath, True)
223
    if first_branch: root = root.firstChild
222
    get(root, xpath, True)
224 223
    return root
225 224

  
226 225
def str2xml(xpath): return path2xml(parse(xpath))
226

  
227
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)

Also available in: Unified diff