<commit_before>import xml.etree.ElementTree as ET
import logging

# TODO with fiona._loading.add_gdal_dll_directories():
from fiona.ogrext import _get_metadata_item
from fiona.env import require_gdal_version
from fiona._env import get_gdal_version_num, calc_gdal_version_num

log = logging.getLogger(__name__)


class MetadataItem:
    # since GDAL 2.0
    CREATION_FIELD_DATA_TYPES = "DMD_CREATIONFIELDDATATYPES"
    # since GDAL 2.3
    CREATION_FIELD_DATA_SUB_TYPES = "DMD_CREATIONFIELDDATASUBTYPES"
    CREATION_OPTION_LIST = "DMD_CREATIONOPTIONLIST"
    LAYER_CREATION_OPTION_LIST = "DS_LAYER_CREATIONOPTIONLIST"
    # since GDAL 2.0
    DATASET_OPEN_OPTIONS = "DMD_OPENOPTIONLIST"
    # since GDAL 2.0
    EXTENSIONS = "DMD_EXTENSIONS"
    EXTENSION = "DMD_EXTENSION"
    VIRTUAL_IO = "DCAP_VIRTUALIO"
    # since GDAL 2.0
    NOT_NULL_FIELDS = "DCAP_NOTNULL_FIELDS"
    # since gdal 2.3
    NOT_NULL_GEOMETRY_FIELDS = "DCAP_NOTNULL_GEOMFIELDS"
    # since GDAL 3.2
    UNIQUE_FIELDS = "DCAP_UNIQUE_FIELDS"
    # since GDAL 2.0
    DEFAULT_FIELDS = "DCAP_DEFAULT_FIELDS"
    OPEN = "DCAP_OPEN"
    CREATE = "DCAP_CREATE"


def _parse_options(xml):
    """Convert metadata xml to dict"""
    options = {}
    if len(xml) > 0:

        try:
            root = ET.fromstring(xml)
            for option in root.iter('Option'):

                option_name = option.attrib['name']
                opt = {}
                opt.update((k, v) for k, v in option.attrib.items() if not k == 'name')

                values = []
                for value in option.iter('Value'):
                    values.append(value.text)
                if len(values) > 0:
                    opt['values'] = values

                options[option_name] = opt
        except Exception as e:
            log.warning("XML options parsing failed: {}: {}".format(str(e), xml))
            return None

    return options


@require_gdal_version('2.0')
def dataset_creation_options(driver):
    """ Returns dataset creation options for driver

    Parameters
    ----------
    driver : str

    Returns<commit_msg>fail when xml can not be parsed<commit_after>import xml.etree.ElementTree as ET
import logging

# TODO with fiona._loading.add_gdal_dll_directories():
from fiona.ogrext import _get_metadata_item, DriverError
from fiona.env import require_gdal_version
from fiona._env import get_gdal_version_num, calc_gdal_version_num

log = logging.getLogger(__name__)


class MetadataItem:
    # since GDAL 2.0
    CREATION_FIELD_DATA_TYPES = "DMD_CREATIONFIELDDATATYPES"
    # since GDAL 2.3
    CREATION_FIELD_DATA_SUB_TYPES = "DMD_CREATIONFIELDDATASUBTYPES"
    CREATION_OPTION_LIST = "DMD_CREATIONOPTIONLIST"
    LAYER_CREATION_OPTION_LIST = "DS_LAYER_CREATIONOPTIONLIST"
    # since GDAL 2.0
    DATASET_OPEN_OPTIONS = "DMD_OPENOPTIONLIST"
    # since GDAL 2.0
    EXTENSIONS = "DMD_EXTENSIONS"
    EXTENSION = "DMD_EXTENSION"
    VIRTUAL_IO = "DCAP_VIRTUALIO"
    # since GDAL 2.0
    NOT_NULL_FIELDS = "DCAP_NOTNULL_FIELDS"
    # since gdal 2.3
    NOT_NULL_GEOMETRY_FIELDS = "DCAP_NOTNULL_GEOMFIELDS"
    # since GDAL 3.2
    UNIQUE_FIELDS = "DCAP_UNIQUE_FIELDS"
    # since GDAL 2.0
    DEFAULT_FIELDS = "DCAP_DEFAULT_FIELDS"
    OPEN = "DCAP_OPEN"
    CREATE = "DCAP_CREATE"


def _parse_options(xml):
    """Convert metadata xml to dict"""
    options = {}
    if len(xml) > 0:

        root = ET.fromstring(xml)
        for option in root.iter('Option'):

            option_name = option.attrib['name']
            opt = {}
            opt.update((k, v) for k, v in option.attrib.items() if not k == 'name')

            values = []
            for value in option.iter('Value'):
                values.append(value.text)
            if len(values) > 0:
                opt['values'] = values

            options[option_name] = opt

    return options


@require_gdal_version('2.0')
def dataset_creation_options(driver):
    """ Returns dataset creation options for driver

    Parameters
    ----------
    driver : str

    Returns
{'fraction_non_alphanumeric': 0.09237645890240874, 'fraction_numerical': 0.009932952570151478, 'mean_word_length': 2.750465549348231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '5234903', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1383, 'n_words': 293}