güncelleme, bunları kullanmak için autoxml de ufak bi peç lazım, onu ayrıca koyucam

test etmeden aktive etmiyorum

minidom implementasyonu da çalışmıyor bu arada
This commit is contained in:
Gürer Özen
2006-04-19 06:26:53 +00:00
parent 5c601fec0a
commit e921de2f96
2 changed files with 66 additions and 151 deletions
+42 -57
View File
@@ -9,17 +9,17 @@
# #
# Please read the COPYING file. # Please read the COPYING file.
# #
# Authors: Eray Ozkural <eray@pardus.org.tr>
# Baris Metin <baris@pardus.org.tr
# Gurer Ozen <gurer@pardus.org.tr>
""" """
some helper functions for high-level access to XML structures xmlext is a helper module for accessing XML files using
xml.dom.minidom . It is a convenient wrapper for some
this implementation uses piksemel, a fast C-based XML library DOM functions, and provides path based get/add functions
as in KDE API.
function names are mixedCase for compatibility with minidom, function names are mixedCase for compatibility with minidom,
an old library an 'old library'
this implementation uses piksemel
""" """
import gettext import gettext
@@ -27,11 +27,23 @@ __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext _ = __trans.ugettext
import pisi import pisi
from piksemel import * import piksemel as iks
class XmlError(pisi.Error): def getAllNodes(node, tagPath):
"named this way because the class if mostly used with an import *" """retrieve all nodes that match a given tag path."""
pass 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): def getNodeAttribute(node, attrname):
"""get named attribute from DOM node""" """get named attribute from DOM node"""
@@ -39,23 +51,22 @@ def getNodeAttribute(node, attrname):
def getChildElts(node): def getChildElts(node):
"""get only child elements""" """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): 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 = ""): def getNodeText(node, tagpath = ""):
"""get the first child and expect it to be text!""" """get the first child and expect it to be text!"""
if tagpath!="": if tagpath!="":
node = getNode(node, tagpath) node = getNode(node, tagpath)
try: if not node:
child = node.firstChild() return None
except IndexError: child = node.firstChild()
if not child:
return None return None
except AttributeError: # no node by that name if child.type() == iks.DATA:
return None # in any case, strip whitespaces...
if child.type() == CDATA: #FIXME: ??? TEXT type?
# in any case, strip whitespaces... :/ you don't like it?
return child.data().strip() return child.data().strip()
else: else:
raise XmlError(_("getNodeText: Expected text node, got something else!")) raise XmlError(_("getNodeText: Expected text node, got something else!"))
@@ -87,28 +98,15 @@ def getNode(node, tagpath):
node = currentNode node = currentNode
return currentNode return currentNode
def getAllNodes(node, tagPath): def addText(node, tagpath, text):
"""retrieve all nodes that match a given tag path.""" if tagpath != "":
tags = tagpath.split("/")
#FIXME: better tag split for tag in tags:
tags = tagPath.split('/') if node.getTag(tag):
node = node.getTag(tag)
if len(tags) == 0: else:
return [] node = node.appendTag(tag)
node.appendData(text)
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 createTagPath(node, tags): def createTagPath(node, tags):
"""create new child at the end of a tag chain starting from node """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""" """add newnode at the end of a tag chain, smart one"""
node = createTagPath(node, tags) node = createTagPath(node, tags)
if newnode: # node to add specified if newnode: # node to add specified
node.appendTag(newnode) node.appendChild(newnode)
return node return node
def addNode(node, tagpath, newnode = None, branch=True): def addNode(node, tagpath, newnode = None, branch=True):
"""add a new node at the end of the tree and returns it """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 addTagPath(node, tags, newnode)
return node 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)
+24 -94
View File
@@ -9,26 +9,32 @@
# #
# Please read the COPYING file. # Please read the COPYING file.
# #
# Authors: Eray Ozkural <eray@pardus.org.tr>
# Gurer Ozen <gurer@pardus.org.tr>
# Baris Metin <baris@pardus.org.tr
""" """
XmlFile class further abstracts a dom object using the XmlFile class further abstracts a dom object using the
high-level dom functions provided in xmlext module high-level dom functions provided in xmlext module (and sorely lacking
in xml.dom :( )
this implementation uses piksemel, a fast C-based XML library function names are mixedCase for compatibility with minidom,
an 'old library'
this implementation uses piksemel
""" """
import gettext import gettext
__trans = gettext.translation('pisi', fallback=True) __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext _ = __trans.ugettext
import codecs
import exceptions
import piksemel as iks
import pisi import pisi
from piksemel import * from pisi.file import File
from pisi.util import join_path as join
class Error(pisi.Error): class Error(pisi.Error):
"named this way because the class if mostly used with an import *"
pass pass
class XmlFile(object): class XmlFile(object):
@@ -39,102 +45,26 @@ class XmlFile(object):
def newDocument(self): def newDocument(self):
"""clear DOM""" """clear DOM"""
self.doc = newDocument(self.rootTag) self.doc = iks.newDocument(self.rootTag)
def unlink(self): def unlink(self):
"""deallocate DOM structure""" """deallocate DOM structure"""
pass # piksemel is garbage collected! pass # piksemel isnt as lame as minidom
def rootNode(self): def rootNode(self):
"""returns root document element""" """returns root document element"""
return self.doc return self.doc
def readxml(self, fileName): def readxml(self, uri, tmpDir='/tmp', sha1sum=False, compress=None, sign=None):
uri = File.make_uri(uri)
localpath = File.download(uri, tmpDir,sha1sum=sha1sum,compress=compress,sign=sign)
try: try:
self.doc = parse(fileName) self.doc = iks.parse(localpath)
except ParseError, inst: return self.doc
raise XmlError(_("File '%s' has invalid XML: %s\n") % (fileName, except Exception, e:
str(inst))) raise Error(_("File '%s' has invalid XML") % (localpath) )
def writexml(self, fileName): def writexml(self, uri, tmpDir = '/tmp', sha1sum=False, compress=None, sign=None):
f = codecs.open(fileName,'w', "utf-8") f = File(uri, File.write, sha1sum=sha1sum, compress=compress, sign=sign)
f.write(self.doc.toPrettyString()) f.write(self.doc.toPrettyString())
f.close() f.close()
def verifyRootTag(self):
actual_roottag = self.rootNode().tagName
if actual_roottag != self.rootTag:
raise Error(_("Root tagname %s not identical to %s as expected") %
(actual_roottag, self.rootTag) )
# construction helpers
#def newNode(self, tag):
# return self.dom.createElement(tag)
#def newTextNode(self, text):
# return self.dom.createTextNode(text)
#def newAttribute(self, attr):
# return self.dom.createAttribute(attr)
# read helpers
def getNode(self, tagPath = ""):
"""returns the *first* matching node for given tag path."""
self.verifyRootTag()
return getNode(self.doc, tagPath)
def getNodeText(self, tagPath):
"""returns the text of *first* matching node for given tag path."""
node = self.getNode(tagPath)
if not node:
return None
return getNodeText(node)
def getAllNodes(self, tagPath):
"""returns all nodes matching a given tag path."""
self.verifyRootTag()
return getAllNodes(self.doc, tagPath)
def getChildren(self, tagpath):
""" returns the children of the given path"""
node = self.getNode(tagpath)
return [x for x in parent.childNodes]
# get only child elements
def getChildElts(self, tagpath):
""" returns the children of the given path, only with given type """
return getChildElts(self.getNode(tagpath))
# write helpers
def addNode(self, tagPath, newnode = None):
"this adds the newnode under given tag path"
self.verifyRootTag()
return addNode(self.doc, tagPath, newnode)
def addNodeUnder(self, node, tagPath, newnode = None):
"this adds the new stuff under node and then following tag path"
self.verifyRootTag()
return addNode(node, tagPath, newnode)
def addChild(self, newnode):
"add a new child node right under root element document"
self.dom.documentElement.appendChild(newnode)
def addText(self, node, text):
"add text to node"
raise Error('NOT IMPLEMENTED')
#node.appendChild(self.newTextNode(text))
def addTextNode(self, tagPath, text):
"add a text node with given tag path"
raise Error('NOT IMPLEMENTED')
#node = self.addNode(tagPath, self.newTextNode(text))
#return node
def addTextNodeUnder(self, node, tagPath, text):
"add a text node under given node with tag path (phew)"
raise Error('NOT IMPLEMENTED')
#return self.addNodeUnder(node, tagPath, self.newTextNode(text))