iro/validate.py
branchdevel
changeset 118 e16c0250c974
parent 115 323d06431100
child 125 19b3f383c9ce
equal deleted inserted replaced
117:351a02310dd8 118:e16c0250c974
     1 import re
     1 import re
     2 from decorator import decorator
     2 from decorator import decorator
       
     3 
       
     4 from twisted.internet import defer
     3 
     5 
     4 from inspect import getcallargs
     6 from inspect import getcallargs
     5 from .error import ValidateException
     7 from .error import ValidateException
     6 
     8 
     7 def vBool(value, field):
     9 def vBool(value, field):
    38     that will validate usrhash with the function vuserhash.
    40     that will validate usrhash with the function vuserhash.
    39     Every validate function should raise an Exception, if the the value is not valid'''
    41     Every validate function should raise an Exception, if the the value is not valid'''
    40     @decorator
    42     @decorator
    41     def v(f,*a,**k):
    43     def v(f,*a,**k):
    42         kp=getcallargs(f,*a,**k)
    44         kp=getcallargs(f,*a,**k)
       
    45         def dfunc(*x,**y):
       
    46             return None
    43         try:
    47         try:
    44             if need or kp[kwd] is not None:
    48             if need or kp[kwd] is not None:
    45                 kp[kwd] = func(kp[kwd],kwd,*args,**kargs)
    49                 dfunc=func
    46             else:
       
    47                 kp[kwd] = None
       
    48         except KeyError:
    50         except KeyError:
    49             if need:
    51             if need:
    50                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
    52                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
    51         return f(**kp)
    53 
       
    54         def _gotResult(value):
       
    55             kp[kwd] = value
       
    56             e = defer.maybeDeferred(f,**kp)
       
    57             return e
       
    58         d = defer.maybeDeferred(dfunc, kp[kwd],kwd,*args,**kargs)
       
    59         return d.addCallback(_gotResult)
    52     return v
    60     return v
    53  
    61