* 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
This commit is contained in:
Eray Özkural
2005-11-10 11:21:49 +00:00
parent a7b571e986
commit 67d3fea7cc
3 changed files with 30 additions and 25 deletions
+8 -3
View File
@@ -378,7 +378,8 @@ class autoxml(oo.autosuper, oo.autoprop):
cls.__ne__ = notequal cls.__ne__ = notequal
if xmlfile_support: 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) self.readxml(filename)
errs = [] errs = []
self.decode(self.rootNode(), 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) errs.append(_("autoxml.read: File '%s' has errors") % filename)
raise Error(*errs) raise Error(*errs)
self.unlink() if eraseDoc:
self.unlink() # get rid of the tree
errs = self.errors() errs = self.errors()
if errs: if errs:
errs.append(_("autoxml.read: File '%s' has errors") % filename) errs.append(_("autoxml.read: File '%s' has errors") % filename)
raise Error(*errs) 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() errs = self.errors()
if errs: if errs:
errs.append(_("autoxml.write: object validation has failed")) 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) errs.append(_("autoxml.write: File encoding '%s' has errors") % filename)
raise Error(*errs) raise Error(*errs)
self.writexml(filename) self.writexml(filename)
if eraseDoc:
self.unlink() # get rid of the tree
cls.read = read cls.read = read
cls.write = write cls.write = write
+10 -10
View File
@@ -60,8 +60,8 @@ class XmlFile(object):
def unlink(self): def unlink(self):
"""deallocate DOM structure""" """deallocate DOM structure"""
#del self.doc del self.doc
self.doc = None # why? #self.doc = None # why?
def rootNode(self): def rootNode(self):
"""returns root document element""" """returns root document element"""
@@ -76,7 +76,7 @@ class XmlFile(object):
def writexml(self, path): def writexml(self, path):
f = file(path, 'w') f = file(path, 'w')
Print(self.rootNode(), stream = f) PrettyPrint(self.rootNode(), stream = f)
f.close() f.close()
def verifyRootTag(self): def verifyRootTag(self):
@@ -88,20 +88,20 @@ class XmlFile(object):
# construction helpers # construction helpers
def newNode(self, tag): def newNode(self, tag):
return self.dom.createElementNS(None, tag) return self.doc.createElementNS(None, tag)
def newTextNode(self, text): def newTextNode(self, text):
return self.dom.createTextNode(text) return self.doc.createTextNode(text)
def newAttribute(self, attr): def newAttribute(self, attr):
return self.dom.createAttribute(attr) return self.doc.createAttribute(attr)
# read helpers # read helpers
def getNode(self, tagPath = ""): def getNode(self, tagPath = ""):
"""returns the *first* matching node for given tag path.""" """returns the *first* matching node for given tag path."""
self.verifyRootTag() self.verifyRootTag()
return getNode(self.dom.documentElement, tagPath) return getNode(self.doc.documentElement, tagPath)
def getNodeText(self, tagPath): def getNodeText(self, tagPath):
"""returns the text of *first* matching node for given tag path.""" """returns the text of *first* matching node for given tag path."""
@@ -113,7 +113,7 @@ class XmlFile(object):
def getAllNodes(self, tagPath): def getAllNodes(self, tagPath):
"""returns all nodes matching a given tag path.""" """returns all nodes matching a given tag path."""
self.verifyRootTag() self.verifyRootTag()
return getAllNodes(self.dom.documentElement, tagPath) return getAllNodes(self.doc.documentElement, tagPath)
def getChildren(self, tagpath): def getChildren(self, tagpath):
""" returns the children of the given path""" """ returns the children of the given path"""
@@ -142,7 +142,7 @@ class XmlFile(object):
def addNode(self, tagPath, newnode = None): def addNode(self, tagPath, newnode = None):
"this adds the newnode under given tag path" "this adds the newnode under given tag path"
self.verifyRootTag() self.verifyRootTag()
return addNode(self.dom.documentElement, tagPath, newnode) return addNode(self.doc.documentElement, tagPath, newnode)
def addNodeUnder(self, node, tagPath, newnode = None): def addNodeUnder(self, node, tagPath, newnode = None):
"this adds the new stuff under node and then following tag path" "this adds the new stuff under node and then following tag path"
@@ -151,7 +151,7 @@ class XmlFile(object):
def addChild(self, newnode): def addChild(self, newnode):
"add a new child node right under root element document" "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): def addText(self, node, text):
"add text to node" "add text to node"
+12 -12
View File
@@ -48,26 +48,26 @@ class XmlFile(object):
def newDocument(self): def newDocument(self):
"""clear DOM""" """clear DOM"""
impl = mdom.getDOMImplementation() impl = mdom.getDOMImplementation()
self.dom = impl.createDocument(None, self.rootTag, None) self.doc = impl.createDocument(None, self.rootTag, None)
def unlink(self): def unlink(self):
"""deallocate DOM structure""" """deallocate DOM structure"""
self.dom.unlink() self.doc.unlink()
def rootNode(self): def rootNode(self):
"""returns root document element""" """returns root document element"""
return self.dom.documentElement return self.doc.documentElement
def readxml(self, fileName): def readxml(self, fileName):
try: try:
self.dom = mdom.parse(fileName) self.doc = mdom.parse(fileName)
except ExpatError, inst: except ExpatError, inst:
raise Error(_("File '%s' has invalid XML: %s\n") % (fileName, raise Error(_("File '%s' has invalid XML: %s\n") % (fileName,
str(inst))) str(inst)))
def writexml(self, fileName): def writexml(self, fileName):
f = codecs.open(fileName,'w', "utf-8") f = codecs.open(fileName,'w', "utf-8")
f.write(self.dom.toprettyxml()) f.write(self.doc.toprettyxml())
f.close() f.close()
def verifyRootTag(self): def verifyRootTag(self):
@@ -79,20 +79,20 @@ class XmlFile(object):
# construction helpers # construction helpers
def newNode(self, tag): def newNode(self, tag):
return self.dom.createElement(tag) return self.doc.createElement(tag)
def newTextNode(self, text): def newTextNode(self, text):
return self.dom.createTextNode(text) return self.doc.createTextNode(text)
def newAttribute(self, attr): def newAttribute(self, attr):
return self.dom.createAttribute(attr) return self.doc.createAttribute(attr)
# read helpers # read helpers
def getNode(self, tagPath = ""): def getNode(self, tagPath = ""):
"""returns the *first* matching node for given tag path.""" """returns the *first* matching node for given tag path."""
self.verifyRootTag() self.verifyRootTag()
return getNode(self.dom.documentElement, tagPath) return getNode(self.doc.documentElement, tagPath)
def getNodeText(self, tagPath): def getNodeText(self, tagPath):
"""returns the text of *first* matching node for given tag path.""" """returns the text of *first* matching node for given tag path."""
@@ -104,7 +104,7 @@ class XmlFile(object):
def getAllNodes(self, tagPath): def getAllNodes(self, tagPath):
"""returns all nodes matching a given tag path.""" """returns all nodes matching a given tag path."""
self.verifyRootTag() self.verifyRootTag()
return getAllNodes(self.dom.documentElement, tagPath) return getAllNodes(self.doc.documentElement, tagPath)
def getChildren(self, tagpath): def getChildren(self, tagpath):
""" returns the children of the given path""" """ returns the children of the given path"""
@@ -133,7 +133,7 @@ class XmlFile(object):
def addNode(self, tagPath, newnode = None): def addNode(self, tagPath, newnode = None):
"this adds the newnode under given tag path" "this adds the newnode under given tag path"
self.verifyRootTag() self.verifyRootTag()
return addNode(self.dom.documentElement, tagPath, newnode) return addNode(self.doc.documentElement, tagPath, newnode)
def addNodeUnder(self, node, tagPath, newnode = None): def addNodeUnder(self, node, tagPath, newnode = None):
"this adds the new stuff under node and then following tag path" "this adds the new stuff under node and then following tag path"
@@ -142,7 +142,7 @@ class XmlFile(object):
def addChild(self, newnode): def addChild(self, newnode):
"add a new child node right under root element document" "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): def addText(self, node, text):
"add text to node" "add text to node"