Skip to content

Instantly share code, notes, and snippets.

@tmehlinger
Last active May 25, 2016 15:26
Show Gist options
  • Save tmehlinger/68fa817b26185c3ace9c61968d3e5119 to your computer and use it in GitHub Desktop.
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
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