<commit_before>from invtool.tests.test_data import (
    TEST_DOMAIN, TEST_FQDN, TEST_IPv4, TEST_IPv6, TEST_DESCRIPTION,
    TEST_TTL, TEST_PORT, TEST_WEIGHT, TEST_PRIORITY, TEST_TEXT
)


def build_extractor(field_name, nas_name):
    def extractor(nas):
        if not getattr(nas, nas_name):
            return {}
        data = {field_name: getattr(nas, nas_name)}
        return data
    return extractor


###############
# DNS OPTIONS #
###############


def view_arguments(field_name):
    def add_view_arguments(parser, required=False):
        pri_group = parser.add_mutually_exclusive_group()

        pri_group.add_argument('--no-private', default=False,
                               action='store_true', dest='no_private',
                               help="Disable private view.", required=required)

        pri_group.add_argument('--private', default=False, action='store_true',
                               dest='private', help="Enabled private view.",
                               required=False)

        pub_group = parser.add_mutually_exclusive_group()

        pub_group.add_argument('--no-public', default=False,
                               action='store_true', dest='no_public',
                               help="Disable public view.", required=required)

        pub_group.add_argument('--public', default=False, action='store_true',
                               dest='public', help="Enabled public view.",
                               required=False)

    def extract_views(nas):
        views = []
        if nas.no_private:
            views.append('no-private')
        elif nas.private:
            views.append('private')

        if nas.no_public:
            views.append('no-public')
        elif nas.public:
            views.append('public')
        data = {field_name: views}
        return data

    def test_data():
        return '', '--no-public --private'

    return add_view_arguments, extract_views, test_data


def _add_domain_argument(parser, required=True):
    parser.add_argument(
        '--domain', default=None, type=str, dest='domain',
        help="The domain a record is in.", required=False)


def domain_argument(field_name):
    def extract_domain(nas):
        data = {}
        if nas.domain:
            data.update({field_name: nas.domain})
            return data

    def test_data():
        return 'domain', TEST_DOMAIN

    return _add_domain_argument, extract_domain, test_data


def _add_label_argument(parser, required=True):
    parser.add_argument(
        '--label', default="", type=str, dest='label',
        help="The first label in the fqdn. If label is ommited then '' is "
        "used and is analigouse to using '@' in a zone file (the record "
        "will get it's domain's name as it's fqdn).", required=False
    )


def fqdn_argument(field_name, rdtype):
    # We need rdtype because SRV requires a '_' to prefix it's test data
    def add_fqdn_argument(parser, required=True):
        parser.add_argument(
            '--fqdn', default="", type=str, dest='fqdn',
            help="The FQDN of the record being created. If you use this "
            "option you cannot use label or domain", required=False
        )

    def test_data():
        if rdtype == "CNAME":
            # Make the target different so avoid cname loop errors
            return 'fqdn', "A" + TEST_FQDN
        if rdtype == "SRV":
            return 'fqdn', "_" + TEST_FQDN
        else:
            return 'fqdn', TEST_FQDN

    return add_fqdn_argument, build_extractor(field_name, 'fqdn'), test_data


def ip_argument(field_name, ip_type):
    def add_ip_argument(parser, required=True):
        parser.add_argument(
            '--ip', default=None, type=str, dest='ip',
            help="A string representation of an IP address.",
            required=required
        )

    def test_data():
        if ip_type == '4':
            return 'ip', TEST_IPv4
        elif ip_type == '6':
            return 'ip', TEST_IPv6

    return add_ip_argument, build_extractor(field_name, 'ip'), test_data


def target_argument(field_name):
    def add_target_arg(parser, required=True):
        parser.add_argument(
            '--target', default=None, type=str, dest='target',
            help="The target name of a record", required=required
        )

    def test_data():
        return 'target', TEST_FQDN

    return add_target_arg, build_extractor(field_name, 'target'), test_data


def description_argument(field_name):
    def add_desc_arg(parser, **kwargs):
        parser.add_argument(
            '--description', default="", type=str, dest='description',
            help="Tell us a little about this record", required=False
        )

    def test_data():
        return 'description', TEST_DESCRIPTION

    return add_desc_arg, build_extractor(field_name, 'description'), test_data


def comment_argument(field_name):
    def add_com_arg(parser, **kwargs):
        parser.add_argument(
            '--comment', default="", type=str,
            dest='comment', help="Use this to record why a change is "
            "being made", required=False
        )

    def test_data():
        return 'comment', TEST_DESCRIPTION  # Reuse this test data

    return add_com_arg, build_extractor(field_name, 'comment'), test_data


def text_argument(field_name):
    def add_text_argument(parser, required=True):
        parser.add_argument(
            '--text', default=None, type=str, dest='text',
            help="The text data.", required=required
        )

    def test_data():
        return 'text', TEST_TEXT

    return add_text_argument, build_extractor(field_name, 'text'), test_data


def write_num_argument(parser, name, dest, help_text, required=False):
    parser.add_argument(
        '--{0}'.format(name), default=None, type=int,
        dest=dest, help=help_text, required=required
    )
    return parser


def ttl_argument(field_name):
    def add_ttl_argument(parser, **kwargs):
        write_num_argument(
            parser, 'ttl', 'ttl', "The ttl of a record.", required=False
        )

    def extract_ttl(nas):
        data = {}
        if nas.ttl:
            data['ttl'] = nas.ttl
        return data

    def test_data():
        return 'ttl', TEST_TTL
<commit_msg>added the ability to set the TTL of any record to None<commit_after>from invtool.tests.test_data import (
    TEST_DOMAIN, TEST_FQDN, TEST_IPv4, TEST_IPv6, TEST_DESCRIPTION,
    TEST_TTL, TEST_PORT, TEST_WEIGHT, TEST_PRIORITY, TEST_TEXT
)


