view Config.py @ 14:d98bd27f77b2 default tip

Testcase for loops.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Wed, 14 Nov 2012 22:18:24 +0100
parents 09b39021c4af
children
line wrap: on
line source

#!/usr/bin/env python -O

import xml.dom.minidom, sys, re

class Config:
    def __init__(self, path):
        self.document = xml.dom.minidom.parse(path)

    @staticmethod
    def getOption(node, option, match):
        for potentialMatch in node.childNodes:
            if potentialMatch.nodeName == "option":
                if potentialMatch.hasAttribute('match'):
                    matchstr = potentialMatch.getAttribute('match')
                    try:
                        prog = re.compile(matchstr)
                    except:
                        raise xml.dom.SyntaxErr(matchstr
                                                +
                            " is not a valid value for attribute match")
                    if prog.match(match):
                        res = Config.getOption(potentialMatch, option, match)
                        if not res:
                            res = potentialMatch.getAttribute('cxxflags')
                        return res
        return None

    def getCxxflags(self, name):
        value = self.getOption(self.document.childNodes[0], 'Cxxflags', name)
        if value == None:
            raise xml.dom.NotFoundErr()
        return value

    def getFiles(self):
        retVal = []
        for elem in self.document.getElementsByTagName('file'):
            if elem.hasAttribute('name'):
                retVal.append(elem.getAttribute('name'))
            else:
                raise xml.dom.SyntaxErr("<file> must have a name attribute")

        return retVal