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:
+39
-54
@@ -9,17 +9,17 @@
|
||||
#
|
||||
# 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
|
||||
|
||||
this implementation uses piksemel, a fast C-based XML library
|
||||
xmlext is a helper module for accessing XML files using
|
||||
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
|
||||
an 'old library'
|
||||
|
||||
this implementation uses piksemel
|
||||
"""
|
||||
|
||||
import gettext
|
||||
@@ -27,11 +27,23 @@ __trans = gettext.translation('pisi', fallback=True)
|
||||
_ = __trans.ugettext
|
||||
|
||||
import pisi
|
||||
from piksemel import *
|
||||
import piksemel as iks
|
||||
|
||||
class XmlError(pisi.Error):
|
||||
"named this way because the class if mostly used with an import *"
|
||||
pass
|
||||
def getAllNodes(node, tagPath):
|
||||
"""retrieve all nodes that match a given tag path."""
|
||||
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):
|
||||
"""get named attribute from DOM node"""
|
||||
@@ -39,23 +51,22 @@ def getNodeAttribute(node, attrname):
|
||||
|
||||
def getChildElts(node):
|
||||
"""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):
|
||||
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 = ""):
|
||||
"""get the first child and expect it to be text!"""
|
||||
if tagpath!="":
|
||||
node = getNode(node, tagpath)
|
||||
try:
|
||||
if not node:
|
||||
return None
|
||||
child = node.firstChild()
|
||||
except IndexError:
|
||||
if not child:
|
||||
return None
|
||||
except AttributeError: # no node by that name
|
||||
return None
|
||||
if child.type() == CDATA: #FIXME: ??? TEXT type?
|
||||
# in any case, strip whitespaces... :/ you don't like it?
|
||||
if child.type() == iks.DATA:
|
||||
# in any case, strip whitespaces...
|
||||
return child.data().strip()
|
||||
else:
|
||||
raise XmlError(_("getNodeText: Expected text node, got something else!"))
|
||||
@@ -87,28 +98,15 @@ def getNode(node, tagpath):
|
||||
node = currentNode
|
||||
return currentNode
|
||||
|
||||
def getAllNodes(node, tagPath):
|
||||
"""retrieve all nodes that match a given tag path."""
|
||||
|
||||
#FIXME: better tag split
|
||||
tags = tagPath.split('/')
|
||||
|
||||
if len(tags) == 0:
|
||||
return []
|
||||
|
||||
nodeList = [node] # basis case
|
||||
|
||||
def addText(node, tagpath, text):
|
||||
if tagpath != "":
|
||||
tags = tagpath.split("/")
|
||||
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
|
||||
if node.getTag(tag):
|
||||
node = node.getTag(tag)
|
||||
else:
|
||||
node = node.appendTag(tag)
|
||||
node.appendData(text)
|
||||
|
||||
def createTagPath(node, tags):
|
||||
"""create new child at the end of a tag chain starting from node
|
||||
@@ -123,7 +121,7 @@ def addTagPath(node, tags, newnode=None):
|
||||
"""add newnode at the end of a tag chain, smart one"""
|
||||
node = createTagPath(node, tags)
|
||||
if newnode: # node to add specified
|
||||
node.appendTag(newnode)
|
||||
node.appendChild(newnode)
|
||||
return node
|
||||
|
||||
def addNode(node, tagpath, newnode = None, branch=True):
|
||||
@@ -160,16 +158,3 @@ def addNode(node, tagpath, newnode = None, branch=True):
|
||||
return addTagPath(node, tags, newnode)
|
||||
|
||||
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
@@ -9,26 +9,32 @@
|
||||
#
|
||||
# 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
|
||||
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
|
||||
__trans = gettext.translation('pisi', fallback=True)
|
||||
_ = __trans.ugettext
|
||||
|
||||
import codecs
|
||||
import exceptions
|
||||
|
||||
import piksemel as iks
|
||||
|
||||
import pisi
|
||||
from piksemel import *
|
||||
from pisi.file import File
|
||||
from pisi.util import join_path as join
|
||||
|
||||
class Error(pisi.Error):
|
||||
"named this way because the class if mostly used with an import *"
|
||||
pass
|
||||
|
||||
class XmlFile(object):
|
||||
@@ -39,102 +45,26 @@ class XmlFile(object):
|
||||
|
||||
def newDocument(self):
|
||||
"""clear DOM"""
|
||||
self.doc = newDocument(self.rootTag)
|
||||
self.doc = iks.newDocument(self.rootTag)
|
||||
|
||||
def unlink(self):
|
||||
"""deallocate DOM structure"""
|
||||
pass # piksemel is garbage collected!
|
||||
pass # piksemel isnt as lame as minidom
|
||||
|
||||
def rootNode(self):
|
||||
"""returns root document element"""
|
||||
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:
|
||||
self.doc = parse(fileName)
|
||||
except ParseError, inst:
|
||||
raise XmlError(_("File '%s' has invalid XML: %s\n") % (fileName,
|
||||
str(inst)))
|
||||
self.doc = iks.parse(localpath)
|
||||
return self.doc
|
||||
except Exception, e:
|
||||
raise Error(_("File '%s' has invalid XML") % (localpath) )
|
||||
|
||||
def writexml(self, fileName):
|
||||
f = codecs.open(fileName,'w', "utf-8")
|
||||
def writexml(self, uri, tmpDir = '/tmp', sha1sum=False, compress=None, sign=None):
|
||||
f = File(uri, File.write, sha1sum=sha1sum, compress=compress, sign=sign)
|
||||
f.write(self.doc.toPrettyString())
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user