* revamp xmlfile

- yes, I'm hooked to it. XML sucks.
  - define a root element tag name which actually gets checked.
    Rationale: an XML document has only one root element
  - put all helper functions that don't depend on a dom object outside
    the class, helps me write modular parsers in specfile
* specfile:
  - update tags
This commit is contained in:
Eray Özkural
2005-06-10 19:57:39 +00:00
parent c6104f2edf
commit 9b55467e1d
3 changed files with 90 additions and 46 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ def main():
# doing the real job.
pb = PisiBuild(pspec)
util.information("Building PISI package for: %s\n" % pb.spec.sourceName)
util.information("Building PISI source package: %s\n" % pb.spec.sourceName)
util.information("Fetching source from: %s (be patient)\n" % pb.spec.archiveUri)
pb.fetchArchive(doProgress)
+20 -6
View File
@@ -18,27 +18,41 @@ class DepInfo:
self.package = getNodeText(node).strip()
self.versionFrom = getNodeAttribute(node, "versionFrom")
class PackageInfo:
def __init__(self, node):
comps = getChildElts(node)
self.name = comps[0]
class SpecFile(XmlFile):
"""A class for reading/writing from/to a PSPEC (PISI SPEC) file."""
def __init__(self):
XmlFile.__init__(self,"PSPEC")
def read(self, filename):
"""Read PSPEC file"""
self.readxml(filename)
self.sourceName = self.getChildText("PSPEC/Source/Name")
archiveNode = self.getNode("PSPEC/Source/Archive")
self.sourceName = self.getChildText("Source/Name")
archiveNode = self.getNode("Source/Archive")
self.archiveUri = getNodeText(archiveNode).strip()
self.archiveType = getNodeAttribute(archiveNode, "archType")
self.archiveHash = getNodeAttribute(archiveNode, "md5sum")
patchElts = self.getChildElts("PSPEC/Source/Patches")
patchElts = self.getChildElts("Source/Patches")
patches = [ PatchInfo(p) for p in patchElts ]
for x in patches:
print "patch fn:", x.filename
print "patch ct:", x.compressionType
buildDepElts = self.getChildElts("PSPEC/Source/BuildDependencies")
buildDepElts = self.getChildElts("Source/BuildDependencies")
buildDeps = [DepInfo(d) for d in buildDepElts]
for x in buildDeps:
print "patch nm:", x.package
print "patch vf:", x.versionFrom
print "dep nm:", x.package
print "dep vf:", x.versionFrom
# find all binary packages
packageElts = self.getAllNodes("Package")
packages = [PackageInfo(p) for p in packageElts]
print packages
def verify(self):
"""Verify PSPEC structures, are they what we want of them?"""
+69 -39
View File
@@ -25,22 +25,86 @@ def getNodeText(node):
else:
raise XmlError("getNodeText: Expected text node, got something else!")
# get only child elements
def getChildElts(node):
"""get only child elements"""
return filter(lambda x:x.nodeType==x.ELEMENT_NODE, node.childNodes)
def getNode(node, tagpath):
"""returns the *first* matching node for given tag path."""
tags=tagpath.split('/')
# iterative code to search for the path
# get DOM for top node
nodeList = node.getElementsByTagName(tags[0])
if len(nodeList)==0:
return None # not found
node = nodeList[0] # discard other matches
for tag in tags[1:]:
nodeList = node.getElementsByTagName(tag)
if len(nodeList)==0:
return None
else:
node = nodeList[0]
return node
def getAllNodes(node, tags):
"""retrieve all nodes that match a given tag path."""
print "tags = ", tags
if len(tags)==0:
return None
nodeList = node.getElementsByTagName(tags[0])
if len(nodeList)==0:
return None
for tag in tags[1:]:
results = map(lambda x: x.getElementsByTagName(tag),nodeList)
nodeList = []
for x in results:
nodeList.extend(x)
pass # emacs indentation error, keep it here
if len(nodeList)==0:
return None
return nodeList
# xmlfile class that further abstracts a dom object
class XmlFile(object):
"""A class for retrieving information from an XML file"""
def readxml(self, filenm):
self.dom = mdom.parse(filenm)
def __init__(self, rootTag):
self.rootTag = rootTag
def writexml(self, filenm):
f = file(filenm,'w')
def readxml(self, fileName):
self.dom = mdom.parse(fileName)
def writexml(self, fileName):
f = file(fileName,'w')
self.dom.writexml(f)
def verifyRootTag(self):
if self.dom.documentElement.tagName != self.rootTag:
raise XmlError("Root tagname not " % self.rootTag % " as expected")
def getNode(self, tagPath):
"""returns the *first* matching node for given tag path."""
self.verifyRootTag()
return getNode(self.dom.documentElement, tagPath)
def getAllNodes(self, tagPath):
"""returns all nodes matching a given tag path."""
self.verifyRootTag()
tags = tagPath.split('/')
return getAllNodes(self.dom.documentElement, tags)
def getChildren(self, tagpath):
""" returns the children of the given path"""
node = self.getNode(tagpath)
@@ -59,43 +123,9 @@ class XmlFile(object):
node = self.getNode(tagpath)
return filter(lambda x:x.nodeType==x.ELEMENT_NODE, node.childNodes)
def getNode(self, tagpath):
"""returns the node for given *unique* path of the node.
getNode("PSPEC/Source")
returns the node with the tag path PSPEC/Source"""
tags=tagpath.split('/')
# code to search for the path
# get DOM for top node
nodelist = self.dom.getElementsByTagName(tags[0])
if len(nodelist)==0:
return None # not found
node = nodelist[0] # discard other matches
for nodename in tags[1:]:
nodelist = node.getElementsByTagName(nodename)
if len(nodelist)==0:
return None
else:
node = nodelist[0]
return node
def getAllNodes(self, nodepath):
"""returns all trees corresponding to given path.
getAllNodes("PSPEC/Source")
returns an array of nodes under PSPEC/Source"""
raise XmlError("Not implemented!")
def getChildText(self, tagpath):
node = self.getNode(tagpath)
if not node:
return None
return getNodeText(node)