<commit_before>#!/usr/bin/env python
'''
Octo-DNS Reporter / exit non-zero on failure
'''

from __future__ import absolute_import, division, print_function, \
    unicode_literals

from concurrent.futures import ThreadPoolExecutor
from logging import getLogger
from sys import stdout
import sys
import re
from dns.exception import Timeout
from dns.resolver import NXDOMAIN, NoAnswer, NoNameservers, Resolver, query

from octodns.cmds.args import ArgumentParser
from octodns.manager import Manager
from octodns.zone import Zone

log = getLogger('check-zone')

class AsyncResolver(Resolver):
    """
    Create an DNS resolver using an async pool of workers
    """

    def __init__(self, num_workers, *args, **kwargs):
        super(AsyncResolver, self).__init__(*args, **kwargs)
        self.executor = ThreadPoolExecutor(max_workers=num_workers)

    def query(self, *args, **kwargs):
        return self.executor.submit(super(AsyncResolver, self).query, *args,
                                    **kwargs)

def configure_resolvers(dns_servers):
    """
    For each dns_server, configure an an async resolver
    Return a list of a AsyncResolvers
    """
    resolvers = []
    ip_addr_re = re.compile(r'^[\d\.]+$')
    for server in dns_servers:
        resolver = AsyncResolver(configure=False,
                                 num_workers=4)
        # We need to resolve this if it's not an IP
        if not ip_addr_re.match(server):
            server = unicode(query(server, 'A')[0])
        log.info('server=%s', server)
        resolver.nameservers = [server]
        resolver.lifetime = 8
        resolvers.append(resolver)
    return resolvers

def quote_cleanup(values):
    """
    Some cleanup for unicode u"'dns.domain.'" / u'"dns.domain."'
    """
    return [unicode(r).replace("'", "").replace('"', '') for r in values]


def record_value_list(record):
    """
    Get a list of configured record values
    (even for ones that only have one value)
    """
    if hasattr(record,'values'):
        return record.values
    else:
        return [record.value]
<commit_msg>Add validip function<commit_after>#!/usr/bin/env python
'''
Octo-DNS Reporter / exit non-zero on failure
'''

from __future__ import absolute_import, division, print_function, \
    unicode_literals

from concurrent.futures import ThreadPoolExecutor
from logging import getLogger
from sys import stdout
import sys
import re
from dns.exception import Timeout
from dns.resolver import NXDOMAIN, NoAnswer, NoNameservers, Resolver, query

from octodns.cmds.args import ArgumentParser
from octodns.manager import Manager
from octodns.zone import Zone

log = getLogger('check-zone')

class AsyncResolver(Resolver):
    """
    Create an DNS resolver using an async pool of workers
    """

    def __init__(self, num_workers, *args, **kwargs):
        super(AsyncResolver, self).__init__(*args, **kwargs)
        self.executor = ThreadPoolExecutor(max_workers=num_workers)

    def query(self, *args, **kwargs):
        return self.executor.submit(super(AsyncResolver, self).query, *args,
                                    **kwargs)

def configure_resolvers(dns_servers):
    """
    For each dns_server, configure an an async resolver
    Return a list of a AsyncResolvers
    """
    resolvers = []
    for server in dns_servers:
        # We need to resolve this if it's not an IP
        if not validip(server):
            server = unicode(query(server, 'A')[0])
        resolver = AsyncResolver(configure=False,
                                 num_workers=4)
        log.info('server=%s', server)
        resolver.nameservers = [server]
        resolver.lifetime = 8
        resolvers.append(resolver)
    return resolvers

def quote_cleanup(values):
    """
    Some cleanup for unicode u"'dns.domain.'" / u'"dns.domain."'
    """
    return [unicode(r).replace("'", "").replace('"', '') for r in values]

def validip(ip):
    return ip.count('.') == 3 and  all(0<=int(num)<256 for num in ip.rstrip().split('.'))

def record_value_list(record):
    """
    Get a list of configured record values
    (even for ones that only have one value)
    """
    if hasattr(record,'values'):
        return record.values
    else:
        return [record.value]

{'fraction_non_alphanumeric': 0.10878267012008476, 'fraction_numerical': 0.0025900635742877325, 'mean_word_length': 3.0037700282752122, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3691264', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1250, 'n_words': 367}