Skip to content

Commit

Permalink
Fix #1 multiple namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Aug 20, 2015
1 parent d9c3cfb commit 4b1263c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,62 @@ admin
bar
```

# Namespace support

When you are working with multiple projects using the same environment maybe you want to use different namespaces for ENV vars based configs


```bash
export DYNACONF_DATABASE="DYNADB"
export PROJ1_DATABASE="PROJ1DB"
export PROJ2_DATABASE="PROJ2DB"
```

and then access them


```python
from dynaconf import settings

# configure() or configure('settingsmodule.path') is needed
# only when DYNACONF_SETINGS_MODULE is not defined
settings.configure()

# access default namespace settings
settings.DATABASE
'DYNADB'

# switch namespaces
settings.namespace('PROJ1')
settings.DATABASE
'PROJ1DB'

settings.namespace('PROJ2')
settings.DATABASE
'PROJ2DB'

# return to default, call it without args
settings.namespace()
settings.DATABASE
'DYNADB'
```

You can also use the context manager:

```python
settings.DATABASE
'DYNADB'

with settings.using_namespace('PROJ1'):
settings.DATABASE
'PROJ1DB'

with settings.using_namespace('PROJ2'):
settings.DATABASE
'PROJ2DB'

settings.DATABASE
'DYNADB'
```

> This was inspired by django.conf.settings
22 changes: 21 additions & 1 deletion dynaconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import os
import importlib
from contextlib import contextmanager
from dynaconf import default_settings
from dynaconf.utils.parse_conf import parse_conf_data
from dynaconf.conf.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -89,7 +90,26 @@ def values(self):
def get(self, key, default=None):
return self.store.get(key, default)

def load_from_envvar_namespace(self, namespace='DYNACONF', silent=True):
@contextmanager
def using_namespace(self, namespace):
try:
self.namespace(namespace)
yield
finally:
self.namespace()

def namespace(self, namespace=None):
namespace = namespace or 'DYNACONF'
if not isinstance(namespace, basestring):
raise AttributeError('namespace should be a string')
if "_" in namespace:
raise AttributeError('namespace should not contains _')
self.DYNACONF_NAMESPACE = namespace.upper()
self.load_from_envvar_namespace(namespace=namespace, silent=False)

def load_from_envvar_namespace(self, namespace=None, silent=True):
namespace = namespace or getattr(
self, 'DYNACONF_NAMESPACE', 'DYNACONF')
try:
data = {
key.partition('_')[-1]: parse_conf_data(data)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='dynaconf',
version="0.1.0",
version="0.1.1",
url='https://github.com/rochacbruno/dynaconf',
license='MIT',
author="Bruno Rocha",
Expand Down

0 comments on commit 4b1263c

Please sign in to comment.