Changeset 4 for trunk/python/Rappture/library.py
- Timestamp:
- Mar 18, 2005, 10:04:28 AM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/Rappture/library.py
r3 r4 21 21 re_xml = re.compile('<\?[Xx][Mm][Ll]') 22 22 re_dots = re.compile('(\([^\)]*)\.([^\)]*\))') 23 re_pathElem = re.compile('^(([a-zA-Z_]+ )([0-9]*))?(\(([^\)]+)\))?$')23 re_pathElem = re.compile('^(([a-zA-Z_]+#?)([0-9]*))?(\(([^\)]+)\))?$') 24 24 re_pathCreateElem = re.compile('^([^\(]+)\(([^\)]+)\)$') 25 25 … … 51 51 By default, this method returns an object representing the 52 52 DOM node referenced by the path. This is changed by setting 53 the "flavor" argument to " name" (for name of the tail element),53 the "flavor" argument to "id" (for name of the tail element), 54 54 to "type" (for the type of the tail element), to "component" 55 (for the component name "type( name)"), or to "object"55 (for the component name "type(id)"), or to "object" 56 56 for the default (an object representing the tail element). 57 57 """ … … 65 65 elif flavor == 'component': 66 66 return self._node2comp(node) 67 elif flavor == ' name':67 elif flavor == 'id': 68 68 return self._node2name(node) 69 69 elif flavor == 'type': 70 70 return node.tagName 71 71 72 raise ValueError, "bad flavor '%s': should be object, name, type" % flavor72 raise ValueError, "bad flavor '%s': should be object, id, type" % flavor 73 73 74 74 # ------------------------------------------------------------------ … … 84 84 By default, this method returns a list of objects representing 85 85 the children. This is changed by setting the "flavor" argument 86 to " name" (for tail names of all children), to "type" (for the86 to "id" (for tail names of all children), to "type" (for the 87 87 types of all children), to "component" (for the path component 88 88 names of all children), or to "object" for the default (a list … … 103 103 elif flavor == 'component': 104 104 return [self._node2comp(n) for n in nlist] 105 elif flavor == ' name':105 elif flavor == 'id': 106 106 return [self._node2name(n) for n in nlist] 107 107 elif flavor == 'type': … … 224 224 """ 225 225 Used internally to create a path component name for the 226 specified node. A path component name has the form "type( name)"226 specified node. A path component name has the form "type(id)" 227 227 or just "type##" if the node doesn't have a name. This name 228 228 can be used in a path to uniquely address the component. … … 254 254 Used internally to find a particular element within the 255 255 root node according to the path, which is a string of 256 the form "type ##(name).type##(name). ...", where each257 "type" is a tag <type>; if the optional ##is specified,256 the form "typeNN(id).typeNN(id). ...", where each 257 "type" is a tag <type>; if the optional NN is specified, 258 258 it indicates an index for the <type> tag within its parent; 259 if the optional ( name) part is included, it indicates a260 tag of the form <type id=" name">.259 if the optional (id) part is included, it indicates a 260 tag of the form <type id="id">. 261 261 262 262 By default, it looks for an element along the path and … … 264 264 it creates various elements along the path as it goes. 265 265 This is useful for "put" operations. 266 267 If you include "#" instead of a specific number, a node 268 will be created automatically with a new number. For example, 269 the path "foo.bar#" called the first time will create "foo.bar", 270 the second time "foo.bar1", the third time "foo.bar2" and 271 so forth. 266 272 267 273 Returns an object representing the element indicated by … … 280 286 for part in path: 281 287 # 282 # Pick apart "type123( name)" for this part.288 # Pick apart "type123(id)" for this part. 283 289 # 284 290 match = self.re_pathElem.search(part) 285 291 if not match: 286 raise ValueError, "bad path component '%s': should have the form 'type123( name)'" % part292 raise ValueError, "bad path component '%s': should have the form 'type123(id)'" % part 287 293 288 294 (dummy, type, index, dummy, name) = match.groups() … … 306 312 else: 307 313 # 308 # If the name is like "type( name)", then look for elements314 # If the name is like "type(id)", then look for elements 309 315 # that match the type and see if one has the requested name. 310 # if the name is like "( name)", then look for any elements316 # if the name is like "(id)", then look for any elements 311 317 # with the requested name. 312 318 # … … 333 339 # 334 340 # If the "create" flag is set, then create a node 335 # with the specified "type(name)" and continue on. 341 # with the specified "type(id)" and continue on. 342 # If the type is "type#", then create a node with 343 # an automatic number. 336 344 # 337 345 match = self.re_pathCreateElem.search(part) … … 342 350 name = "" 343 351 344 node = self.doc.createElement(type) 345 lastnode.appendChild(node) 352 if type.endswith('#'): 353 type = type.rstrip('#') 354 node = self.doc.createElement(type) 355 356 # find the last node of same type and append there 357 pos = None 358 for n in lastnode.childNodes: 359 if n.nodeName == type: 360 pos = n 361 if pos: 362 pos = pos.nextSibling 363 364 if pos: 365 lastnode.insertBefore(node,pos) 366 else: 367 lastnode.appendChild(node) 368 else: 369 node = self.doc.createElement(type) 370 lastnode.appendChild(node) 346 371 347 372 if name: 348 node.setAttribute(" name",name)373 node.setAttribute("id",name) 349 374 350 375 lastnode = node
Note: See TracChangeset
for help on using the changeset viewer.