diff --git a/pisi/pxml/xmlextpiks.py b/pisi/pxml/xmlextpiks.py index 0fc1a183..3a376713 100644 --- a/pisi/pxml/xmlextpiks.py +++ b/pisi/pxml/xmlextpiks.py @@ -9,17 +9,17 @@ # # Please read the COPYING file. # -# Authors: Eray Ozkural -# Baris Metin """ - some helper functions for high-level access to XML structures - - this implementation uses piksemel, a fast C-based XML library + xmlext is a helper module for accessing XML files using + xml.dom.minidom . It is a convenient wrapper for some + DOM functions, and provides path based get/add functions + as in KDE API. function names are mixedCase for compatibility with minidom, - an old library + an 'old library' + + this implementation uses piksemel """ import gettext @@ -27,11 +27,23 @@ __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext import pisi -from piksemel import * +import piksemel as iks -class XmlError(pisi.Error): - "named this way because the class if mostly used with an import *" - pass +def getAllNodes(node, tagPath): + """retrieve all nodes that match a given tag path.""" + tags = tagPath.split('/') + if len(tags) == 0: + return [] + nodeList = [node] # basis case + for tag in tags: + results = map(lambda x: getTagByName(x, tag), nodeList) + nodeList = [] + for x in results: + nodeList.extend(x) + pass # emacs indentation error, keep it here + if len(nodeList) == 0: + return [] + return nodeList def getNodeAttribute(node, attrname): """get named attribute from DOM node""" @@ -39,23 +51,22 @@ def getNodeAttribute(node, attrname): def getChildElts(node): """get only child elements""" - return [x for x in parent.childNodes if x.type() == TAG ] + return [x for x in parent.tags()] def getTagByName(parent, childName): - return [x for x in parent if x.type() == TAG and x.name() == childName] + return [x for x in parent.tags(childName)] def getNodeText(node, tagpath = ""): """get the first child and expect it to be text!""" if tagpath!="": node = getNode(node, tagpath) - try: - child = node.firstChild() - except IndexError: + if not node: + return None + child = node.firstChild() + if not child: return None - except AttributeError: # no node by that name - return None - if child.type() == CDATA: #FIXME: ??? TEXT type? - # in any case, strip whitespaces... :/ you don't like it? + if child.type() == iks.DATA: + # in any case, strip whitespaces... return child.data().strip() else: raise XmlError(_("getNodeText: Expected text node, got something else!")) @@ -87,28 +98,15 @@ def getNode(node, tagpath): node = currentNode return currentNode -def getAllNodes(node, tagPath): - """retrieve all nodes that match a given tag path.""" - - #FIXME: better tag split - tags = tagPath.split('/') - - if len(tags) == 0: - return [] - - nodeList = [node] # basis case - - for tag in tags: - results = map(lambda x: getTagByName(x, tag), nodeList) - nodeList = [] - for x in results: - nodeList.extend(x) - pass # emacs indentation error, keep it here - - if len(nodeList) == 0: - return [] - - return nodeList +def addText(node, tagpath, text): + if tagpath != "": + tags = tagpath.split("/") + for tag in tags: + if node.getTag(tag): + node = node.getTag(tag) + else: + node = node.appendTag(tag) + node.appendData(text) def createTagPath(node, tags): """create new child at the end of a tag chain starting from node @@ -123,8 +121,8 @@ def addTagPath(node, tags, newnode=None): """add newnode at the end of a tag chain, smart one""" node = createTagPath(node, tags) if newnode: # node to add specified - node.appendTag(newnode) - return node + node.appendChild(newnode) + return node def addNode(node, tagpath, newnode = None, branch=True): """add a new node at the end of the tree and returns it @@ -160,16 +158,3 @@ def addNode(node, tagpath, newnode = None, branch=True): return addTagPath(node, tags, newnode) return node - -def addText(node, tagPath, text, branch = True): - node = addNode(node, tagPath, branch = branch) - return node.appendData(text) - - -# cannot be implemented with piksemel -#def newNode(node, tag): -# return Node(tag) - -# cannot be implemented with piksemel :( -#def newTextNode(node, text): -# return Node(text) diff --git a/pisi/pxml/xmlfilepiks.py b/pisi/pxml/xmlfilepiks.py index b22ee413..0d43d8c1 100644 --- a/pisi/pxml/xmlfilepiks.py +++ b/pisi/pxml/xmlfilepiks.py @@ -9,26 +9,32 @@ # # Please read the COPYING file. # -# Authors: Eray Ozkural -# Gurer Ozen -# Baris Metin