Mercurial > treepkg
changeset 407:fb473f67345b treepkg-status
implemented parsing from xml string
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Wed, 21 Jul 2010 15:52:15 +0000 |
parents | 52e3c3976e53 |
children | 02d498ee90b8 |
files | treepkg/info/status.py treepkg/info/status.xsd |
diffstat | 2 files changed, 122 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/treepkg/info/status.py Tue Jul 13 16:46:17 2010 +0000 +++ b/treepkg/info/status.py Wed Jul 21 15:52:15 2010 +0000 @@ -26,6 +26,29 @@ root = doc.documentElement return (doc, root) +def getTextFromNode(node): + if not node: + return None + textnode = node.firstChild + if not textnode or textnode.nodeType != node.TEXT_NODE: + return None + return textnode.data + +def getChild(node, name, required=False): + if not node: + if required: + raise TreepkgInfoException("") + return None + childs = node.getElementsByTagName(name) + if not childs: + if required: + raise TreepkgInfoException("") + return None + return childs[0] + +class TreepkgInfoException(Exception): + """ Exception class for TreepkgInfo """ + class TreepkgInfo: def __init__(self, config, numnewestrev=-1): @@ -44,8 +67,12 @@ return self.tpkgroot.toxml() @staticmethod - def fromxml(xml): #FIXME - pass + def fromxml(xml): + doc = xml.dom.minidom.parseString(xml) + root = doc.documentElement + if not root.tagName == "treepkg": + raise TreepkgInfoException("XML is not valid for treepkfinfo") + return TreepkgRootInfo.fromxml(root) # FIXME def add_revisions(self, track, trackinfo): revisions = track.get_revisions() @@ -100,7 +127,7 @@ self.tracks.append(track) def toxml(self): - (doc, root) = createTpkgRoot("status") + (doc, root) = createTpkgRoot("info") # add <name> nameele = createTpkgElement(doc, "name") text = doc.createTextNode(self.name) @@ -130,6 +157,24 @@ tracksele.appendChild(track.toxml()) root.appendChild(tracksele) return root + + @staticmethod + def fromxml(node): + version = node.getAttribute("version") + infoele = getChild(node, "info", True) + nameele = getChild(infoele, "name", True) + name = getTextFromNode(nameele) + treepkgpathele = getChild(infoele, "treepkgpath") + treepkgpath = getTextFromNode(treepkgpathele) + trackspathele = getChild(infoele, "trackspath") + trackspath = getTextFromNode(trackspathele) + trackseles = node.getElementsByTagName("tracks") + tracks = [] + for trackele in trackseles: + tracks.append(TreepkgTrackInfo.fromxml(trackele)) + trackinfo = TreepkgRootInfo(name, treepkgpath, trackspath, version) + trackinfo.tracks = tracks + return trackinfo class TreepkgTrackInfo: @@ -156,6 +201,21 @@ root.appendChild(osele) return root + @staticmethod + def fromxml(node): + nameele = getChild(node, "name", True) + name = getTextFromNode(nameele) + osele = getChild(node, "os") + os = getTextFromNode(osele) + treepkgtrackinfo = TreepkgTrackInfo(name, os) + treepkgtrackinfo.revisions = [] + revisioneles = node.getElementsByTagName("revision") + for revele in revisioneles: + treepkgrevision = TreepkgTrackRevisionInfo.fromxml(revele) + treepkgtrackinfo.revisions.append(treepkgrevision) + return treepkgtrackinfo + + class TreepkgTrackRevisionInfo: def __init__(self, number, rules, status): @@ -202,6 +262,28 @@ root.appendChild(logsele) return root + @staticmethod + def fromxml(node): + numberele = getChild(node, "number", True) + number = getTextFromNode(numberele) + rulesele = getChild(node, "rules", True) + rules = getTextFromNode(rulesele) + statusele = getChild(node, "status", True) + messageele = getChild(statusele, "message") + message = getTextFromNode(messageele) + treepkgrevisioninfo = TreepkgTrackRevisionInfo(number, rules, message) + treepkgrevisioninfo.packages = [] + treepkgrevisioninfo.logs = [] + packageeles = getChild(node, "packages") + for packageele in packageeles: + treepkgrevisioninfo.packages.append( + TreepkgPackageInfo.fromxml(packageele)) + logeles = getChild(node, "logs") + for logele in logeles: + treepkgrevisioninfo.logs.append( + TreepkgLogInfo.fromxml(logele)) + return treepkgrevisioninfo + class TreepkgLogInfo: def __init__(self, name, path): @@ -222,6 +304,14 @@ root.appendChild(pathele) return root + @staticmethod + def fromxml(node): + nameele = getChild(node, "name", True) + name = getTextFromNode(nameele) + pathele = getChild(node, "path", True) + path = getTextFromNode(pathele) + return TreepkgLogInfo(name, path) + class TreepkgPackageInfo: def __init__(self, name, path, type, arch): @@ -255,6 +345,20 @@ def add_checksum(self, checksum): self.checksums.append(checksum) + @staticmethod + def fromxml(node): + nameele = getChild(node, "name", True) + name = getTextFromNode(nameele) + pathele = getChild(node, "path", True) + path = getTextFromNode(pathele) + ptype = node.getAttribute("type") + arch = node.getAttribute("arch") + packageinfo = TreepkgPackageInfo(name, path, ptype, arch) + checksumeles = node.getElementsByTagName("checksum") + for checksumele in checksumeles: + packageinfo.checksums.append(TreepkgChecksumInfo.fromxml(checksumele)) + return packageinfo + class TreepkgChecksumInfo: def __init__(self, checksum, type="md5"): @@ -266,5 +370,13 @@ text = doc.createTextNode(self.checksum) root.appendChild(text) # add attribute type - root.setAttributeNS(TREEPKG_NAMESPACE_URI, "type", self.type) + if type: + root.setAttributeNS(TREEPKG_NAMESPACE_URI, "type", self.type) return root + + @staticmethod + def fromxml(node): + checksumele = getChild(node, "checksum", True) + checksum = getTextFromNode(checksumele) + ctype = node.getAttribute("type") + return TreepkgChecksumInfo(checksum, ctype)
--- a/treepkg/info/status.xsd Tue Jul 13 16:46:17 2010 +0000 +++ b/treepkg/info/status.xsd Wed Jul 21 15:52:15 2010 +0000 @@ -57,21 +57,22 @@ <xsd:complexType name="logs"> <xsd:sequence> - <xsd:element name="log" type="tpkg:log"/> + <xsd:element name="log" type="tpkg:log" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType nam="log"> <xsd:sequence> - <xsd:element name="name" type="xsd:string"/> - <xsd:element name="path" type="xsd:string"/> + <xsd:element name="name" type="xsd:string" minOccurs="1"/> + <xsd:element name="path" type="xsd:string" minOccurs="1"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="packages"> <xsd:sequence> - <xsd:element name="package" type="tpkg:package"/> + <xsd:element name="package" type="tpkg:package" + maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> @@ -88,7 +89,7 @@ <xsd:complexType name="checksum"> <xsd:sequence> - <xsd:element name="checksum" type="xsd:string"/> + <xsd:element name="checksum" type="xsd:string" minOccurs="1"/> </xsd:sequence> <xsd:attribute name="type" type="xsd:string"/> </xsd:complexType>