<commit_before># -*- coding: utf-8 -*-

import re
from lxml import etree


LOCALES = ['DE', 'FR', 'EN', 'IT']
XPATH_NODE = 'node'
XPATH_TEXT = 'text'

gmd_namespaces = {
    'atom': 'http://www.w3.org/2005/Atom',
    'che': 'http://www.geocat.ch/2008/che',
    'csw': 'http://www.opengis.net/cat/csw/2.0.2',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dct': 'http://purl.org/dc/terms/',
    'ddi': 'http://www.icpsr.umich.edu/DDI',
    'dif': 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/',
    'fgdc': 'http://www.opengis.net/cat/csw/csdgm',
    'gco': 'http://www.isotc211.org/2005/gco',
    'gmd': 'http://www.isotc211.org/2005/gmd',
    'gmx': 'http://www.isotc211.org/2005/gmx',
    'gml': 'http://www.opengis.net/gml',
    'ogc': 'http://www.opengis.net/ogc',
    'ows': 'http://www.opengis.net/ows',
    'rim': 'urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0',
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'srv': 'http://www.isotc211.org/2005/srv',
    'xs': 'http://www.w3.org/2001/XMLSchema',
    'xs2': 'http://www.w3.org/XML/Schema',
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
    'xlink': 'http://www.w3.org/1999/xlink',
}

GMD_URL_LABEL = './/gmd:description'
GMD_SERVICE_NODES = '//gmd:identificationInfo//srv:containsOperations/srv:SV_OperationMetadata[.//srv:operationName//gco:CharacterString/text()]'  # noqa
GMD_SERVICE_URLS = [
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#DE" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#FR" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#EN" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#IT" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[./text()]/text()',  # noqa
]
GMD_MEDIA_TYPE = '//gmd:identificationInfo//srv:serviceType/gco:LocalName/text()'  # noqa
GMD_SERVICE_TITLE = './/srv:operationName/gco:CharacterString/text()'  # noqa
URL_PATH_LIST = [
    './/gmd:linkage//che:LocalisedURL[@locale="#DE"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#FR"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#EN"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#IT"]/text()',
    './/che:LocalisedURL/text()',
    './/gmd:URL/text()',
]
GMD_RESOURCE_NAME = './/gmd:name/gco:CharacterString/text()'
GMD_RESOURCE_DESCRIPTION = './/gmd:description'

DOWNLOAD_PROTOCOL = "WWW:DOWNLOAD"
OGC_WMTS_PROTOCOL = "OGC:WMTS"
OGC_WFS_PROTOCOL = "OGC:WFS"
OGC_WMS_PROTOCOL = "OGC:WMS"
LINKED_DATA_PROTOCOL = "LINKED:DATA"
ESRI_REST_PROTOCOL = "ESRI:REST"
MAP_PROTOCOL = 'MAP:Preview'
SERVICE_PROTOCOLS = [OGC_WMTS_PROTOCOL, OGC_WFS_PROTOCOL,
                     OGC_WMS_PROTOCOL, LINKED_DATA_PROTOCOL,
                     ESRI_REST_PROTOCOL, MAP_PROTOCOL]
SERVICE_FORMAT = 'SERVICE'


def get_elem_tree_from_string(xml_string):
    try:
        xml_elem_tree = etree.fromstring(xml_string)
    except etree.XMLSyntaxError as e:
        raise MetadataFormatError('Could not parse XML: %r' % e)
    return xml_elem_tree


def xpath_get_single_sub_node_for_node_and_path(node, path):
    results = node.xpath(path, namespaces=gmd_namespaces)
    if results:
        return results[0]
    else:
        return None


def xpath_get_all_sub_nodes_for_node_and_path(node, path):
    results = node.xpath(path, namespaces=gmd_namespaces)
    if results:
        return results
    else:
        return None


def xpath_get_all_values_for_node_and_path_list(node, path_list):
    values = []
    for path in path_list:
        value = node.xpath(path, namespaces=gmd_namespaces)
        if value:
            values.extend(value)
    return values


