<commit_before>from __future__ import print_function
import math

from .util import resolve_ns

from xml.etree import ElementTree as etree
from mapproxy.compat import string_type
from mapproxy.request.wms import switch_bbox_epsg_axis_order


class WMSCapabilities(object):
    _default_namespace = None
    _namespaces = {
        'xlink': 'http://www.w3.org/1999/xlink',
    }

    version = None


    def __init__(self, tree):
        self.tree = tree
        self._layer_tree = None

    def resolve_ns(self, xpath):
        return resolve_ns(xpath, self._namespaces, self._default_namespace)

    def findtext(self, tree, xpath):
        return tree.findtext(self.resolve_ns(xpath))

    def find(self, tree, xpath):
        return tree.find(self.resolve_ns(xpath))

    def findall(self, tree, xpath):
        return tree.findall(self.resolve_ns(xpath))

    def attrib(self, elem, name):
        return elem.attrib[self.resolve_ns(name)]

    def metadata(self):
        md = dict(
            name = self.findtext(self.tree, 'Service/Name'),
            title = self.findtext(self.tree, 'Service/Title'),
            abstract = self.findtext(self.tree, 'Service/Abstract'),
            fees = self.findtext(self.tree, 'Service/Fees'),
            access_constraints = self.findtext(self.tree, 'Service/AccessConstraints'),
        )
        elem = self.find(self.tree, 'Service/OnlineResource')
        if elem is not None:
            md['online_resource'] = self.attrib(elem, 'xlink:href')

        md['contact'] = self.parse_contact()
        return md

    def parse_contact(self):
        elem = self.find(self.tree, 'Service/ContactInformation')
        if not elem:
            elem = etree.Element(None)
        md = dict(
            person = self.findtext(elem, 'ContactPersonPrimary/ContactPerson'),
            organization = self.findtext(elem, 'ContactPersonPrimary/ContactOrganization'),
            position = self.findtext(elem, 'ContactPosition'),

            address = self.findtext(elem, 'ContactAddress/Address'),
            city = self.findtext(elem, 'ContactAddress/City'),
            postcode = self.findtext(elem, 'ContactAddress/PostCode'),
            country = self.findtext(elem, 'ContactAddress/Country'),
            phone = self.findtext(elem, 'ContactVoiceTelephone'),
            fax = self.findtext(elem, 'ContactFacsimileTelephone'),
            email = self.findtext(elem, 'ContactElectronicMailAddress'),
        )

        return md


    def layers(self):
        if not self._layer_tree:
            root_layer = self.find(self.tree, 'Capability/Layer')
            self._layer_tree = self.parse_layer(root_layer, None)

        return self._layer_tree

    def layers_list(self):
        layers = []
        def append_layer(layer):
            if layer.get('name'):
                layers.append(layer)<commit_msg>Addressing FutureWarning for elem check (#217)<commit_after>from __future__ import print_function
import math

from .util import resolve_ns

from xml.etree import ElementTree as etree
from mapproxy.compat import string_type
from mapproxy.request.wms import switch_bbox_epsg_axis_order


class WMSCapabilities(object):
    _default_namespace = None
    _namespaces = {
        'xlink': 'http://www.w3.org/1999/xlink',
    }

    version = None


    def __init__(self, tree):
        self.tree = tree
        self._layer_tree = None

    def resolve_ns(self, xpath):
        return resolve_ns(xpath, self._namespaces, self._default_namespace)

    def findtext(self, tree, xpath):
        return tree.findtext(self.resolve_ns(xpath))

    def find(self, tree, xpath):
        return tree.find(self.resolve_ns(xpath))

    def findall(self, tree, xpath):
        return tree.findall(self.resolve_ns(xpath))

    def attrib(self, elem, name):
        return elem.attrib[self.resolve_ns(name)]

    def metadata(self):
        md = dict(
            name = self.findtext(self.tree, 'Service/Name'),
            title = self.findtext(self.tree, 'Service/Title'),
            abstract = self.findtext(self.tree, 'Service/Abstract'),
            fees = self.findtext(self.tree, 'Service/Fees'),
            access_constraints = self.findtext(self.tree, 'Service/AccessConstraints'),
        )
        elem = self.find(self.tree, 'Service/OnlineResource')
        if elem is not None:
            md['online_resource'] = self.attrib(elem, 'xlink:href')

        md['contact'] = self.parse_contact()
        return md

    def parse_contact(self):
        elem = self.find(self.tree, 'Service/ContactInformation')
        if elem is None or len(elem) is 0:
            elem = etree.Element(None)
        md = dict(
            person = self.findtext(elem, 'ContactPersonPrimary/ContactPerson'),
            organization = self.findtext(elem, 'ContactPersonPrimary/ContactOrganization'),
            position = self.findtext(elem, 'ContactPosition'),

            address = self.findtext(elem, 'ContactAddress/Address'),
            city = self.findtext(elem, 'ContactAddress/City'),
            postcode = self.findtext(elem, 'ContactAddress/PostCode'),
            country = self.findtext(elem, 'ContactAddress/Country'),
            phone = self.findtext(elem, 'ContactVoiceTelephone'),
            fax = self.findtext(elem, 'ContactFacsimileTelephone'),
            email = self.findtext(elem, 'ContactElectronicMailAddress'),
        )

        return md


    def layers(self):
        if not self._layer_tree:
            root_layer = self.find(self.tree, 'Capability/Layer')
            self._layer_tree = self.parse_layer(root_layer, None)

        return self._layer_tree

    def layers_list(self):
        layers = []
        def append_layer(layer):
            if layer.get('name'):
                layers.append(layer)
{'fraction_non_alphanumeric': 0.11207045415299603, 'fraction_numerical': 0.0024175444655499916, 'mean_word_length': 2.6268002504696306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1767971', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1653, 'n_words': 349}