<commit_before>import codecs
import xml.etree.ElementTree as ET
import xml.dom.minidom as DOM

class SDDraftEditor():
    """Class for containing the core functions for editing a Service Definition Draft document."""
    def __init__(self, path):
        self._path = path
        self._loadxml()

    #region Static Methods

    @staticmethod
    def append_element(parent_elem, subelem):
        """Adds a child element to the end of a parent element."""
        parent_elem.append(subelem)

    @classmethod
    def enum_list_to_str(cls, values, enum, exception_message):
        string_values = []
        for val in values:
            string_values.append(cls.enum_to_str(val, enum, exception_message))
        return ",".join(string_values)

    @classmethod
    def create_element(cls, tag, value, attrib = {}):
        elem = ET.Element(tag, attrib)
        cls.set_element_value(elem, value)
        return elem

    @classmethod
    def create_config_element(cls, key, value):
        key_elem = cls.create_element("Key", key)
        value_elem = cls.create_element("Value", value, {"xsi:type": "xs:string"})
        return cls.create_element(
            "PropertySetProperty", [key_elem, value_elem], {"xsi:type": "typens:PropertySetProperty"})

    @staticmethod
    def enum_to_str(value, enum, exception_message):
        if isinstance(value, basestring):
            # Convert string to enum to check compatibility
            # Raises ValueError if unknown value.
            value = enum(value)
        elif not isinstance(value, enum):
            # not a known capability, raise exception
            raise TypeError(exception_message)
        return value.value

    @staticmethod
    def get_element_value(element, default = None):
        if element.text == None:
            return default
        if element.text.upper() == "TRUE":
            return True
        if element.text.upper() == "FALSE":
            return False

        try:
            return int(element.text)
        except ValueError:
            pass

        try:
            return float(element.text)
        except (ValueError):
            pass

        return element.text

    @staticmethod
    def set_element_value(element, value):
        if value == None:
            element.text = None
            return
        if isinstance(value, bool):
            element.text = "true" if value == True else "false"
            return
        if isinstance(value, (int, float)):
            element.text = repr(value)
            return
        if isinstance(value, basestring):
            element.text = value
            return
        if isinstance(value, list):
            # assume list of elements, remove all current and set<commit_msg>Add support for xsi:type & xsi:nil being set<commit_after>import codecs
import xml.etree.ElementTree as ET
import xml.dom.minidom as DOM

class SDDraftEditor():
    """Class for containing the core functions for editing a Service Definition Draft document."""
    def __init__(self, path):
        self._path = path
        self._loadxml()

    #region Static Methods

    @staticmethod
    def append_element(parent_elem, subelem):
        """Adds a child element to the end of a parent element."""
        parent_elem.append(subelem)

    @classmethod
    def enum_list_to_str(cls, values, enum, exception_message):
        if values == None:
            return None

        string_values = []
        for val in values:
            string_values.append(cls.enum_to_str(val, enum, exception_message))
        output = ",".join(string_values)
        if output == "":
            return None
        return output

    @classmethod
    def create_element(cls, tag, value, attrib = {}):
        elem = ET.Element(tag, attrib)
        cls.set_element_value(elem, value)
        return elem

    @classmethod
    def create_config_element(cls, key, value):
        key_elem = cls.create_element("Key", key)
        value_elem = cls.create_element("Value", value, {"xsi:type": "xs:string"})
        return cls.create_element(
            "PropertySetProperty", [key_elem, value_elem], {"xsi:type": "typens:PropertySetProperty"})

    @staticmethod
    def enum_to_str(value, enum, exception_message):
        if isinstance(value, basestring):
            # Convert string to enum to check compatibility
            # Raises ValueError if unknown value.
            value = enum(value)
        elif not isinstance(value, enum):
            # not a known capability, raise exception
            raise TypeError(exception_message)
        return value.value

    @staticmethod
    def get_element_value(element, default = None):
        if element.text == None:
            return default
        if element.text.upper() == "TRUE":
            return True
        if element.text.upper() == "FALSE":
            return False

        try:
            return int(element.text)
        except ValueError:
            pass

        try:
            return float(element.text)
        except (ValueError):
            pass

        return element.text

    @staticmethod
    def set_element_value(element, value, set_xsi_type = False, set_xsi_nil = False):
        if value == None:
            element.text = None

            if set_xsi_nil:
                if "{http://www.w3.org/2001/XMLSchema-instance}type" in element.attrib:
                    del element.attrib["{http://www.w3.org/2001/XMLSchema-instance}type"]
                element.set("{http://www.w3.org/2001/XMLSchema-instance}nil", "true")
            return
        elif set_xsi_nil:
            if "{http://www.w3.org/2001/XMLSchema-instance}nil" in element.attrib:
                    del element.attrib["{http://www.w3.org/2001/XMLSchema-instance}nil"]

        if isinstance(value, bool):
            element.text = "true" if value == True else "false"
            if set_xsi_type:
                # elementtree doesn't seem to support mapping schemas for values
                # Arc seems to consistently use the XS namespace, so it's not a problem right now
                element.set("{http://www.w3.org/2001/XMLSchema-instance}type", "xs:boolean")
            return
        if isinstance(value, int):
            element.text = repr(value)
            if set_xsi_type:
                element.set("{http://www.w3.org/2001/XMLSchema-instance}type", "xs:int")
            return
        if isinstance(value, float):
            element.text = repr(value)
            if set_xsi_type:
                element.set("{http://www.w3.org/2001/XMLSchema-instance}type", "xs:float")
            return
        if isinstance(value, basestring):
            element.text = value
            if set_xsi_type:
                element.set("{http://www.w3.org/2001/XMLSchema-instance}type", "xs:string")
            return
        if isinstance(value, list):
            # assume list of elements, remove all current and set
{'fraction_non_alphanumeric': 0.09767374656841497, 'fraction_numerical': 0.006501950585175552, 'mean_word_length': 2.1307100859339667, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 9, 'xml': 6}, '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': '2633885', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1863, 'n_words': 513}