def xpath_get_first_of_values_from_path_list(node, path_list, get=XPATH_NODE):
    get_text = ''
    if get == XPATH_TEXT:
        get_text = '/text()'
    for path in path_list:
        value = node.xpath(path + get_text, namespaces=gmd_namespaces)
        if value:
            return value[0], path
    return None, None


def xpath_get_language_dict_from_geocat_multilanguage_node(node):
    language_dict = {'en': '', 'it': '', 'de': '', 'fr': ''}
    localised_string_found = False
    for locale in LOCALES:
        value_locale = \
            node.xpath('.//gmd:textGroup/gmd:LocalisedCharacterString[@locale="#{}"]/text()'  # noqa
                       .format(locale),
                       namespaces=gmd_namespaces)
        if value_locale:
            localised_string_found = True
            language_dict[locale.lower()] = _clean_string(value_locale[0])
    if localised_string_found:
        return language_dict
    value = node.xpath('.//gco:CharacterString/text()',
                       namespaces=gmd_namespaces)
    if value:
        for locale in LOCALES:
            language_dict[locale.lower()] = _clean_string(value[0])
        return language_dict
    return language_dict


def xpath_get_url_and_languages(node):
    languages = []
    url, _ = xpath_get_first_of_values_from_path_list(
        node=node, path_list=URL_PATH_LIST)
    for locale in LOCALES:
        value_locale = \
            node.xpath('.//che:LocalisedURL[@locale="#{}"]'
                       .format(locale) + '/text()',
                       namespaces=gmd_namespaces)
        if value_locale:
            languages.append(locale.lower())
    return url, languages


def xpath_get_rights_dict_form_rights_node(node):
    rights_dict = {'en': '', 'it': '', 'de': '', 'fr': ''}
    try:
        for locale in LOCALES:
            value_locale = \<commit_msg>style: call function only once instead of multiple times<commit_after># -*- coding: utf-8 -*-

import re
from lxml import etree


LOCALES = ['DE', 'FR', 'EN', 'IT']
XPATH_NODE = 'node'
XPATH_TEXT = 'text'

gmd_namespaces = {
    'atom': 'http://www.w3.org/2005/Atom',
    'che': 'http://www.geocat.ch/2008/che',
    'csw': 'http://www.opengis.net/cat/csw/2.0.2',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dct': 'http://purl.org/dc/terms/',
    'ddi': 'http://www.icpsr.umich.edu/DDI',
    'dif': 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/',
    'fgdc': 'http://www.opengis.net/cat/csw/csdgm',
    'gco': 'http://www.isotc211.org/2005/gco',
    'gmd': 'http://www.isotc211.org/2005/gmd',
    'gmx': 'http://www.isotc211.org/2005/gmx',
    'gml': 'http://www.opengis.net/gml',
    'ogc': 'http://www.opengis.net/ogc',
    'ows': 'http://www.opengis.net/ows',
    'rim': 'urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0',
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'srv': 'http://www.isotc211.org/2005/srv',
    'xs': 'http://www.w3.org/2001/XMLSchema',
    'xs2': 'http://www.w3.org/XML/Schema',
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
    'xlink': 'http://www.w3.org/1999/xlink',
}

GMD_URL_LABEL = './/gmd:description'
GMD_SERVICE_NODES = '//gmd:identificationInfo//srv:containsOperations/srv:SV_OperationMetadata[.//srv:operationName//gco:CharacterString/text()]'  # noqa
GMD_SERVICE_URLS = [
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#DE" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#FR" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#EN" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[@locale = "#IT" and ./text()]/text()',  # noqa
    './/srv:connectPoint//gmd:linkage//che:LocalisedURL[./text()]/text()',  # noqa
]
GMD_MEDIA_TYPE = '//gmd:identificationInfo//srv:serviceType/gco:LocalName/text()'  # noqa
GMD_SERVICE_TITLE = './/srv:operationName/gco:CharacterString/text()'  # noqa
URL_PATH_LIST = [
    './/gmd:linkage//che:LocalisedURL[@locale="#DE"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#FR"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#EN"]/text()',
    './/gmd:linkage//che:LocalisedURL[@locale="#IT"]/text()',
    './/che:LocalisedURL/text()',
    './/gmd:URL/text()',
]
GMD_RESOURCE_NAME = './/gmd:name/gco:CharacterString/text()'
GMD_RESOURCE_DESCRIPTION = './/gmd:description'