def build_extractor(field_name, nas_name):
    def extractor(nas):
        if not getattr(nas, nas_name):
            return {}
        data = {field_name: getattr(nas, nas_name)}
        return data
    return extractor


###############
# DNS OPTIONS #
###############


def view_arguments(field_name):
    def add_view_arguments(parser, required=False):
        pri_group = parser.add_mutually_exclusive_group()

        pri_group.add_argument('--no-private', default=False,
                               action='store_true', dest='no_private',
                               help="Disable private view.", required=required)

        pri_group.add_argument('--private', default=False, action='store_true',
                               dest='private', help="Enabled private view.",
                               required=False)

        pub_group = parser.add_mutually_exclusive_group()

        pub_group.add_argument('--no-public', default=False,
                               action='store_true', dest='no_public',
                               help="Disable public view.", required=required)

        pub_group.add_argument('--public', default=False, action='store_true',
                               dest='public', help="Enabled public view.",
                               required=False)

    def extract_views(nas):
        views = []
        if nas.no_private:
            views.append('no-private')
        elif nas.private:
            views.append('private')

        if nas.no_public:
            views.append('no-public')
        elif nas.public:
            views.append('public')
        data = {field_name: views}
        return data

    def test_data():
        return '', '--no-public --private'

    return add_view_arguments, extract_views, test_data


def _add_domain_argument(parser, required=True):
    parser.add_argument(
        '--domain', default=None, type=str, dest='domain',
        help="The domain a record is in.", required=False)


def domain_argument(field_name):
    def extract_domain(nas):
        data = {}
        if nas.domain:
            data.update({field_name: nas.domain})
            return data

    def test_data():
        return 'domain', TEST_DOMAIN

    return _add_domain_argument, extract_domain, test_data


def _add_label_argument(parser, required=True):
    parser.add_argument(
        '--label', default="", type=str, dest='label',
        help="The first label in the fqdn. If label is ommited then '' is "
        "used and is analigouse to using '@' in a zone file (the record "
        "will get it's domain's name as it's fqdn).", required=False
    )


def fqdn_argument(field_name, rdtype):
    # We need rdtype because SRV requires a '_' to prefix it's test data
    def add_fqdn_argument(parser, required=True):
        parser.add_argument(
            '--fqdn', default="", type=str, dest='fqdn',
            help="The FQDN of the record being created. If you use this "
            "option you cannot use label or domain", required=False
        )

    def test_data():
        if rdtype == "CNAME":
            # Make the target different so avoid cname loop errors
            return 'fqdn', "A" + TEST_FQDN
        if rdtype == "SRV":
            return 'fqdn', "_" + TEST_FQDN
        else:
            return 'fqdn', TEST_FQDN

    return add_fqdn_argument, build_extractor(field_name, 'fqdn'), test_data


def ip_argument(field_name, ip_type):
    def add_ip_argument(parser, required=True):
        parser.add_argument(
            '--ip', default=None, type=str, dest='ip',
            help="A string representation of an IP address.",
            required=required
        )

    def test_data():
        if ip_type == '4':
            return 'ip', TEST_IPv4
        elif ip_type == '6':
            return 'ip', TEST_IPv6

    return add_ip_argument, build_extractor(field_name, 'ip'), test_data


def target_argument(field_name):
    def add_target_arg(parser, required=True):
        parser.add_argument(
            '--target', default=None, type=str, dest='target',
            help="The target name of a record", required=required
        )

    def test_data():
        return 'target', TEST_FQDN

    return add_target_arg, build_extractor(field_name, 'target'), test_data


def description_argument(field_name):
    def add_desc_arg(parser, **kwargs):
        parser.add_argument(
            '--description', default="", type=str, dest='description',
            help="Tell us a little about this record", required=False
        )

    def test_data():
        return 'description', TEST_DESCRIPTION

    return add_desc_arg, build_extractor(field_name, 'description'), test_data


def comment_argument(field_name):
    def add_com_arg(parser, **kwargs):
        parser.add_argument(
            '--comment', default="", type=str,
            dest='comment', help="Use this to record why a change is "
            "being made", required=False
        )

    def test_data():
        return 'comment', TEST_DESCRIPTION  # Reuse this test data

    return add_com_arg, build_extractor(field_name, 'comment'), test_data


def text_argument(field_name):
    def add_text_argument(parser, required=True):
        parser.add_argument(
            '--text', default=None, type=str, dest='text',
            help="The text data.", required=required
        )

    def test_data():
        return 'text', TEST_TEXT

    return add_text_argument, build_extractor(field_name, 'text'), test_data


def write_num_argument(parser, name, dest, help_text, required=False):
    parser.add_argument(
        '--{0}'.format(name), default=None, type=int,
        dest=dest, help=help_text, required=required
    )
    return parser


def ttl_argument(field_name):
    def add_ttl_argument(parser, **kwargs):
        parser.add_argument(
            '--{0}'.format('ttl'), default=None,
            dest='ttl', required=False,
            help="The ttl of a record. Setting the ttl to None will set it to "
            "the default zone TTL"
        )

    def extract_ttl(nas):
        data = {}
        if nas.ttl:
            data['ttl'] = nas.ttl
        return data

    def test_data():
        return 'ttl', TEST_TTL

{'fraction_non_alphanumeric': 0.11402750491159136, 'fraction_numerical': 0.0011787819253438114, 'mean_word_length': 2.4154589371980677, 'pattern_counts': {'":': 4, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1166837', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3666, 'n_words': 926}