<commit_before>import re
import sys
from werkzeug.routing import Rule

class Validation_Response : 
    def __init__(self):
        self.valid = True
        self.details = {}
        self.arguments = {}


class Argument : 
    required = True,
    repeated = False,
    validator = None,
    defaultValue = None,
    order = 50,
    allowableValues = None

    def __init__(self, desc, validator, required = False, repeated = False,
                 defaultValue = None, order = 50, allowableValues = None) :
        self.description = desc
        self.required = required
        self.repeated = repeated
        self.validator = validator
        self.defaultValue = defaultValue
        self.order = order
        self.allowableValues = allowableValues
        if(self.validator == None) : 
            print "A validator is required"

class ValidationTypeException(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)
        


def boolean(value):
    if(value.lower() == "true") :
        return True
    elif(value.lower()=="false"):
         return False
    else:
        raise ValidationTypeException("boolean")

def time(value): 
    m = re.match(r"T(?P<hour>(\d){1,2})(?P<minutes>(\d){2})", value)
    if(m) :
        if(int(m.groupdict()['hour']) < 0 or int(m.groupdict()['hour']) > 24 or
           int(m.groupdict()['minutes']) < 0 or int(m.groupdict()['minutes']) > 60):
            raise ValidationTypeException("time1")
        else:
            return value
    else:
            raise ValidationTypeException("time2")

    return value

def datetime_validator(value): 
    m = re.match(r"^(\d){4}(?P<month>(\d){2})(?P<day>(\d){2})(?P<hour>T(\d){3,4})", value)
    if(m) :
        if(int(m.groupdict()['month']) < 1 or int(m.groupdict()['month']) > 12 or
           int(m.groupdict()['day']) < 1 or int(m.groupdict()['day']) > 31):
            raise Exception("validation_error")
        else:
            time(m.groupdict()['hour'])
            return value
    else:
        raise ValidationTypeException("datetime")

    return value

    
def entrypoint(valid_types = None):
    if valid_types == None:
        types = ("validity_pattern", "line", "route", "vehicle_journey",
        "stop_point", "stop_area", "stop_time", "network", "mode", "commercial_mode",
        "city", "connection", "route_point", "district", "department", "company", " vehicle", "country", "way", "coord", "address")
    else:
        types  = valid_types
    return lambda value : entrypoint_validator(value, types)


def entrypoint_validator(value, valid_types=None):
    m = re.match(r"^(?P<type>(\w)+):", value)
    if not m or m.groupdict()["type"] not in valid_types:
        raise ValidationTypeException("entrypoint")
    return value

def filter(value):
    return value


def validate_arguments(request, validation_dict) :
    response = Validation_Response() 
    for key, value in request.args.iteritems() :
        if not (key in validation_dict) : 
            response.details[key] = {"status" : "ignored", "value":value}
        else:
            if not validation_dict[key].repeated and len(request.args.getlist(key)) > 1:
                response.details[key] = {"status" : "multiple", "value":value}

            for val in request.args.getlist(key) :
                try :
                    parsed_val = validation_dict[key].validator(val)
                    response.details[key] = {"status" : "valid", "value": val}
                    if not(validation_dict[key].repeated) :
                        if not(validation_dict[key].allowableValues) or parsed_val in validation_dict[key].allowableValues :
                            response.arguments[key] = parsed_val
                        else:
                            response.valid=False
                            response.details[key] = {"status" : "not in allowable values", "value":parsed_val}
                    else:
                        if not(validation_dict[key].allowableValues) or parsed_val in validation_dict[key].allowableValues :
                            if not(key in response.arguments):
                                response.arguments[key] = []
                            response.arguments[key].append(parsed_val)
                        else:
                            response.valid=False
                            response.details[key] = {"status" : "not in allowable values", "value":parsed_val}
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    if validation_dict[key].required:
                        response.valid = False
                    response.details[key] = {"status" : "notvalid", "value" : val }
    for key, value in validation_dict.iteritems():
        if not(key in request.args) :
            if value.required:
                response.valid = False
                response.details[key] = {"status", "missing"}
            else:
                response.arguments[key] = value.defaultValue

    return response

class RuleArguments(Rule):
    arguments = {},
    def __init__(self, string, defaults=None, subdomain=None, methods=None,
                 build_only=False, endpoint=None, strict_slashes=None,
                 redirect_to=None, alias=False, host=None, arguments=None):
        Rule.__init__(self, string, defaults, subdomain, methods, build_only,
                      endpoint, strict_slashes, redirect_to, alias, host)
        self.arguments = arguments

def validate_apis(apis) :
    response = Validation_Response() 
    for name, api in apis.iteritems():
        response.details[name] = [] 
        
        if not "arguments" in api:
            response.valid = False<commit_msg>Jörmungandr : Correction de la validation<commit_after>import re
import sys
from werkzeug.routing import Rule

class Validation_Response : 
    def __init__(self):
        self.valid = True
        self.details = {}
        self.arguments = {}


