Created
September 3, 2012 09:44
-
-
Save jul/3608152 to your computer and use it in GitHub Desktop.
brouillon de décorateur générique pour argument nommés ... brouillon ... brouillon ... DRAFT ... ACHTUNG .. danger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
class decorate(object): | |
def __init__(self,name="must have key",hook=None, api_hook=None,): | |
self.hook=hook | |
self.name=name | |
self.api_hook=api_hook | |
def __call__(self,*pos,**named): | |
def wrapped(func): | |
self.__module__ = func.__module__ | |
self.__doc__=func.__doc__ | |
self.__name__ = func.__name__ | |
if self.api_hook: | |
self.__api__= self.name + ":" +self.api_hook( *pos,**named) | |
def rewrapped(*a,**kw): | |
rewrapped.__name__ = self.__name__ | |
try: | |
self.hook(*pos,**named)(*a,**kw) | |
except Exception as e: | |
print self.__api__ | |
print str(e) | |
return func(*a,**kw) | |
return rewrapped | |
return wrapped | |
def must_be_in(*key): | |
def check(*a,**kw): | |
print "given %s" % kw | |
print "****kkk" | |
print "checking %s" % key | |
if set(key) & set(kw) != set(key): | |
raise Exception("missing key %s in %s" % (set(key)^( set(kw)& set(key)),kw)) | |
return check | |
def must_be_in_api(*key): | |
return ",".join(key) | |
must_have=decorate("must have key",must_be_in,must_be_in_api) | |
def must(*must_have): | |
print "add %s" % must_have | |
# print "opt?%s" % opts | |
def wrapped(func): | |
must.__module__ = func.__module__ | |
must.__doc__=func.__doc__ | |
must.__name__ = func.__name__ | |
must._api+="REQUIRES:%s\n"+",".join(must_have) | |
def rewrapped(*a,**kw): | |
for arg in must_have: | |
if arg not in kw: | |
raise Exception("Missing key %s in named arguments for function %s" % (arg, func.__name__)) | |
return func(*a,**kw) | |
return rewrapped | |
return wrapped | |
def default_value(**default): | |
def wrapped(func): | |
default_value.__module__ = func.__module__ | |
default_value.__doc__=func.__doc__ | |
default_value.__name__ = func.__name__ | |
print "DEFAULT:%s\n"+",".join(["%s=%s" % (k,v) for (k,v )in default.items()]) | |
def rewrapped(*a,**kw): | |
for key,default_val in default.items(): | |
if not key in kw: | |
kw[key]=default_val | |
return func(*a,**kw) | |
return rewrapped | |
return wrapped | |
def inrange(low,high): | |
def _in_range(_int): | |
return high >= _int >= low | |
return _in_range | |
def validate(**validator): | |
def wrapped(func): | |
validate.__module__ = func.__module__ | |
validate.__doc__=func.__doc__ | |
validate.__name__ = func.__name__ | |
print validator | |
print "VALIDATE:%s\n"+",".join(["%s=%s" % (k,v.__name__) for k,v in validator.items()]) | |
def rewrapped(*a,**kw): | |
for key,valid in validator.items(): | |
if not valid(kw[key]): | |
raise Exception("Invalid argument %s %s in named arguments for function %s" % (key,valid.__name__, func.__name__)) | |
return func(*a,**kw) | |
return rewrapped | |
return wrapped | |
@default_value(nawak=123) | |
@must_have("name") | |
@validate(name = lambda x:x.startswith("_"),port = inrange(1024,1030 )) | |
def toto(*a,**kw): | |
print a | |
print kw | |
print "done" | |
return 1 | |
toto(2, "uiui", port=1025, name= "_this") | |
try: | |
toto(2, "uiui",port=1025, name= "this") | |
except Exception as e: | |
print "************ intercepted ***************" | |
print e | |
try: | |
toto(2, "uiui", nime= "this") | |
except Exception as e: | |
print "************ intercepted ***************" | |
print e | |
print "ok" | |
toto(2, "uiui",port=0, name= "_this") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment