Advertisement
Guest User

Untitled

a guest
May 25th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import distutils.util
  2. import functools
  3. import inspect
  4.  
  5. from fabric.decorators import task
  6.  
  7.  
  8. class bool_args(object):
  9. def __init__(self, *bool_arg_names):
  10. self.bool_arg_names = bool_arg_names
  11.  
  12. def __call__(self, f):
  13. @functools.wraps(f)
  14. def wrapper(*args, **kwargs):
  15. callargs = inspect.getcallargs(f, *args, **kwargs)
  16. for k, v in callargs.viewitems():
  17. if k in self.bool_arg_names and not isinstance(v, bool):
  18. callargs[k] = bool(distutils.util.strtobool(v))
  19. return f(**callargs)
  20. return wrapper
  21.  
  22.  
  23. @task
  24. @bool_args('my_bool_arg')
  25. def do_something(my_bool_arg=True):
  26. assert isinstance(my_bool_arg, bool)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement