-
-
Save bitprophet/3873fb8220ce3a8fbd24 to your computer and use it in GitHub Desktop.
implementation
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
""" | |
require replacement in decorator form | |
Will raise a NameError exception if a Required fabric enviroment variable is | |
not set, before calling the decorated task. | |
Use: | |
from fabric.api import env | |
from fabric.contrib.require import Require | |
... | |
env['important_variable'] = important_value | |
@Require('important_variable') | |
def fabric_task(): | |
... | |
""" | |
from fabric.api import env | |
class Require(object): | |
def __init__(self, *args): | |
self.required = set(args) | |
def __call__(self, function): | |
def wrapper(*args, **kwargs): | |
available = set(env.keys()) | |
missing = self.required.difference(available) | |
if (missing): | |
missing_text = ", ".join(missing) | |
raise NameError( | |
"Missing Env Variables: \"%s\" before calling: %s" % | |
(missing_text, function)) | |
return function(*args, **kwargs) | |
wrapper.__name__ = "Require(%s)" % (function.__name__) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment