iro/validate.py
branchdevel
changeset 196 ee2c051fbe3f
parent 193 e5ec4bfa4929
child 267 ef2df3f23cb1
equal deleted inserted replaced
195:74d12f301a5d 196:ee2c051fbe3f
   131         ret=ret[0]
   131         ret=ret[0]
   132     return ret
   132     return ret
   133 
   133 
   134 def validate(kwd,func, need=True,*args,**kargs):
   134 def validate(kwd,func, need=True,*args,**kargs):
   135     '''validate decorator
   135     '''validate decorator
   136     use it like this:
   136 use it like this:
   137         @validate(kwd=userhash, func=vuserhash)
   137   @validate(kwd=userhash, func=vuserhash)
   138         f(userhash)
   138   f(userhash)
   139     that will validate usrhash with the function vuserhash.
   139 that will validate usrhash with the function vuserhash.
   140     Every validate function should raise an Exception, if the the value is not valid'''
   140 Every validate function should raise an Exception, if the the value is not valid.
       
   141 All args and kargs are used to call the validate function.
       
   142 if need is True, the kwd can't be None.'''
   141     @decorator
   143     @decorator
   142     def v(f,*a,**k):
   144     def v(f,*a,**k):
   143         kp=getcallargs(f,*a,**k)
   145         kp=getcallargs(f,*a,**k)
   144         def dfunc(*x,**y):
   146         def dfunc(*x,**y):
   145             return None
   147             return None
   146         try:
   148         try:
   147             if need or kp[kwd] is not None:
   149             if kp[kwd] is not None:
   148                 dfunc=func
   150                 dfunc=func
       
   151             elif need:
       
   152                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
   149         except KeyError:
   153         except KeyError:
   150             if need:
   154             if need:
   151                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
   155                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
       
   156             kp[kwd] = None
   152 
   157 
   153         def _gotResult(value):
   158         def _gotResult(value):
   154             kp[kwd] = value
   159             kp[kwd] = value
   155             e = defer.maybeDeferred(f,**kp)
   160             e = defer.maybeDeferred(f,**kp)
   156             return e
   161             return e