class Argument : 
    required = True,
    repeated = False,
    validator = None,
    defaultValue = None,
    order = 50,
    allowableValues = None

    def __init__(self, desc, validator, required = False, repeated = False,
                 defaultValue = None, order = 50, allowableValues = None) :
        self.description = desc
        self.required = required
        self.repeated = repeated
        self.validator = validator
        self.defaultValue = defaultValue
        self.order = order
        self.allowableValues = allowableValues
        if(self.validator == None) : 
            print "A validator is required"

class ValidationTypeException(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)
        


def boolean(value):
    if(value.lower() == "true") :
        return True
    elif(value.lower()=="false"):
         return False
    else:
        raise ValidationTypeException("boolean")

def time(value): 
    m = re.match(r"T(?P<hour>(\d){1,2})(?P<minutes>(\d){2})", value)
    if(m) :
        if(int(m.groupdict()['hour']) < 0 or int(m.groupdict()['hour']) > 24 or
           int(m.groupdict()['minutes']) < 0 or int(m.groupdict()['minutes']) > 60):
            raise ValidationTypeException("time1")
        else:
            return value
    else:
            raise ValidationTypeException("time2")

    return value

def datetime_validator(value): 
    m = re.match(r"^(\d){4}(?P<month>(\d){2})(?P<day>(\d){2})(?P<hour>T(\d){3,4})", value)
    if(m) :
        if(int(m.groupdict()['month']) < 1 or int(m.groupdict()['month']) > 12 or
           int(m.groupdict()['day']) < 1 or int(m.groupdict()['day']) > 31):
            raise Exception("validation_error")
        else:
            time(m.groupdict()['hour'])
            return value
    else:
        raise ValidationTypeException("datetime")

    return value

    
def entrypoint(valid_types = None):
    if valid_types == None:
        types = ("validity_pattern", "line", "route", "vehicle_journey",
        "stop_point", "stop_area", "stop_time", "network", "mode", "commercial_mode",
        "city", "connection", "route_point", "district", "department", "company", " vehicle", "country", "way", "coord", "address")
    else:
        types  = valid_types
    return lambda value : entrypoint_validator(value, types)


def entrypoint_validator(value, valid_types=None):
    m = re.match(r"^(?P<type>(\w)+):", value)
    if not m or m.groupdict()["type"] not in valid_types:
        raise ValidationTypeException("entrypoint")
    return value

def filter(value):
    return unicode(value)


def validate_arguments(request, validation_dict) :
    response = Validation_Response() 
    for key, value in request.args.iteritems() :
        if not (key in validation_dict) : 
            response.details[key] = {"status" : "ignored", "value":value}
        else:
            if not validation_dict[key].repeated and len(request.args.getlist(key)) > 1:
                response.details[key] = {"status" : "multiple", "value":value}

            for val in request.args.getlist(key) :
                try :
                    parsed_val = validation_dict[key].validator(val)
                    response.details[key] = {"status" : "valid", "value": val}
                    if not(validation_dict[key].repeated) :
                        if not(validation_dict[key].allowableValues) or parsed_val in validation_dict[key].allowableValues :
                            response.arguments[key] = parsed_val
                        else:
                            response.valid=False
                            response.details[key] = {"status" : "not in allowable values", "value":parsed_val}
                    else:
                        if not(validation_dict[key].allowableValues) or parsed_val in validation_dict[key].allowableValues :
                            if not(key in response.arguments):
                                response.arguments[key] = []
                            response.arguments[key].append(parsed_val)
                        else:
                            response.valid=False
                            response.details[key] = {"status" : "not in allowable values", "value":parsed_val}
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    response.valid = False
                    response.details[key] = {"status" : "notvalid", "value" : val }
    for key, value in validation_dict.iteritems():
        if not(key in request.args) :
            if value.required:
                response.valid = False
                response.details[key] = {"status", "missing"}
            else:
                response.arguments[key] = value.defaultValue

    return response

class RuleArguments(Rule):
    arguments = {},
    def __init__(self, string, defaults=None, subdomain=None, methods=None,
                 build_only=False, endpoint=None, strict_slashes=None,
                 redirect_to=None, alias=False, host=None, arguments=None):
        Rule.__init__(self, string, defaults, subdomain, methods, build_only,
                      endpoint, strict_slashes, redirect_to, alias, host)
        self.arguments = arguments

def validate_apis(apis) :
    response = Validation_Response() 
    for name, api in apis.iteritems():
        response.details[name] = [] 
        
        if not "arguments" in api:
            response.valid = False
{'fraction_non_alphanumeric': 0.12494542914520212, 'fraction_numerical': 0.004889548589889112, 'mean_word_length': 2.179017485428809, 'pattern_counts': {'":': 10, '<': 23, '<?xml version=': 0, '>': 25, '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': '2199993', 'n_tokens_mistral': 3248, 'n_tokens_neox': 3120, 'n_words': 723}