<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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.
"""

import re
import urllib
from distutils.version import StrictVersion
from .. exc import ValidationError

name_re = re.compile(r'^[A-Za-z_][\w]*$')

# hostname_re and email_re shamelessly stolen from django.core.validators
hostname_re = re.compile(
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|"
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'
r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)'
r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
re.IGNORECASE)


def validate_name(name):
    if name_re.match(name) is None:
        raise ValidationError("Invalid name {}".format(name))
    return name


def validate_password(password):
    # nothing as of now
    return password


def validate_hostname(source):
    if len(source) > 255:
        raise ValidationError('hostname too long')

    if source.endswith('.') or \
      not hostname_re.match(source):
        raise ValidationError("{} is not a valid hostname".format(source))

    return source


def validate_redirect_http_code(http_code):
    try:
        http_code = int(http_code)
        assert http_code in (301, 302, 303, 307)

    except (ValueError, AssertionError):
        raise ValidationError("Invalid redirection http_code {}"\
                              .format(http_code))

    else:
        return http_code


def validate_redirect_target_path(target_path):
    try:
        target_path = urllib.quote(target_path)
        assert not target_path or target_path.startswith('/')

    except AssertionError:
        raise ValidationError("Target path for redirects must be absolute")

    except TypeError:
        target_path = ''

    return unicode(target_path)


def validate_version(version):
    validator = StrictVersion()
    try:
        validator.parse(version)

    except:
        raise ValidationError('invalid version %s')

    return version


def validate_positive_int(number):
    try:
        number = int(number)
        assert number > 0

    except:
        raise ValidationError('{} is not a positive integer'.format(number))

    return number
<commit_msg>Fixed error in validation error log message<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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.
"""

import re
import urllib
from distutils.version import StrictVersion
from .. exc import ValidationError

name_re = re.compile(r'^[A-Za-z_][\w]*$')

# hostname_re and email_re shamelessly stolen from django.core.validators
hostname_re = re.compile(
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|"
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'
r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)'
r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
re.IGNORECASE)


def validate_name(name):
    if name_re.match(name) is None:
        raise ValidationError("Invalid name {}".format(name))
    return name


def validate_password(password):
    # nothing as of now
    return password


def validate_hostname(source):
    if len(source) > 255:
        raise ValidationError('hostname too long')

    if source.endswith('.') or \
      not hostname_re.match(source):
        raise ValidationError("{} is not a valid hostname".format(source))

    return source


def validate_redirect_http_code(http_code):
    try:
        http_code = int(http_code)
        assert http_code in (301, 302, 303, 307)

    except (ValueError, AssertionError):
        raise ValidationError("Invalid redirection http_code {}"\
                              .format(http_code))

    else:
        return http_code


def validate_redirect_target_path(target_path):
    try:
        target_path = urllib.quote(target_path)
        assert not target_path or target_path.startswith('/')

    except AssertionError:
        raise ValidationError("Target path for redirects must be absolute")

    except TypeError:
        target_path = ''

    return unicode(target_path)


def validate_version(version):
    validator = StrictVersion()
    try:
        validator.parse(version)

    except:
        raise ValidationError('invalid version {}'.format(version))

    return version


def validate_positive_int(number):
    try:
        number = int(number)
        assert number > 0

    except:
        raise ValidationError('{} is not a positive integer'.format(number))

    return number

{'fraction_non_alphanumeric': 0.17470881863560733, 'fraction_numerical': 0.03993344425956739, 'mean_word_length': 3.336940836940837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '907452', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2087, 'n_words': 494}