iro/validate.py
branchdevel
changeset 106 d2992f011930
child 109 935b5fcaf152
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/validate.py	Sun Jan 22 12:26:00 2012 +0100
@@ -0,0 +1,27 @@
+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 valide'''
+    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
+