Skip to content

Commit

Permalink
accept settings modules and files in LazySettings init method
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed Apr 8, 2016
1 parent 82d3a51 commit f8466a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
22 changes: 20 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ $ python app.py
The `simple_settings.settings` object reads the command line and environment in this order (but simple-settings takes first value it encounters), to know which file to load.

Another option is use class `LazySettings` instead singleton object `settings`.
With `LazySettings` class is possible to determine settings files in object create:
```python
from simple_settings import LazySettings


settings = LazySettings('settings.development')
```
If you don't pass any value in _LazySettings_ init argument, this class follow the same behavior of _settings_ object.


## Example
This is a very dummy example, in real world you would use simple-settings in more complex cases.
Expand All @@ -65,7 +75,7 @@ You don't need specify which setting _simple-settings_ must load, you can do thi
```python
from simple_settings import settings

print settings.SIMPLE_CONF
print(settings.SIMPLE_CONF)
```
### **Run**

Expand Down Expand Up @@ -107,7 +117,14 @@ $ python app.py --settings=production,amazon,new_relic
```
simple-setting will load all settings modules in order that was specified (`production`-> `amazon` -> `new_relic`) overriding possibles conflicts.

But remember, the environment is still a priority.
This also work with _LazySettings_ class:
```python
from simple_settings import LazySettings


settings = LazySettings('production', 'amazon', 'new_relic')
```
You can combine any type of settings (_python modules_, _yaml_, etc.).

## Ignored settings
* Python modules:
Expand Down Expand Up @@ -173,6 +190,7 @@ assert settings.SOME_SETTING == 'bar'
## Changelog
### [NEXT_RELEASE]
* Some refactors.
* Determine settings files and modules directly in LazySettings object (to avoid use env or command line argument);

### [0.5.0] - 2016-02-03
* Some refactors.
Expand Down
2 changes: 1 addition & 1 deletion simple_settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
from .core import settings # noqa
from .core import LazySettings, settings # noqa
9 changes: 3 additions & 6 deletions simple_settings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class LazySettings(object):
ENVIRON_KEYS = ('settings', 'SIMPLE_SETTINGS')
COMMAND_LINE_ARGS = ('--settings', '--simple-settings')

def __init__(self):
self._dict = {}
self._settings_list = []
def __init__(self, *settings_list):
self._settings_list = list(settings_list)
self._initialized = False
self._dict = {}

def _get_settings_from_cmd_line(self):
for arg in sys.argv[1:]:
Expand Down Expand Up @@ -57,11 +57,8 @@ def setup(self):
raise RuntimeError('Settings are not configured')
self._settings_list = settings_value.split(',')

self._settings_list = settings_value.split(',')
self._load_settings_pipeline()

process_special_settings(self._dict)

self._initialized = True

def _load_settings_pipeline(self):
Expand Down
21 changes: 18 additions & 3 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,28 @@ def get_settings_by_environment(module_name):

class TestSettings(object):

@pytest.mark.parametrize('cmd_arg', ['settings', 'simple-settings'])
def test_should_read_init_settings_value(self):
expect_module = 'tests.samples.simple'
settings = LazySettings(expect_module)

assert settings._settings_list == [expect_module]
assert settings.SIMPLE_STRING == u'simple'

def test_should_read_init_multiples_settings_value(self):
expect_modules = 'tests.samples.simple,tests.samples.complex'
settings = LazySettings(*expect_modules.split(','))

assert settings._settings_list == expect_modules.split(',')
assert settings.SIMPLE_STRING == u'simple'
assert settings.COMPLEX_DICT['complex'] == 'settings'

@pytest.mark.parametrize('cmd_arg', LazySettings.COMMAND_LINE_ARGS)
def test_should_read_cmd_line_settings_value(self, cmd_arg):
expect_module = 'tests.samples.simple'

settings = LazySettings()
with patch.object(
sys, 'argv', ['', '--{}={}'.format(cmd_arg, expect_module)]
sys, 'argv', ['', '{}={}'.format(cmd_arg, expect_module)]
):
settings.setup()

Expand All @@ -48,7 +63,7 @@ def test_should_read_cmd_line_multiples_settings_value(self):

assert settings._settings_list == expect_modules.split(',')

@pytest.mark.parametrize('env_var', ['settings', 'SIMPLE_SETTINGS'])
@pytest.mark.parametrize('env_var', LazySettings.ENVIRON_KEYS)
def test_should_read_environment_settings_value(self, env_var):
expect_module = 'tests.samples.complex'

Expand Down

0 comments on commit f8466a4

Please sign in to comment.