-
-
Save eltismerino/a8ec8584034c8a7d087e to your computer and use it in GitHub Desktop.
This actually works and unlike the older version doesn't mess up the task aliases parameter
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 import tasks | |
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): | |
""" | |
The fabric.decorators.task decorator which automatically converts command line task arguments | |
to a boolean representation if applicable. | |
:param args: | |
:param kwargs: | |
:return: wrapped | |
""" | |
invoked = bool(not args or kwargs) | |
task_class = kwargs.pop("task_class", tasks.WrappedCallableTask) | |
def wrapper(f): | |
return task_class(fix_boolean(f), *args, **kwargs) | |
return wrapper if invoked else wrapper(args[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment