* implement xmlfile with piksemel

* some req. functions are missing, go back to mdom
This commit is contained in:
Eray Özkural
2005-11-09 22:48:23 +00:00
parent c8f341ca62
commit a11509db79
4 changed files with 30 additions and 37 deletions
+3 -1
View File
@@ -462,7 +462,7 @@ class autoxml(oo.autosuper, oo.autoprop):
tag_type = spec[0] tag_type = spec[0]
assert type(tag_type) == type(type) assert type(tag_type) == type(type)
def readtext(node, blah): def readtext(node, blah):
node.normalize() #node.normalize() # piksemel doesn't have this
return getNodeText(node) return getNodeText(node)
def writetext(node, blah, text): def writetext(node, blah, text):
addText(node, "", text) addText(node, "", text)
@@ -687,6 +687,7 @@ class autoxml(oo.autosuper, oo.autoprop):
dummy = node.ownerDocument.createElement("Dummy") dummy = node.ownerDocument.createElement("Dummy")
dummy.appendChild(node) dummy.appendChild(node)
l.append(decode_item(dummy, errs, where + unicode("[%s]" % ix))) l.append(decode_item(dummy, errs, where + unicode("[%s]" % ix)))
#l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
ix += 1 ix += 1
return l return l
@@ -699,6 +700,7 @@ class autoxml(oo.autosuper, oo.autoprop):
else: else:
listnode = node listnode = node
encode_item(listnode, item, errs) encode_item(listnode, item, errs)
#encode_item(node, item, errs)
else: else:
if req is mandatory: if req is mandatory:
errs.append(_('Mandatory list empty')) errs.append(_('Mandatory list empty'))
+1 -1
View File
@@ -12,7 +12,7 @@
# Authors: Eray Ozkural <eray@uludag.org.tr> # Authors: Eray Ozkural <eray@uludag.org.tr>
try: try:
#zimbabwe # comment out to disable piks zimbabwe # comment out to disable piks
from xmlextpiks import * from xmlextpiks import *
except: except:
print 'xmlext: piksemel implementation cannot be loaded, falling back to minidom' print 'xmlext: piksemel implementation cannot be loaded, falling back to minidom'
+1 -1
View File
@@ -12,7 +12,7 @@
# Authors: Eray Ozkural <eray@uludag.org.tr> # Authors: Eray Ozkural <eray@uludag.org.tr>
try: try:
#horagata # comment out to disable piks horagata # comment out to disable piks
from xmlextpiks import * from xmlextpiks import *
from xmlfilepiks import * from xmlfilepiks import *
except: except:
+25 -34
View File
@@ -25,6 +25,7 @@ __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext _ = __trans.ugettext
import pisi import pisi
from piksemel import *
class Error(pisi.Error): class Error(pisi.Error):
"named this way because the class if mostly used with an import *" "named this way because the class if mostly used with an import *"
@@ -39,27 +40,26 @@ class XmlFile(object):
def newDOM(self): def newDOM(self):
"""clear DOM""" """clear DOM"""
impl = mdom.getDOMImplementation() self.doc = newDocument(self.rootTag)
self.dom = impl.createDocument(None, self.rootTag, None)
def unlink(self): def unlink(self):
"""deallocate DOM structure""" """deallocate DOM structure"""
self.dom.unlink() pass # piksemel is garbage collected!
def rootNode(self): def rootNode(self):
"""returns root document element""" """returns root document element"""
return self.dom.documentElement return self.doc
def readxml(self, fileName): def readxml(self, fileName):
try: try:
self.dom = mdom.parse(fileName) self.doc = parse(fileName)
except ExpatError, inst: except ParseError, inst:
raise XmlError(_("File '%s' has invalid XML: %s\n") % (fileName, raise XmlError(_("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.toPrettyString())
f.close() f.close()
def verifyRootTag(self): def verifyRootTag(self):
@@ -70,21 +70,21 @@ class XmlFile(object):
# construction helpers # construction helpers
def newNode(self, tag): #def newNode(self, tag):
return self.dom.createElement(tag) # return self.dom.createElement(tag)
def newTextNode(self, text): #def newTextNode(self, text):
return self.dom.createTextNode(text) # return self.dom.createTextNode(text)
def newAttribute(self, attr): #def newAttribute(self, attr):
return self.dom.createAttribute(attr) # return self.dom.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, 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."""
@@ -96,36 +96,24 @@ 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, tagPath)
def getChildren(self, tagpath): def getChildren(self, tagpath):
""" returns the children of the given path""" """ returns the children of the given path"""
node = self.getNode(tagpath) node = self.getNode(tagpath)
return node.childNodes return [x for x in parent.childNodes]
# get only elements of a given type
#FIXME: this doesn't work
def getChildrenWithType(self, tagpath, type):
""" returns the children of the given path, only with given type """
node = self.getNode(tagpath)
return filter(lambda x:x.nodeType == type, node.childNodes)
# get only child elements # get only child elements
def getChildElts(self, tagpath): def getChildElts(self, tagpath):
""" returns the children of the given path, only with given type """ """ returns the children of the given path, only with given type """
node = self.getNode(tagpath) return getChildElts(self.getNode(tagpath))
try:
return filter(lambda x:x.nodeType == x.ELEMENT_NODE,
node.childNodes)
except AttributeError:
return None
# write helpers # write helpers
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, 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"
@@ -138,13 +126,16 @@ class XmlFile(object):
def addText(self, node, text): def addText(self, node, text):
"add text to node" "add text to node"
node.appendChild(self.newTextNode(text)) raise Error('NOT IMPLEMENTED')
#node.appendChild(self.newTextNode(text))
def addTextNode(self, tagPath, text): def addTextNode(self, tagPath, text):
"add a text node with given tag path" "add a text node with given tag path"
node = self.addNode(tagPath, self.newTextNode(text)) raise Error('NOT IMPLEMENTED')
return node #node = self.addNode(tagPath, self.newTextNode(text))
#return node
def addTextNodeUnder(self, node, tagPath, text): def addTextNodeUnder(self, node, tagPath, text):
"add a text node under given node with tag path (phew)" "add a text node under given node with tag path (phew)"
return self.addNodeUnder(node, tagPath, self.newTextNode(text)) raise Error('NOT IMPLEMENTED')
#return self.addNodeUnder(node, tagPath, self.newTextNode(text))