iro/validate.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 26 Jan 2012 01:15:33 +0100
branchdevel
changeset 109 935b5fcaf152
parent 106 d2992f011930
child 110 601fc908d9f1
permissions -rw-r--r--
little typos

import re
from inspect import getcallargs
from .error import ValidateException

def vuserhash(hash):
    '''vailidate function for userhash'''
    if not re.match(r'^[a-f0-9]{15,}$', hash.lower()):
        raise ValidateException()
    return True

def validate(**kargs):
    '''validate decorator
    use it like this:
        @validate(userhash=vuserhash)
        f(userhash)
    that will validate usrhah with the function vuserhash.
    Every validate function should raise an Exception, if the the value is not valid'''
    def v(f):
        def new_f(*a,**k):
            kp=getcallargs(f,*a,**k)
            for i in kargs:
                kargs[i](kp[i])
            return f(*a,**k)
        new_f.__name__ = f.__name__
        return new_f
    return v