DOWNLOAD_PROTOCOL = "WWW:DOWNLOAD"
OGC_WMTS_PROTOCOL = "OGC:WMTS"
OGC_WFS_PROTOCOL = "OGC:WFS"
OGC_WMS_PROTOCOL = "OGC:WMS"
LINKED_DATA_PROTOCOL = "LINKED:DATA"
ESRI_REST_PROTOCOL = "ESRI:REST"
MAP_PROTOCOL = 'MAP:Preview'
SERVICE_PROTOCOLS = [OGC_WMTS_PROTOCOL, OGC_WFS_PROTOCOL,
                     OGC_WMS_PROTOCOL, LINKED_DATA_PROTOCOL,
                     ESRI_REST_PROTOCOL, MAP_PROTOCOL]
SERVICE_FORMAT = 'SERVICE'


def get_elem_tree_from_string(xml_string):
    try:
        xml_elem_tree = etree.fromstring(xml_string)
    except etree.XMLSyntaxError as e:
        raise MetadataFormatError('Could not parse XML: %r' % e)
    return xml_elem_tree


def xpath_get_single_sub_node_for_node_and_path(node, path):
    results = node.xpath(path, namespaces=gmd_namespaces)
    if results:
        return results[0]
    else:
        return None


def xpath_get_all_sub_nodes_for_node_and_path(node, path):
    results = node.xpath(path, namespaces=gmd_namespaces)
    if results:
        return results
    else:
        return None


def xpath_get_all_values_for_node_and_path_list(node, path_list):
    values = []
    for path in path_list:
        value = node.xpath(path, namespaces=gmd_namespaces)
        if value:
            values.extend(value)
    return values


def xpath_get_first_of_values_from_path_list(node, path_list, get=XPATH_NODE):
    get_text = ''
    if get == XPATH_TEXT:
        get_text = '/text()'
    for path in path_list:
        value = node.xpath(path + get_text, namespaces=gmd_namespaces)
        if value:
            return value[0], path
    return None, None


def xpath_get_language_dict_from_geocat_multilanguage_node(node):
    language_dict = {'en': '', 'it': '', 'de': '', 'fr': ''}
    localised_string_found = False
    for locale in LOCALES:
        value_locale = \
            node.xpath('.//gmd:textGroup/gmd:LocalisedCharacterString[@locale="#{}"]/text()'  # noqa
                       .format(locale),
                       namespaces=gmd_namespaces)
        if value_locale:
            localised_string_found = True
            cleaned_value = _clean_string(value_locale[0])
            language_dict[locale.lower()] = cleaned_value
    if localised_string_found:
        return language_dict
    value = node.xpath('.//gco:CharacterString/text()',
                       namespaces=gmd_namespaces)
    if value:
        cleaned_value = _clean_string(value[0])
        for locale in LOCALES:
            language_dict[locale.lower()] = cleaned_value
        return language_dict
    return language_dict


def xpath_get_url_and_languages(node):
    languages = []
    url, _ = xpath_get_first_of_values_from_path_list(
        node=node, path_list=URL_PATH_LIST)
    for locale in LOCALES:
        value_locale = \
            node.xpath('.//che:LocalisedURL[@locale="#{}"]'
                       .format(locale) + '/text()',
                       namespaces=gmd_namespaces)
        if value_locale:
            languages.append(locale.lower())
    return url, languages


def xpath_get_rights_dict_form_rights_node(node):
    rights_dict = {'en': '', 'it': '', 'de': '', 'fr': ''}
    try:
        for locale in LOCALES:
            value_locale = \
{'fraction_non_alphanumeric': 0.16893613282965828, 'fraction_numerical': 0.013177545462531845, 'mean_word_length': 3.135125317835089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 34, 'xml': 14}, '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': '2996840', 'n_tokens_mistral': 4368, 'n_tokens_neox': 4272, 'n_words': 598}