Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env var prefix support #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ If there's an environment variable with the same name as the given configuration

# even if the default for 'SOMETHING' or the value in the config file is different from 'value'


It's useful to use prefixes to make sure your project environment variables do not conflict with other projects's environment variables:

from derpconf.config import Config

Config.allow_environment_variables(prefix='MYAPP_')

# called program with MYAPP_SOMETHING=value myprogram.py
assert config.SOMETHING == "value"


Reloading Configurations
------------------------

Expand Down
9 changes: 6 additions & 3 deletions derpconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Config(object):
class_aliases = defaultdict(list)
class_aliased_items = {}
_allow_environment_variables = False
_environment_variables_prefix = ''

@classmethod
def define(cls, key, value, description, group='General'):
Expand All @@ -57,8 +58,9 @@ def get_conf_file(cls, conf_name, lookup_paths):
return None

@classmethod
def allow_environment_variables(cls):
def allow_environment_variables(cls, prefix=''):
cls._allow_environment_variables = True
cls._environment_variables_prefix = prefix

@classmethod
def load(cls, path, conf_name=None, lookup_paths=[], defaults={}):
Expand Down Expand Up @@ -171,11 +173,12 @@ def __setattr__(self, name, value):
super(Config, self).__setattr__(name, value)

def __getattribute__(self, name):
if name in ['allow_environment_variables']:
if name in ['allow_environment_variables', '_environment_variables_prefix']:
return super(Config, self).__getattribute__(name)

if self.allow_environment_variables:
value = os.environ.get(name, None)
prefix = self._environment_variables_prefix
value = os.environ.get(prefix + name, None)
if value is not None:
return value

Expand Down
12 changes: 12 additions & 0 deletions vows/config_vows.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ def topic(self):
def should_be_equal_to_env(self, topic):
expect(topic).to_equal("baz")

class WhenPassingPrefix(Vows.Context):
def topic(self):
os.environ['MYPREFIX_SOME_CONFIGURATION'] = "test value"
config = Config()

Config.allow_environment_variables(prefix='MYPREFIX_')

return config.SOME_CONFIGURATION

def should_be_equal_to_env(self, topic):
expect(topic).to_equal("test value")

class WhenReloading(Vows.Context):
def topic(self):
class SpecialConfig(Config):
Expand Down