<commit_before>""" Builtin types of dotconf.schema
"""

import re
import urlparse
import socket

try:
    import ipaddr
except ImportError:
    IPADDR_ENABLED = False
else:
    IPADDR_ENABLED = True

from dotconf.schema import Type, ValidationError


class Number(Type):

    """ A type representing a number (a float or an integer).
    """

    def validate(self, value):
        if not isinstance(value, int) and not isinstance(value, float) \
            and not isinstance(value, long):
            raise ValidationError('%r is not a number' % value)
        else:
            return value

    def cast(self, value):
        return float(value)


class Integer(Number):

    """ A type representing an integer in the configuration.

    :param min: define the minimum acceptable value value of the integer
    :param max: define the maximum acceptable value value of the integer

    Example in configuration::

        my_integer = 42
        my_integer = 42.0  # Will also match this type
    """

    def __init__(self, min=None, max=None):
        super(Integer, self).__init__()
        self._min = min
        self._max = max

    def validate(self, value):
        value = super(Integer, self).validate(value)
        if int(value) == value:
            value = int(value)
            if self._min is not None and value < self._min:
                raise ValidationError('%r is lower than the minimum (%d)'
                                      % (value, self._min))
            elif self._max is not None and value > self._max:
                raise ValidationError('%r is greater than the maximum (%d)'
                                      % (value, self._max))
            return value
        else:
            raise ValidationError('%r is not an integer value' % value)

    def cast(self, value):
        return int(value)


class Float(Number):

    """ A type representing a float in the configuration.

    Example in configuration::

        my_float = 42.2
        my_float = 42  # All values matched by the Integer type also match
                       # for the Float type
    """

    def __init__(self):
        super(Float, self).__init__()

    def validate(self, value):
        value = super(self, Float).validate(value)
        return float(value)


class Boolean(Type):

    """ A type representing a boolean value in the configuration.

    Example in configuration::

        my_boolean = yes
    """

    is_argparse_flag = True

    def __init__(self):
        super(Boolean, self).__init__()

    def validate(self, value):
        if value is not True and value is not False:
            raise ValidationError('%r is not a boolean value' % value)
        return value

    def cast(self, value):
        return bool(value)


class String(Type):

    """ A type representing a string in the configuration.

    Example in configuration::

        my_string = "hello, world!"

    """

    def __init__(self):
        super(String, self).__init__()

    def validate(self, value):
        return str(value)

    def cast(self, value):
        return value


class Regex(String):

    """ A string base type validated against a regex.
    """

    def __init__(self, regex, error='value doesn\'t match'):
        super(String, self).__init__()
        self._regex = re.compile(regex)
        self._error = error

    def validate(self, value):
        value = super(Regex, self).validate(value)
        if not self._regex.match(value):
            raise ValidationError(self._error)
        return value
<commit_msg>Reworked Regex type<commit_after>""" Builtin types of dotconf.schema
"""

import re
import urlparse
import socket

try:
    import ipaddr
except ImportError:
    IPADDR_ENABLED = False
else:
    IPADDR_ENABLED = True

from dotconf.schema import Type, ValidationError


class Number(Type):

    """ A type representing a number (a float or an integer).
    """

    def validate(self, value):
        if not isinstance(value, int) and not isinstance(value, float) \
            and not isinstance(value, long):
            raise ValidationError('%r is not a number' % value)
        else:
            return value

    def cast(self, value):
        return float(value)


class Integer(Number):

    """ A type representing an integer in the configuration.

    :param min: define the minimum acceptable value value of the integer
    :param max: define the maximum acceptable value value of the integer

    Example in configuration::

        my_integer = 42
        my_integer = 42.0  # Will also match this type
    """

    def __init__(self, min=None, max=None):
        super(Integer, self).__init__()
        self._min = min
        self._max = max

    def validate(self, value):
        value = super(Integer, self).validate(value)
        if int(value) == value:
            value = int(value)
            if self._min is not None and value < self._min:
                raise ValidationError('%r is lower than the minimum (%d)'
                                      % (value, self._min))
            elif self._max is not None and value > self._max:
                raise ValidationError('%r is greater than the maximum (%d)'
                                      % (value, self._max))
            return value
        else:
            raise ValidationError('%r is not an integer value' % value)

    def cast(self, value):
        return int(value)


class Float(Number):

    """ A type representing a float in the configuration.

    Example in configuration::

        my_float = 42.2
        my_float = 42  # All values matched by the Integer type also match
                       # for the Float type
    """

    def __init__(self):
        super(Float, self).__init__()

    def validate(self, value):
        value = super(self, Float).validate(value)
        return float(value)


class Boolean(Type):

    """ A type representing a boolean value in the configuration.

    Example in configuration::

        my_boolean = yes
    """

    is_argparse_flag = True

    def __init__(self):
        super(Boolean, self).__init__()

    def validate(self, value):
        if value is not True and value is not False:
            raise ValidationError('%r is not a boolean value' % value)
        return value

    def cast(self, value):
        return bool(value)


class String(Type):

    """ A type representing a string in the configuration.

    Example in configuration::

        my_string = "hello, world!"

    """

    def __init__(self):
        super(String, self).__init__()

    def validate(self, value):
        return str(value)

    def cast(self, value):
        return value


class _BaseRegex(String):

    """ Base class for regex types.
    """

    def __init__(self, regex, error='value doesn\'t match'):
        super(String, self).__init__()
        self._regex = re.compile(regex)
        self._error = error

    def validate(self, value):
        value = super(_BaseRegex, self).validate(value)
        match = self._regex.match(value)
        if match is None:
            raise ValidationError(self._error)
        return match


class Regex(_BaseRegex):

    """ A string based type validated against a regex.
    """

    def validate(self, value):
        value = super(Regex, self).validate(value)
        return value.string

{'fraction_non_alphanumeric': 0.0922846237731734, 'fraction_numerical': 0.0027262813522355507, 'mean_word_length': 2.1734429065743943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '735409', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1904, 'n_words': 704}