<commit_before># -*- coding: utf-8 -*-
"""
gatewaydevice.py
~~~~~~~~~~~~~~~~

This is an implementation of the Internet Gateway Device v1.0 specification.
It explicitly knows how to parse the XML device description for IGDs.
"""
import requests
import xml.etree.ElementTree
from .device import Device


class GatewayDeviceV1(Device):
    """
    An Internet Gateway Device V1.
    """
    def describe(self):
        """
        Retrieve the device description and use it to populate the device
        object.
        """
        r = requests.get(self.location)
        r.raise_for_status()
        xml = ElementTree.parse(r.text).getroot()

        # Begin by setting the base URL.
        return xml

    def _set_base_url(self, root):
        """
        Given an ElementTree root of the description XML, set the base URL.

        :param root: The ElementTree root of the description XML.
        """
        base = root.find('URLBase')

        if base:
            self.base_url = base.text
        else:
            self.base_url = ('http://' + self.source_ip + ':' +
                             str(self.source_port))
        return<commit_msg>Informational fields.<commit_after># -*- coding: utf-8 -*-
"""
gatewaydevice.py
~~~~~~~~~~~~~~~~

This is an implementation of the Internet Gateway Device v1.0 specification.
It explicitly knows how to parse the XML device description for IGDs.
"""
import requests
import xml.etree.ElementTree
from .device import Device
from .utils import camelcase_to_underscore


class GatewayDeviceV1(Device):
    """
    An Internet Gateway Device V1.
    """
    def describe(self):
        """
        Retrieve the device description and use it to populate the device
        object.
        """
        r = requests.get(self.location)
        r.raise_for_status()
        root = ElementTree.parse(r.text).getroot()

        # Start by populating the base URL.
        self._set_base_url(root)

        self._describe_device(root)

        return

    def _set_base_url(self, root):
        """
        Given an ElementTree root of the description XML, set the base URL.

        :param root: The ElementTree root of the description XML.
        """
        base = root.find('URLBase')

        if base:
            self.base_url = base.text
        else:
            self.base_url = ('http://' + self.source_ip + ':' +
                             str(self.source_port))
        return

    def _describe_device(self, root):
        """
        Given the root of the description XML, grab the device portion and use
        it to populate this object.

        :param root: The ElementTree root of the description XML.
        """
        dev = root.find('device')

        if not dev:
            raise ValueError('Malformed XML received: absent device tag.')

        # Begin by populating some of the informational fields. These are all
        # plain strings.
        informational_fields = ['deviceType', 'friendlyName', 'manufacturer',
                                'manufacturerURL', 'modelDescription',
                                'modelName', 'modelNumber', 'modelURL',
                                'serialNumber', 'UDN', 'UPC',
                                'presentationURL']

        for field in informational_fields:
            try:
                attr_name = camelcase_to_underscore(field)
                setattr(self, attr_name, dev.find(field).text)
            except AttributeError:
                # dev.find() returned None
                pass

        return
{'fraction_non_alphanumeric': 0.09714769839028523, 'fraction_numerical': 0.002824060999717594, 'mean_word_length': 1.9296939619520264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7033160', 'n_tokens_mistral': 947, 'n_tokens_neox': 898, 'n_words': 300}