Last active
May 25, 2016 15:26
-
-
Save tmehlinger/68fa817b26185c3ace9c61968d3e5119 to your computer and use it in GitHub Desktop.
A decorator for turning boolean parameters into actual bool types in Fabric tasks
This file contains hidden or 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
import distutils.util | |
import functools | |
import inspect | |
from fabric.decorators import task | |
class bool_args(object): | |
def __init__(self, *bool_arg_names): | |
self.bool_arg_names = bool_arg_names | |
def __call__(self, f): | |
@functools.wraps(f) | |
def wrapper(*args, **kwargs): | |
callargs = inspect.getcallargs(f, *args, **kwargs) | |
for k, v in callargs.viewitems(): | |
if k in self.bool_arg_names and not isinstance(v, bool): | |
callargs[k] = bool(distutils.util.strtobool(v)) | |
return f(**callargs) | |
return wrapper | |
@task | |
@bool_args('my_bool_arg') | |
def do_something(my_bool_arg=True): | |
assert isinstance(my_bool_arg, bool) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment