All pastes #157966 Raw Edit

Untitled

public python v1 · immutable
·published 2006-09-01 21:22 UTC
rendered paste body
from urllib import urlopenfrom xml.dom import minidom, Nodeclass BonsaiResult:    nodes = []class CiNode:    log = ""    who = ""    date = ""    files = []    class FileNode:    revision = ""    filename = ""class BonsaiParser:    def __init__(self, bonsaiURL):        self.dom = minidom.parse(urlopen(bonsaiURL))        self.nodes = []        self.currentCiNode = None        self.currentFileNode = None    def getData(self):        data = BonsaiResult()        while self._nextCiNode():            ci = CiNode()            ci.log = self._getLog()            ci.who = self._getWho()            ci.date = self._getDate()            while self._nextFileNode():                fn = FileNode()                fn.revision = self._getRevision()                fn.filename = self._getFilename()                ci.files.append(fn)            data.nodes.append(ci)        return data    def _nextCiNode(self):        try:            # first <ci> node?            if not self.currentCiNode:                # every other sibling is a <ci>, so jump 2 ahead                self.currentCiNode = self.dom.getElementsByTagName("ci")[0]            else:                self.currentCiNode = self.currentCiNode.nextSibling.nextSibling        except AttributeError:            self.currentCiNode = None                    if self.currentCiNode:            return True        else:            return False    def _getLog(self):        log = ""        for child in self.currentCiNode.childNodes:            if child.nodeType == Node.ELEMENT_NODE and child.tagName == "log":                log = child.firstChild.data        return log    def _getWho(self):        """Returns the e-mail address of the commit'er"""        return str(self.currentCiNode.getAttribute("who").replace("%", "@"))    def _getDate(self):        """Returns the date (unix time) of the commit"""        return int(self.currentCiNode.getAttribute("date"))                    def _firstFileNode(self):        for child in self.currentCiNode.childNodes:            if child.nodeType == Node.ELEMENT_NODE and child.tagName == "files":                # child is now the <files> element                for c in child.childNodes:                    if c.nodeType == Node.ELEMENT_NODE and c.tagName == "f":                        return c    def _nextFileNode(self):        # every other sibling is a <f>, so go two past the current one        try:            # first <f> node?            if not self.currentFileNode:                self.currentFileNode = self._firstFileNode()            else:                self.currentFileNode = self.currentFileNode.nextSibling.nextSibling        except AttributeError:            self.currentFileNode = None        if self.currentFileNode:            return True        else:            return False    def _getFilename(self):        return str(self.currentFileNode.firstChild.data)    def _getRevision(self):        return float(self.currentFileNode.getAttribute("rev"))bp = BonsaiParser("http://bonsai.mozilla.org/cvsquery.cgi?treeid=default&module=all&branch=HEAD&sortby=Date&date=hours&hours=2&cvsroot=%2Fcvsroot&xml=1")data = bp.getData()for cinode in data.nodes:    print "Who: ", cinode.who    print "Log: \n", cinode.log    print "Date:", cinode.date    for file in cinode.files:        print "Revision: ", file.revision        print "Filename: ", file.filename    print "\n\n\n\n"