<commit_before># -*- coding: utf-8 -*-
"""
declxml is a library for declaratively processing XML documents.

Processors
---------------
The declxml library uses processors to define the structure of XML documents. Processors can be
divided into two categories: *primitive processors* and *aggregate processors*.

Primitive Processors
----------------------
Primitive processors are used to parse and serialize simple, primitive values. The following module
functions are used to construct primitive processors:

.. autofunction:: declxml.boolean
.. autofunction:: declxml.floating_point
.. autofunction:: declxml.integer
.. autofunction:: declxml.string

Aggregate Processors
---------------------
Aggregate processors are composed of other processors. These processors are used for values such as
dictionaries, arrays, and user objects.

.. autofunction:: declxml.array
.. autofunction:: declxml.dictionary
.. autofunction:: declxml.named_tuple
.. autofunction:: declxml.user_object

Parsing
----------
.. autofunction:: declxml.parse_from_file
.. autofunction:: declxml.parse_from_string

Serialization
---------------
.. autofunction:: declxml.serialize_to_file
.. autofunction:: declxml.serialize_to_string

Exceptions
------------
.. autoexception:: declxml.XmlError
.. autoexception:: declxml.InvalidPrimitiveValue
.. autoexception:: declxml.InvalidRootProcessor
.. autoexception:: declxml.MissingValue
"""
from collections import namedtuple
import warnings
import sys
from io import open
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET


class XmlError(Exception):
    """Base error class representing errors processing XML data"""


class InvalidPrimitiveValue(XmlError):
    """Represents errors due to invalid primitive values"""


class InvalidRootProcessor(XmlError):
    """Represents errors due to invalid root processors"""


class MissingValue(XmlError):
    """Represents errors due to missing required values"""


def parse_from_file(root_processor, xml_file_path, encoding='utf-8'):
    """
    Parses the XML file using the processor starting from the root of the document.

    :param root_processor: Root processor of the XML document.
    :param xml_file_path: Path to XML file to parse.

    :return: Parsed value.
    """
    with open(xml_file_path, 'r', encoding=encoding) as xml_file:
        xml_string = xml_file.read()

    parsed_value = parse_from_string(root_processor, xml_string)

    return parsed_value


def parse_from_string(root_processor, xml_string):
    """
    Parses the XML string using the processor starting from the root of the document.

    :param xml_string: XML string to parse.

    See also :func:`declxml.parse_from_file`
    """
    if not _is_valid_root_processor(root_processor):
        raise InvalidRootProcessor('Invalid root processor')

    if sys.version_info[0] == 2 and isinstance(xml_string, unicode):
        xml_string = xml_string.encode('utf-8')

    root = ET.fromstring(xml_string)
    _xml_namespace_strip(root)

    state = _ProcessorState()
    return root_processor.parse_at_root(root, state)


def serialize_to_file(root_processor, value, xml_file_path, encoding='utf-8', indent=None):
    """
    Serializes the value to an XML file using the root processor.

    :param root_processor: Root processor of the XML document.
    :param value: Value to serialize.
    :param xml_file_path: Path to the XML file to which the serialized value will be written.
    :param indent: If specified, then the XML will be formatted with the specified indentation.
    """
    serialized_value = serialize_to_string(root_processor, value, indent)

    with open(xml_file_path, 'w', encoding=encoding) as xml_file:
        xml_file.write(serialized_value)


def serialize_to_string(root_processor, value, indent=None):<commit_msg>Add missing parameter docs and reorder imports<commit_after># -*- coding: utf-8 -*-
"""
declxml is a library for declaratively processing XML documents.

Processors
---------------
The declxml library uses processors to define the structure of XML documents. Processors can be
divided into two categories: *primitive processors* and *aggregate processors*.

Primitive Processors
----------------------
Primitive processors are used to parse and serialize simple, primitive values. The following module
functions are used to construct primitive processors:

.. autofunction:: declxml.boolean
.. autofunction:: declxml.floating_point
.. autofunction:: declxml.integer
.. autofunction:: declxml.string

Aggregate Processors
---------------------
Aggregate processors are composed of other processors. These processors are used for values such as
dictionaries, arrays, and user objects.

.. autofunction:: declxml.array
.. autofunction:: declxml.dictionary
.. autofunction:: declxml.named_tuple
.. autofunction:: declxml.user_object

Parsing
----------
.. autofunction:: declxml.parse_from_file
.. autofunction:: declxml.parse_from_string

Serialization
---------------
.. autofunction:: declxml.serialize_to_file
.. autofunction:: declxml.serialize_to_string

Exceptions
------------
.. autoexception:: declxml.XmlError
.. autoexception:: declxml.InvalidPrimitiveValue
.. autoexception:: declxml.InvalidRootProcessor
.. autoexception:: declxml.MissingValue
"""
from collections import namedtuple
from io import open
import sys
import warnings
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET


class XmlError(Exception):
    """Base error class representing errors processing XML data"""
    pass


class InvalidPrimitiveValue(XmlError):
    """Represents errors due to invalid primitive values"""
    pass


class InvalidRootProcessor(XmlError):
    """Represents errors due to invalid root processors"""
    pass


class MissingValue(XmlError):
    """Represents errors due to missing required values"""
    pass


def parse_from_file(root_processor, xml_file_path, encoding='utf-8'):
    """
    Parses the XML file using the processor starting from the root of the document.

    :param root_processor: Root processor of the XML document.
    :param xml_file_path: Path to XML file to parse.
    :param encoding: Encoding of the file.

    :return: Parsed value.
    """
    with open(xml_file_path, 'r', encoding=encoding) as xml_file:
        xml_string = xml_file.read()

    parsed_value = parse_from_string(root_processor, xml_string)

    return parsed_value


def parse_from_string(root_processor, xml_string):
    """
    Parses the XML string using the processor starting from the root of the document.

    :param xml_string: XML string to parse.

    See also :func:`declxml.parse_from_file`
    """
    if not _is_valid_root_processor(root_processor):
        raise InvalidRootProcessor('Invalid root processor')

    if sys.version_info[0] == 2 and isinstance(xml_string, unicode):
        xml_string = xml_string.encode('utf-8')

    root = ET.fromstring(xml_string)
    _xml_namespace_strip(root)

    state = _ProcessorState()
    return root_processor.parse_at_root(root, state)


def serialize_to_file(root_processor, value, xml_file_path, encoding='utf-8', indent=None):
    """
    Serializes the value to an XML file using the root processor.

    :param root_processor: Root processor of the XML document.
    :param value: Value to serialize.
    :param xml_file_path: Path to the XML file to which the serialized value will be written.
    :param encoding: Encoding of the file.
    :param indent: If specified, then the XML will be formatted with the specified indentation.
    """
    serialized_value = serialize_to_string(root_processor, value, indent)

    with open(xml_file_path, 'w', encoding=encoding) as xml_file:
        xml_file.write(serialized_value)


def serialize_to_string(root_processor, value, indent=None):
{'fraction_non_alphanumeric': 0.10296572088843241, 'fraction_numerical': 0.0015406342277570933, 'mean_word_length': 3.9460317460317462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 80}, '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': '2786336', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2087, 'n_words': 715}