<commit_before>"""
:copyright: (c) 2013 by <NAME>.
:license: MIT. See LICENSE for details.
"""
import xml.etree.ElementTree as et
import inputpy.param as param
from inputpy.designspace import DesignSpace
from inputpy.mapping import Mapping, CodeMapping

class XMLFactory:
    @staticmethod
    def getParameter(element, parentId=None, codeMapping=None):
        # Does not yet handle nested parameters.
        args = element.attrib

        if element.tag.endswith('SParam'):
            args['type'] = 'SParam'
            paramId = args['id']
            if parentId is None:
                nextParent = paramId
            else:
                nextParent = parentId + '.' + paramId
            args['nested'] = [
                XMLFactory.getParameter(e, nextParent) for e in element
            ]

        args['parentId'] = parentId
        return param.getParameter(**args)

    @staticmethod
    def getMapping(element):
        return Mapping(**element.attrib)

    @staticmethod
    def getCodeMapping(fileName, mappingFactory=None):
        factory = mappingFactory or XMLFactory.getMapping
        root = et.parse(fileName).getroot()
        mappings = []
        mappingTypes = []
        for e in root:
            if e.tag.endswith('Mapping'):
                mappings.append(factory(e))
            elif e.tag.endswith('MappingType'):
                mappingTypes.append(factory(e))
        return CodeMapping(mappings, mappingTypes)

    @staticmethod
    def getParamStore(root, codeMapping=None, paramFactory=None):
        # Does not yet handle nested parameters.
        factory = paramFactory or XMLFactory.getParameter
        return param.ParamStore([factory(e) for e in root])

    @staticmethod
    def getDesignSpace(fileName, codeMappingFactory=None, psFactory=None):
        factory = psFactory or XMLFactory.getParamStore
        cmFactory = codeMappingFactory or XMLFactory.getCodeMapping
        root = et.parse(fileName).getroot()
        codeMapping = root.attrib.get('mapping')
        if codeMapping is not None:
            codeMapping = cmFactory(codeMapping)
        ps = factory(root, codeMapping=codeMapping)
        spaceId = root.get('id')
        return DesignSpace(ps, spaceId, fileName)<commit_msg>Cleanup and bug fix in XML factories<commit_after>"""
:copyright: (c) 2013 by <NAME>.
:license: MIT. See LICENSE for details.
"""
import xml.etree.ElementTree as et
import inputpy.param as param
import inputpy.util as util
from inputpy.designspace import DesignSpace
from inputpy.mapping import Mapping, CodeMapping, NULL_CODE_MAPPING
from inputpy.q import *

class XMLFactory:
    @staticmethod
    def __getParamArgs(element, parentId=None):
        args = element.attrib
        if element.tag.endswith(SPARAM):
            args['type'] = SPARAM
            paramId = args[ID_ATTR]
            nextParent = util.absolute(parentId, paramId)
            args['nested'] = [
                XMLFactory.__getParamArgs(e, nextParent) for e in element
            ]

        args['parentId'] = parentId
        return args

    @staticmethod
    def getParameter(element, codeMapping=None):
        cm = codeMapping or NULL_CODE_MAPPING
        return param.paramFactory(XMLFactory.__getParamArgs(element), cm)

    @staticmethod
    def getMapping(element):
        return Mapping(**element.attrib)

    @staticmethod
    def getCodeMapping(fileName, mappingFactory=None):
        factory = mappingFactory or XMLFactory.getMapping
        root = et.parse(fileName).getroot()
        mappings = []
        mappingTypes = []
        for e in root:
            if e.tag.endswith(MAPPING):
                mappings.append(factory(e))
            elif e.tag.endswith(MAPPING_TYPE):
                mappingTypes.append(factory(e))
        return CodeMapping(mappings, mappingTypes)

    @staticmethod
    def getParamStore(root, codeMapping=None, paramFactory=None):
        factory = paramFactory or XMLFactory.getParameter
        cm = codeMapping
        return param.ParamStore([factory(e, codeMapping=cm) for e in root])

    @staticmethod
    def getDesignSpace(fileName, codeMappingFactory=None, psFactory=None):
        factory = psFactory or XMLFactory.getParamStore
        cmFactory = codeMappingFactory or XMLFactory.getCodeMapping
        root = et.parse(fileName).getroot()
        codeMapping = root.attrib.get(MAPPING_ATTR)
        if codeMapping is not None:
            codeMapping = cmFactory(codeMapping)
        ps = factory(root, codeMapping=codeMapping)
        spaceId = root.get(ID_ATTR)
        return DesignSpace(ps, spaceId, fileName)
{'fraction_non_alphanumeric': 0.08956521739130435, 'fraction_numerical': 0.0017391304347826088, 'mean_word_length': 2.6719872306464487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4782776', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1229, 'n_words': 317}