/gist:f832eed2ac0f3ce31edf Secret
Last active
January 22, 2016 10:55
-
-
Save mgedmin/f832eed2ac0f3ce31edf to your computer and use it in GitHub Desktop.
A fixup of http://stackoverflow.com/a/21138900/110151 (untested)
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
from functools import wraps | |
from fabric.api import task as _task | |
def fix_boolean(f): | |
true_values = ("yes", "true", "1") | |
false_values = ("no", "false", "0") | |
def fix_bool(value): | |
if isinstance(value, basestring): | |
if value.lower() in false_values: | |
return False | |
if value.lower() in true_values: | |
return True | |
return value | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
args_ = [fix_bool(arg) for arg in args] | |
kwargs_ = {k: fix_bool(v) for k,v in kwargs.iteritems()} | |
return f(*args_, **kwargs_) | |
return wrapper | |
def task(*args, **kwargs): | |
if args: | |
return _task(fix_boolean(args[0]), *args[1:], **kwargs) | |
else: | |
return lambda fn: _task(fn, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment