iro/validate.py
branchdevel
changeset 106 d2992f011930
child 109 935b5fcaf152
equal deleted inserted replaced
105:48f2b8adc7da 106:d2992f011930
       
     1 import re
       
     2 from inspect import getcallargs
       
     3 from .error import ValidateException
       
     4 
       
     5 def vuserhash(hash):
       
     6     '''vailidate function for userhash'''
       
     7     if not re.match(r'^[a-f0-9]{15,}$', hash.lower()):
       
     8         raise ValidateException()
       
     9     return True
       
    10 
       
    11 def validate(**kargs):
       
    12     '''validate decorator
       
    13     use it like this:
       
    14         @validate(userhash=vuserhash)
       
    15         f(userhash)
       
    16     that will validate usrhah with the function vuserhash.
       
    17     Every validate function should raise an Exception, if the the value is not valide'''
       
    18     def v(f):
       
    19         def new_f(*a,**k):
       
    20             kp=getcallargs(f,*a,**k)
       
    21             for i in kargs:
       
    22                 kargs[i](kp[i])
       
    23             return f(*a,**k)
       
    24         new_f.__name__ = f.__name__
       
    25         return new_f
       
    26     return v
       
    27