From f8d4d86d1c7004f2243c8fb59574779a132b6e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Fri, 10 Jun 2005 17:20:48 +0000 Subject: [PATCH] * xmlfile: put the fun back into minidom - make attr/text functions static - remove redundant getfirstnode function - don't raise exception when tag path not found, return None - implement a method to get only child elements * specfile: use the advanced routines --- src/installdb.py | 2 -- src/specfile.py | 24 +++++++++------- src/xmlfile.py | 75 +++++++++++++++++++++++++++++------------------- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/installdb.py b/src/installdb.py index 1819ad68..37325b3c 100644 --- a/src/installdb.py +++ b/src/installdb.py @@ -1,5 +1,3 @@ # -*- coding: utf-8 -*- # installation database - - diff --git a/src/specfile.py b/src/specfile.py index 95f4fe6c..59b38390 100644 --- a/src/specfile.py +++ b/src/specfile.py @@ -2,28 +2,32 @@ # read/write PISI source package specification file import xml.dom.minidom -from xmlfile import XmlFile +from xmlfile import * class PatchInfo: def __init__(self, filenm, ctype): self.filename = filenm self.compressionType = ctype + def __init__(node): + self.getNodeText(node) + class SpecFile(XmlFile): """A class for reading/writing from/to a PSPEC (PISI SPEC) file.""" def read(self, filename): """Read PSPEC file""" self.readxml(filename) - self.sourceName = self.getFirstChildText("PSPEC/Source/Name") - archiveNode = self.getFirstNode("PSPEC/Source/Archive") - self.archiveUri = self.getNodeText(archiveNode).strip() - self.archiveType = self.getNodeAttribute(archiveNode, "archType") - self.archiveHash = self.getNodeAttribute(archiveNode, "md5sum") - patches = self.getNode("PSPEC/Source/Patches") - #patches = self.dom.getElementsByTagName("PSPEC") - #for x in patc - + self.sourceName = self.getChildText("PSPEC/Source/Name") + archiveNode = self.getNode("PSPEC/Source/Archive") + self.archiveUri = getNodeText(archiveNode).strip() + self.archiveType = getNodeAttribute(archiveNode, "archType") + self.archiveHash = getNodeAttribute(archiveNode, "md5sum") + patchElts = self.getChildElts("PSPEC/Source/Patches") + #patches = [ for p in patchesNode ] + for p in patchElts: + print getNodeText(p) + def verify(self): """Verify PSPEC structures, are they what we want of them?""" return True diff --git a/src/xmlfile.py b/src/xmlfile.py index f98427bd..42a503ff 100644 --- a/src/xmlfile.py +++ b/src/xmlfile.py @@ -1,15 +1,37 @@ +# -*- coding: utf-8 -*- # some helper functions for using minidom -import xml.dom.minidom +import xml.dom.minidom as mdom class XmlError(Exception): pass +# static functions + +def getNodeAttribute(node, attrname): + for i in range(node.attributes.length): + attr = node.attributes.item(i) + if attr.name == attrname: + return attr.childNodes[0].data + +def getNodeText(node): + # get the first child + try: + child = node.childNodes[0] + except IndexError: + return None + if child.nodeType == child.TEXT_NODE: + return child.data + else: + raise XmlError("getNodeText: Expected text node, got something else!") + +# 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 = xml.dom.minidom.parse(filenm) + self.dom = mdom.parse(filenm) def writexml(self, filenm): f = file(filenm,'w') @@ -17,7 +39,21 @@ class XmlFile(object): def getChildren(self, tagpath): """ returns the children of the given path""" - return self.dom.getChildren() + node = self.getNode(tagpath) + return node.childNodes + + # get only elements of a given type + # BUG: 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, slightly better than Serdar's sol'n :> -- exa + def getChildElts(self, tagpath): + """ returns the children of the given path, only with given type """ + 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. @@ -33,17 +69,17 @@ class XmlFile(object): nodelist = self.dom.getElementsByTagName(tags[0]) if len(nodelist)==0: - raise XmlError("Root tag for " % tagpath % " not found") + return None # not found node = nodelist[0] # discard other matches for nodename in tags[1:]: nodelist = node.getElementsByTagName(nodename) if len(nodelist)==0: - raise XmlError("Tag path " % tagpath % " broken") + return None else: node = nodelist[0] - return node + return node def getAllNodes(self, nodepath): """returns all trees corresponding to given path. @@ -52,31 +88,10 @@ class XmlFile(object): returns an array of nodes under PSPEC/Source""" raise XmlError("Not implemented!") - def getFirstNode(self, nodepath): - try: - return self.getNode(nodepath) - except IndexError: - return None - - def getNodeText(self, node): - # get the first child - try: - child = node.childNodes[0] - except IndexError: - return None - - if child.nodeType == child.TEXT_NODE: - return child.data - - def getFirstChildText(self, nodepath): - node = self.getFirstNode(nodepath) + def getChildText(self, tagpath): + node = self.getNode(tagpath) if not node: return None - return self.getNodeText(node) + return getNodeText(node) - def getNodeAttribute(self, node, attrname): - for i in range(node.attributes.length): - attr = node.attributes.item(i) - if attr.name == attrname: - return attr.childNodes[0].data