From e9bd8c943cece3f107b25e4f0e386affad65b375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Wed, 9 Nov 2005 21:06:47 +0000 Subject: [PATCH] * improve piksemel implementation a bit * add newDocument and parse functions to xmlext (kind of for piks compat.) * fix: getNode function was b0rked, xmlext test passes now * switch back to mdom impl. because piks. impl. doesn't work yet --- pisi.e3p | 50 +++++++++++++++++++++++++---------------- pisi/xml/xmlext.py | 3 ++- pisi/xml/xmlextmdom.py | 48 ++++++++++++++++++++++++++++----------- pisi/xml/xmlextpiks.py | 12 +++++----- pisi/xml/xmlfile.py | 5 +++-- pisi/xml/xmlfilemdom.py | 7 +++--- tests/xmlexttests.py | 2 -- 7 files changed, 82 insertions(+), 45 deletions(-) diff --git a/pisi.e3p b/pisi.e3p index e9197b6b..1dbd28e9 100644 --- a/pisi.e3p +++ b/pisi.e3p @@ -1,7 +1,7 @@ - + Python @@ -370,32 +370,44 @@ pisi + xml xmlextmdom.py pisi - xmlext.py - - - pisi - xmlextpiks.py - - - pisi - xmlfile.py - - - pisi - xmlfilepiks.py - - - pisi + xml xmlfilemdom.py pisi + xml + xmlextpiks.py + + + pisi + xml + xmlfilepiks.py + + + pisi + xml + xmlext.py + + + pisi + xml autoxml.py + + pisi + xml + xmlfile.py + + + pisi + xml + __init__.py + @@ -438,9 +450,9 @@ - - + + diff --git a/pisi/xml/xmlext.py b/pisi/xml/xmlext.py index 9b86f462..75381f63 100644 --- a/pisi/xml/xmlext.py +++ b/pisi/xml/xmlext.py @@ -12,7 +12,8 @@ # Authors: Eray Ozkural try: - from xmlextpiks import * + #from xmlextpiks import * + zimbabwe except: print 'xmlext: piksemel implementation cannot be loaded, falling back to minidom' from xmlextmdom import * diff --git a/pisi/xml/xmlextmdom.py b/pisi/xml/xmlextmdom.py index caeafff7..4cab2b72 100644 --- a/pisi/xml/xmlextmdom.py +++ b/pisi/xml/xmlextmdom.py @@ -15,11 +15,9 @@ """ xmlext is a helper module for accessing XML files using - xml.dom.minidom. - - XmlFile class further abstracts a dom object using the - high-level dom functions provided in xmlext module (and sorely lacking - in xml.dom :( ) + 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' @@ -32,6 +30,7 @@ __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext import xml.dom.minidom as mdom +from xml.parsers.expat import ExpatError import pisi @@ -39,6 +38,29 @@ class XmlError(pisi.Error): "named this way because the class if mostly used with an import *" pass +# Document wrappers + +def newDocument(tag): + impl = mdom.getDOMImplementation() + dom = impl.createDocument(None, tag, None) + return dom.documentElement + +def parse(filename): + try: + dom = mdom.parse(filename) + return dom.documentElement + except ExpatError, inst: + raise Error(_("File '%s' has invalid XML: %s\n") % (fileName, + str(inst))) + +def newNode(node, tag): + return node.ownerDocument.createElement(tag) + +def newTextNode(node, text): + return node.ownerDocument.createTextNode(text) + +# Node related wrappers + def getNodeAttribute(node, attrname): """get named attribute from DOM node""" if not node.hasAttribute(attrname): @@ -85,10 +107,16 @@ def getNode(node, tagpath): # iterative code to search for the path for tag in tags: + currentNode = None for child in node.childNodes: if child.nodeType == node.ELEMENT_NODE and child.tagName == tag: - return child - return None + currentNode = child + break + if not currentNode: + return None + else: + node = currentNode + return currentNode def getAllNodes(node, tagPath): """retrieve all nodes that match a given tag path.""" @@ -165,12 +193,6 @@ def addNode(node, tagpath, newnode = None, branch=True): return node -def newNode(node, tag): - return node.ownerDocument.createElement(tag) - -def newTextNode(node, text): - return node.ownerDocument.createTextNode(text) - def addText(node, tagPath, text, branch = True): newnode = newTextNode(node, text) addNode(node, tagPath, newnode, branch) diff --git a/pisi/xml/xmlextpiks.py b/pisi/xml/xmlextpiks.py index 9ae35ffc..aa815998 100644 --- a/pisi/xml/xmlextpiks.py +++ b/pisi/xml/xmlextpiks.py @@ -154,12 +154,14 @@ def addNode(node, tagpath, newnode = None, branch=True): return addTagPath(node, tags, newnode) return node + +# cannot be implemented with piksemel +#def newNode(node, tag): +# return Node(tag) -def newNode(node, tag): - return Node(tag) - -def newTextNode(node, text): - return Node(text) +# cannot be implemented with piksemel :( +#def newTextNode(node, text): +# return Node(text) def addText(node, tagPath, text, branch = True): newnode = newTextNode(node, text) diff --git a/pisi/xml/xmlfile.py b/pisi/xml/xmlfile.py index 39276b08..01a96d29 100644 --- a/pisi/xml/xmlfile.py +++ b/pisi/xml/xmlfile.py @@ -12,8 +12,9 @@ # Authors: Eray Ozkural try: - from xmlextpiks import * - from xmlfilepiks import * + #from xmlextpiks import * + #from xmlfilepiks import * + horagata except: print 'xmlfile: piksemel implementation cannot be loaded, falling back to minidom' from xmlfilemdom import * diff --git a/pisi/xml/xmlfilemdom.py b/pisi/xml/xmlfilemdom.py index a7a8ce27..533b4396 100644 --- a/pisi/xml/xmlfilemdom.py +++ b/pisi/xml/xmlfilemdom.py @@ -28,7 +28,8 @@ import gettext __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext -import xml.dom.minidom as mdom +#import xml.dom.minidom as mdom +from xml.parsers.expat import ExpatError import codecs import pisi @@ -61,8 +62,8 @@ class XmlFile(object): try: self.dom = mdom.parse(fileName) except ExpatError, inst: - raise XmlError(_("File '%s' has invalid XML: %s\n") % (fileName, - str(inst))) + raise Error(_("File '%s' has invalid XML: %s\n") % (fileName, + str(inst))) def writexml(self, fileName): f = codecs.open(fileName,'w', "utf-8") diff --git a/tests/xmlexttests.py b/tests/xmlexttests.py index 96f863d8..b30ca37e 100644 --- a/tests/xmlexttests.py +++ b/tests/xmlexttests.py @@ -42,6 +42,4 @@ class XmlExtTestCase(testcase.TestCase): reada = xmlext.getAllNodes(node, 'team/coder') self.assertEqual(len(reada), 3) - pass - suite = unittest.makeSuite(XmlExtTestCase)