<commit_before># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Argument Types
==============

Provides functions which are used to parser and/or convert
arguments from the argparse object.
"""


from argparse import ArgumentParser
from functools import partial, wraps
from os.path import isdir

from netaddr import AddrFormatError, IPAddress

from pyfarm.core.enums import OS, NUMERIC_TYPES
from pyfarm.core.logger import getLogger
from pyfarm.core.utility import convert

INFINITE = set(["inf", "infinite", "unlimited"])
logger = getLogger("agent")


def assert_instance(func):
    """
    ensures that the instance argument passed along to the validation
    function contains data we expect
    """
    @wraps(func)
    def run(*args, **kwargs):
        instance = kwargs.get("instance")
        assert instance is not None
        assert hasattr(instance, "args") and hasattr(instance, "parser")
        assert isinstance(instance.parser, ArgumentParser)
        return func(*args, **kwargs)
    return run


@assert_instance
def ip(value, instance=None):
    """make sure the ip address provided is valid"""
    try:
        IPAddress(value)
    except (ValueError, AddrFormatError):
        instance.parser.error("%s is not a valid ip address" % value)
    else:
        return value


@assert_instance
def port(value, instance=None):
    """convert and check to make sure the provided port is valid"""
    try:
        value = convert.ston(value)
    except (ValueError, SyntaxError):
        instance.parser.error("failed to convert --port to a number")
    else:
        low_port = 1 if instance.args.uid == 0 else 49152
        high_port = 65535

        if low_port > value or value > high_port:
            instance.parser.error(
                "valid port range is %s-%s" % (low_port, high_port))

        return value

# Function is not currently tested because uid/gid mapping is system specific,
# may require access to external network resources, and internally is<commit_msg>adding system_identifier()<commit_after># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Argument Types
==============

Provides functions which are used to parser and/or convert
arguments from the argparse object.
"""


from argparse import ArgumentParser
from functools import partial, wraps
from os.path import isdir

from netaddr import AddrFormatError, IPAddress

from pyfarm.core.enums import OS, NUMERIC_TYPES
from pyfarm.core.logger import getLogger
from pyfarm.core.utility import convert

INFINITE = set(["inf", "infinite", "unlimited"])
logger = getLogger("agent")


def assert_instance(func):
    """
    ensures that the instance argument passed along to the validation
    function contains data we expect
    """
    @wraps(func)
    def run(*args, **kwargs):
        instance = kwargs.get("instance")
        assert instance is not None
        assert hasattr(instance, "args") and hasattr(instance, "parser")
        assert isinstance(instance.parser, ArgumentParser)
        return func(*args, **kwargs)
    return run


@assert_instance
def ip(value, instance=None):
    """make sure the ip address provided is valid"""
    try:
        IPAddress(value)
    except (ValueError, AddrFormatError):
        instance.parser.error("%s is not a valid ip address" % value)
    else:
        return value


@assert_instance
def port(value, instance=None):
    """convert and check to make sure the provided port is valid"""
    try:
        value = convert.ston(value)
    except (ValueError, SyntaxError):
        instance.parser.error("failed to convert --port to a number")
    else:
        low_port = 1 if instance.args.uid == 0 else 49152
        high_port = 65535

        if low_port > value or value > high_port:
            instance.parser.error(
                "valid port range is %s-%s" % (low_port, high_port))

        return value

@assert_instance
def system_identifier(value, instance=None):
    """validates a --systemid value"""
    if value is None:
        raise NotImplementedError("TODO")
    try:
        value = convert.ston(value)
    except (ValueError, SyntaxError):
        instance.parser.error("failed to convert --systemid to a number")
    else:
        pass

# Function is not currently tested because uid/gid mapping is system specific,
# may require access to external network resources, and internally is
{'fraction_non_alphanumeric': 0.08869943769272629, 'fraction_numerical': 0.007255577725376383, 'mean_word_length': 3.432475884244373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5814415', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1465, 'n_words': 609}