<commit_before>"""Module provider for EasyDNS"""
from __future__ import absolute_import
import json
import logging

import requests
from lexicon.providers.base import Provider as BaseProvider


LOGGER = logging.getLogger(__name__)

NAMESERVER_DOMAINS = ['easydns.net']


def provider_parser(subparser):
    """Configure provider parser for EasyDNS"""
    subparser.add_argument(
        "--auth-username", help="specify username for authentication")
    subparser.add_argument(
        "--auth-token", help="specify token for authentication")


class Provider(BaseProvider):
    """Provider class for EasyDNS"""
    def __init__(self, config):
        super(Provider, self).__init__(config)
        self.domain_id = None
        self.api_endpoint = self._get_provider_option(
            'api_endpoint') or 'https://rest.easydns.net'

    def _authenticate(self):

        payload = self._get('/domain/{0}'.format(self.domain))

        if payload['data']['exists'] == 'N':
            raise Exception('No domain found')

        self.domain_id = payload['data']['id']

    # Create record. If record already exists with the same content, do nothing'

    def _create_record(self, rtype, name, content):
        record = {
            'type': rtype,
            'domain': self.domain_id,
            'host': self._relative_name(name),
            'ttl': self._get_lexicon_option('ttl'),
            'prio': 0,
            'rdata': content
        }
        try:
            self._put(
                '/zones/records/add/{0}/{1}'.format(self.domain_id, rtype), record)
        except requests.exceptions.HTTPError as error:
            # http 400 is ok here, because the record probably already exists
            if error.response.status_code != 400:
                raise
        LOGGER.debug('create_record: %s', True)
        return True

    # List all records. Return an empty list if no records found
    # type, name and content are used to filter records.
    # If possible filter during the query, otherwise filter after response is received.
    def _list_records(self, rtype=None, name=None, content=None):
        payload = self._get('/zones/records/all/{0}'.format(self.domain_id))
        records = []
        for record in payload['data']:
            processed_record = {
                'type': record['type'],
                'name': "{0}.{1}".format(record['host'], record['domain']),
                'ttl': record['ttl'],
                'content': record['rdata'],
                'id': record['id']
            }
            records.append(processed_record)

        if rtype:
            records = [record for record in records if record['type'] == rtype]
        if name:
            records = [record for record in records if record['name']
                       == self._full_name(name)]
        if content:
            records = [
                record for record in records if record['content'] == content]<commit_msg>Revert broken code hiding exceptions, and add a FIXME. Issue will follow.<commit_after>"""Module provider for EasyDNS"""
from __future__ import absolute_import
import json
import logging

import requests
from lexicon.providers.base import Provider as BaseProvider


LOGGER = logging.getLogger(__name__)

NAMESERVER_DOMAINS = ['easydns.net']


def provider_parser(subparser):
    """Configure provider parser for EasyDNS"""
    subparser.add_argument(
        "--auth-username", help="specify username for authentication")
    subparser.add_argument(
        "--auth-token", help="specify token for authentication")


class Provider(BaseProvider):
    """Provider class for EasyDNS"""
    def __init__(self, config):
        super(Provider, self).__init__(config)
        self.domain_id = None
        self.api_endpoint = self._get_provider_option(
            'api_endpoint') or 'https://rest.easydns.net'

    def _authenticate(self):

        payload = self._get('/domain/{0}'.format(self.domain))

        if payload['data']['exists'] == 'N':
            raise Exception('No domain found')

        self.domain_id = payload['data']['id']

    # Create record. If record already exists with the same content, do nothing'

    def _create_record(self, rtype, name, content):
        record = {
            'type': rtype,
            'domain': self.domain_id,
            'host': self._relative_name(name),
            'ttl': self._get_lexicon_option('ttl'),
            'prio': 0,
            'rdata': content
        }
        try:
            self._put(
                '/zones/records/add/{0}/{1}'.format(self.domain_id, rtype), record)
        except requests.exceptions.HTTPError as error:
            # FIXME: adferrand 06/01/2019: Broken provider needs fixes.
            # In fact, this except block will silently hide every HTTP error, as an except
            # block without raise statement will implicitly hide the exception.
            # In reality, tests of Easy DNS are failing...
            # So this provider needs to be corrected.
            if error.response.status_code == 400:
                pass
            # http 400 is ok here, because the record probably already exists
        LOGGER.debug('create_record: %s', True)
        return True

    # List all records. Return an empty list if no records found
    # type, name and content are used to filter records.
    # If possible filter during the query, otherwise filter after response is received.
    def _list_records(self, rtype=None, name=None, content=None):
        payload = self._get('/zones/records/all/{0}'.format(self.domain_id))
        records = []
        for record in payload['data']:
            processed_record = {
                'type': record['type'],
                'name': "{0}.{1}".format(record['host'], record['domain']),
                'ttl': record['ttl'],
                'content': record['rdata'],
                'id': record['id']
            }
            records.append(processed_record)

        if rtype:
            records = [record for record in records if record['type'] == rtype]
        if name:
            records = [record for record in records if record['name']
                       == self._full_name(name)]
        if content:
            records = [
                record for record in records if record['content'] == content]
{'fraction_non_alphanumeric': 0.11452558213210835, 'fraction_numerical': 0.005385712022810074, 'mean_word_length': 2.3126967471143756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5602135', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1662, 'n_words': 501}