From 67d3fea7cc514dee7fe4f1c090e35ab793a742a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Thu, 10 Nov 2005 11:21:49 +0000 Subject: [PATCH] * add an "eraseDoc" option to read and write methods in XmlFile support of autoxml. The default is to keep the Document object * fix: xmlfile cdomlette implementation uses pretty print * fix: self.dom -> self.doc --- pisi/xml/autoxml.py | 11 ++++++++--- pisi/xml/xmlfilecdom.py | 20 ++++++++++---------- pisi/xml/xmlfilemdom.py | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pisi/xml/autoxml.py b/pisi/xml/autoxml.py index d5eb2891..0eac3642 100644 --- a/pisi/xml/autoxml.py +++ b/pisi/xml/autoxml.py @@ -378,7 +378,8 @@ class autoxml(oo.autosuper, oo.autoprop): cls.__ne__ = notequal if xmlfile_support: - def read(self, filename): + def read(self, filename, eraseDoc = False): + "read XML file and decode it into a python object" self.readxml(filename) errs = [] self.decode(self.rootNode(), errs) @@ -388,14 +389,16 @@ class autoxml(oo.autosuper, oo.autoprop): errs.append(_("autoxml.read: File '%s' has errors") % filename) raise Error(*errs) - self.unlink() + if eraseDoc: + self.unlink() # get rid of the tree errs = self.errors() if errs: errs.append(_("autoxml.read: File '%s' has errors") % filename) raise Error(*errs) - def write(self, filename): + def write(self, filename, eraseDoc = False): + "encode the contents of the python object into an XML file" errs = self.errors() if errs: errs.append(_("autoxml.write: object validation has failed")) @@ -409,6 +412,8 @@ class autoxml(oo.autosuper, oo.autoprop): errs.append(_("autoxml.write: File encoding '%s' has errors") % filename) raise Error(*errs) self.writexml(filename) + if eraseDoc: + self.unlink() # get rid of the tree cls.read = read cls.write = write diff --git a/pisi/xml/xmlfilecdom.py b/pisi/xml/xmlfilecdom.py index fdea5b6e..c5e1c7b8 100644 --- a/pisi/xml/xmlfilecdom.py +++ b/pisi/xml/xmlfilecdom.py @@ -60,8 +60,8 @@ class XmlFile(object): def unlink(self): """deallocate DOM structure""" - #del self.doc - self.doc = None # why? + del self.doc + #self.doc = None # why? def rootNode(self): """returns root document element""" @@ -76,7 +76,7 @@ class XmlFile(object): def writexml(self, path): f = file(path, 'w') - Print(self.rootNode(), stream = f) + PrettyPrint(self.rootNode(), stream = f) f.close() def verifyRootTag(self): @@ -88,20 +88,20 @@ class XmlFile(object): # construction helpers def newNode(self, tag): - return self.dom.createElementNS(None, tag) + return self.doc.createElementNS(None, tag) def newTextNode(self, text): - return self.dom.createTextNode(text) + return self.doc.createTextNode(text) def newAttribute(self, attr): - return self.dom.createAttribute(attr) + return self.doc.createAttribute(attr) # read helpers def getNode(self, tagPath = ""): """returns the *first* matching node for given tag path.""" self.verifyRootTag() - return getNode(self.dom.documentElement, tagPath) + return getNode(self.doc.documentElement, tagPath) def getNodeText(self, tagPath): """returns the text of *first* matching node for given tag path.""" @@ -113,7 +113,7 @@ class XmlFile(object): def getAllNodes(self, tagPath): """returns all nodes matching a given tag path.""" self.verifyRootTag() - return getAllNodes(self.dom.documentElement, tagPath) + return getAllNodes(self.doc.documentElement, tagPath) def getChildren(self, tagpath): """ returns the children of the given path""" @@ -142,7 +142,7 @@ class XmlFile(object): def addNode(self, tagPath, newnode = None): "this adds the newnode under given tag path" self.verifyRootTag() - return addNode(self.dom.documentElement, tagPath, newnode) + return addNode(self.doc.documentElement, tagPath, newnode) def addNodeUnder(self, node, tagPath, newnode = None): "this adds the new stuff under node and then following tag path" @@ -151,7 +151,7 @@ class XmlFile(object): def addChild(self, newnode): "add a new child node right under root element document" - self.dom.documentElement.appendChild(newnode) + self.doc.documentElement.appendChild(newnode) def addText(self, node, text): "add text to node" diff --git a/pisi/xml/xmlfilemdom.py b/pisi/xml/xmlfilemdom.py index 1afd374f..5e873b9f 100644 --- a/pisi/xml/xmlfilemdom.py +++ b/pisi/xml/xmlfilemdom.py @@ -48,26 +48,26 @@ class XmlFile(object): def newDocument(self): """clear DOM""" impl = mdom.getDOMImplementation() - self.dom = impl.createDocument(None, self.rootTag, None) + self.doc = impl.createDocument(None, self.rootTag, None) def unlink(self): """deallocate DOM structure""" - self.dom.unlink() + self.doc.unlink() def rootNode(self): """returns root document element""" - return self.dom.documentElement + return self.doc.documentElement def readxml(self, fileName): try: - self.dom = mdom.parse(fileName) + self.doc = mdom.parse(fileName) except ExpatError, inst: raise Error(_("File '%s' has invalid XML: %s\n") % (fileName, str(inst))) def writexml(self, fileName): f = codecs.open(fileName,'w', "utf-8") - f.write(self.dom.toprettyxml()) + f.write(self.doc.toprettyxml()) f.close() def verifyRootTag(self): @@ -79,20 +79,20 @@ class XmlFile(object): # construction helpers def newNode(self, tag): - return self.dom.createElement(tag) + return self.doc.createElement(tag) def newTextNode(self, text): - return self.dom.createTextNode(text) + return self.doc.createTextNode(text) def newAttribute(self, attr): - return self.dom.createAttribute(attr) + return self.doc.createAttribute(attr) # read helpers def getNode(self, tagPath = ""): """returns the *first* matching node for given tag path.""" self.verifyRootTag() - return getNode(self.dom.documentElement, tagPath) + return getNode(self.doc.documentElement, tagPath) def getNodeText(self, tagPath): """returns the text of *first* matching node for given tag path.""" @@ -104,7 +104,7 @@ class XmlFile(object): def getAllNodes(self, tagPath): """returns all nodes matching a given tag path.""" self.verifyRootTag() - return getAllNodes(self.dom.documentElement, tagPath) + return getAllNodes(self.doc.documentElement, tagPath) def getChildren(self, tagpath): """ returns the children of the given path""" @@ -133,7 +133,7 @@ class XmlFile(object): def addNode(self, tagPath, newnode = None): "this adds the newnode under given tag path" self.verifyRootTag() - return addNode(self.dom.documentElement, tagPath, newnode) + return addNode(self.doc.documentElement, tagPath, newnode) def addNodeUnder(self, node, tagPath, newnode = None): "this adds the new stuff under node and then following tag path" @@ -142,7 +142,7 @@ class XmlFile(object): def addChild(self, newnode): "add a new child node right under root element document" - self.dom.documentElement.appendChild(newnode) + self.doc.documentElement.appendChild(newnode) def addText(self, node, text): "add text to node"