Exemple #1
0
    def parse(xml, target=None, parseToInterchangeFormat =False):
        # Removes whitespace between tags to reduce potential parsing issues.
        pattern = re.compile('\<\?xml(.*)\?\>')
        if pattern.search(xml) is None:
            xml = '<?xml version="1.0" encoding="utf-8"?>' + xml
        dom = minidom.parseString(re.sub('>[\n\r\s\t]+<','><',xml))

        if target is None:
            target = {}

        cd = ConfigData()

        for node in dom.childNodes[0].childNodes:
            # Ignore whitespace generated text nodes
            if isinstance(node, (minidom.Comment, minidom.Text)):
                continue

            XMLConfigParser._parseNode(node, cd)

        if parseToInterchangeFormat:
            cd.writeToInterchangeDict(target)
        else:
            cd.writeToDict(target)

        return target
Exemple #2
0
    def parse(data, target=None, parseToInterchangeFormat =False):
        d = json.loads(data)

        if target is None:
            target = {}

        cd = ConfigData()

        for n,v in DictUtils.iter(d):
            JSONConfigParser._parseElement(n, v, cd)

        if parseToInterchangeFormat:
            cd.writeToInterchangeDict(target)
        else:
            cd.writeToDict(target)

        return target
Exemple #3
0
 def _parseElement(name, value, configData):
     if isinstance(value, list):
         configData.setItem(name, value[0], value[1])
     elif isinstance(value, str):
         configData.setItem(name, 's', value)
     elif isinstance(value, (int, float)):
         configData.setItem(name, 'n', value)
     elif isinstance(value, dict):
         cd = ConfigData()
         for n, v in DictUtils.iter(value):
             JSONConfigParser._parseElement(n, v, cd)
         configData.setItem(name, 'o', cd)
Exemple #4
0
    def parse(xml, target=None, parseToInterchangeFormat =False):
        # Removes whitespace between tags to reduce potential parsing issues.
        pattern = re.compile('\<\?xml(.*)\?\>')
        if pattern.search(xml) is None:
            xml = '<?xml version="1.0" encoding="utf-8"?>' + xml
        dom = minidom.parseString(re.sub('>[\n\r\s\t]+<','><',xml))

        if target is None:
            target = {}

        cd = ConfigData()

        for node in dom.childNodes[0].childNodes:
            # Ignore whitespace generated text nodes
            if isinstance(node, (minidom.Comment, minidom.Text)):
                continue

            XMLConfigParser._parseNode(node, cd)

        if parseToInterchangeFormat:
            cd.writeToInterchangeDict(target)
        else:
            cd.writeToDict(target)

        return target
Exemple #5
0
    def parse(data, target=None, parseToInterchangeFormat=False):
        d = json.loads(data)

        if target is None:
            target = {}

        cd = ConfigData()

        for n, v in DictUtils.iter(d):
            JSONConfigParser._parseElement(n, v, cd)

        if parseToInterchangeFormat:
            cd.writeToInterchangeDict(target)
        else:
            cd.writeToDict(target)

        return target
Exemple #6
0
    def _parseNode(node, configData):
        nodeName = node.getAttribute('n')
        nodeType = node.tagName

        if nodeType != 'o':
            XMLConfigParser._parseAttribute(nodeName, nodeType, node.getAttribute('v'), configData)
            return

        cd = ConfigData()
        for k in node.attributes.keys():
            if k != 'n':
                aValue = node.getAttribute(k)
                aType  = 's'
                if aValue.find(':') != -1:
                    aValue = node.getAttribute(k).split(':')
                    aType  = str(aValue[0])
                    aValue = aValue[-1]
                XMLConfigParser._parseAttribute(k, aType, aValue, cd)

        for child in node.childNodes:
            XMLConfigParser._parseNode(child, cd)

        configData.setItem(nodeName, 'o', cd)