* 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
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# installation database
|
# installation database
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+13
-9
@@ -2,27 +2,31 @@
|
|||||||
# read/write PISI source package specification file
|
# read/write PISI source package specification file
|
||||||
|
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom
|
||||||
from xmlfile import XmlFile
|
from xmlfile import *
|
||||||
|
|
||||||
class PatchInfo:
|
class PatchInfo:
|
||||||
def __init__(self, filenm, ctype):
|
def __init__(self, filenm, ctype):
|
||||||
self.filename = filenm
|
self.filename = filenm
|
||||||
self.compressionType = ctype
|
self.compressionType = ctype
|
||||||
|
|
||||||
|
def __init__(node):
|
||||||
|
self.getNodeText(node)
|
||||||
|
|
||||||
class SpecFile(XmlFile):
|
class SpecFile(XmlFile):
|
||||||
"""A class for reading/writing from/to a PSPEC (PISI SPEC) file."""
|
"""A class for reading/writing from/to a PSPEC (PISI SPEC) file."""
|
||||||
|
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
"""Read PSPEC file"""
|
"""Read PSPEC file"""
|
||||||
self.readxml(filename)
|
self.readxml(filename)
|
||||||
self.sourceName = self.getFirstChildText("PSPEC/Source/Name")
|
self.sourceName = self.getChildText("PSPEC/Source/Name")
|
||||||
archiveNode = self.getFirstNode("PSPEC/Source/Archive")
|
archiveNode = self.getNode("PSPEC/Source/Archive")
|
||||||
self.archiveUri = self.getNodeText(archiveNode).strip()
|
self.archiveUri = getNodeText(archiveNode).strip()
|
||||||
self.archiveType = self.getNodeAttribute(archiveNode, "archType")
|
self.archiveType = getNodeAttribute(archiveNode, "archType")
|
||||||
self.archiveHash = self.getNodeAttribute(archiveNode, "md5sum")
|
self.archiveHash = getNodeAttribute(archiveNode, "md5sum")
|
||||||
patches = self.getNode("PSPEC/Source/Patches")
|
patchElts = self.getChildElts("PSPEC/Source/Patches")
|
||||||
#patches = self.dom.getElementsByTagName("PSPEC")
|
#patches = [ for p in patchesNode ]
|
||||||
#for x in patc
|
for p in patchElts:
|
||||||
|
print getNodeText(p)
|
||||||
|
|
||||||
def verify(self):
|
def verify(self):
|
||||||
"""Verify PSPEC structures, are they what we want of them?"""
|
"""Verify PSPEC structures, are they what we want of them?"""
|
||||||
|
|||||||
+45
-30
@@ -1,15 +1,37 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
# some helper functions for using minidom
|
# some helper functions for using minidom
|
||||||
|
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom as mdom
|
||||||
|
|
||||||
class XmlError(Exception):
|
class XmlError(Exception):
|
||||||
pass
|
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):
|
class XmlFile(object):
|
||||||
"""A class for retrieving information from an XML file"""
|
"""A class for retrieving information from an XML file"""
|
||||||
|
|
||||||
def readxml(self, filenm):
|
def readxml(self, filenm):
|
||||||
self.dom = xml.dom.minidom.parse(filenm)
|
self.dom = mdom.parse(filenm)
|
||||||
|
|
||||||
def writexml(self, filenm):
|
def writexml(self, filenm):
|
||||||
f = file(filenm,'w')
|
f = file(filenm,'w')
|
||||||
@@ -17,7 +39,21 @@ class XmlFile(object):
|
|||||||
|
|
||||||
def getChildren(self, tagpath):
|
def getChildren(self, tagpath):
|
||||||
""" returns the children of the given path"""
|
""" 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):
|
def getNode(self, tagpath):
|
||||||
"""returns the node for given *unique* path of the node.
|
"""returns the node for given *unique* path of the node.
|
||||||
@@ -33,17 +69,17 @@ class XmlFile(object):
|
|||||||
nodelist = self.dom.getElementsByTagName(tags[0])
|
nodelist = self.dom.getElementsByTagName(tags[0])
|
||||||
|
|
||||||
if len(nodelist)==0:
|
if len(nodelist)==0:
|
||||||
raise XmlError("Root tag for " % tagpath % " not found")
|
return None # not found
|
||||||
|
|
||||||
node = nodelist[0] # discard other matches
|
node = nodelist[0] # discard other matches
|
||||||
for nodename in tags[1:]:
|
for nodename in tags[1:]:
|
||||||
nodelist = node.getElementsByTagName(nodename)
|
nodelist = node.getElementsByTagName(nodename)
|
||||||
if len(nodelist)==0:
|
if len(nodelist)==0:
|
||||||
raise XmlError("Tag path " % tagpath % " broken")
|
return None
|
||||||
else:
|
else:
|
||||||
node = nodelist[0]
|
node = nodelist[0]
|
||||||
|
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def getAllNodes(self, nodepath):
|
def getAllNodes(self, nodepath):
|
||||||
"""returns all trees corresponding to given path.
|
"""returns all trees corresponding to given path.
|
||||||
@@ -52,31 +88,10 @@ class XmlFile(object):
|
|||||||
returns an array of nodes under PSPEC/Source"""
|
returns an array of nodes under PSPEC/Source"""
|
||||||
raise XmlError("Not implemented!")
|
raise XmlError("Not implemented!")
|
||||||
|
|
||||||
def getFirstNode(self, nodepath):
|
def getChildText(self, tagpath):
|
||||||
try:
|
node = self.getNode(tagpath)
|
||||||
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)
|
|
||||||
if not node:
|
if not node:
|
||||||
return None
|
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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user