Skip to content

Commit

Permalink
load multiples settings separated by comma (like a pipeline)
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed May 28, 2015
1 parent 1408e08 commit 1f2ae96
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
40 changes: 24 additions & 16 deletions simple_settings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@
import sys


def _get_settings_from_cmd_line():
for arg in sys.argv[1:]:
if arg.startswith('--settings'):
try:
return arg.split('=')[1]
except IndexError:
return None


class _Settings(object):

def __init__(self):
self._dict = {}
self._setup()

def _setup(self):
self._settings_module = self._get_settings_from_cmd_line()
if not self._settings_module:
self._settings_module = os.environ.get('settings')
if not self._settings_module:
settings_value = _get_settings_from_cmd_line()
if settings_value is None:
settings_value = os.environ.get('settings')
if settings_value is None:
raise RuntimeError('Settings are not configured')
self._load_settings_module()

def _get_settings_from_cmd_line(self):
for arg in sys.argv:
if arg.startswith('--settings'):
try:
return arg.split('=')[1]
except IndexError:
return None

def _load_settings_module(self):
module = importlib.import_module(self._settings_module)

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

def _load_settings_pipeline(self):
for settings_module in self._settings_list:
self._load_python_module(settings_module)

def _load_python_module(self, settings_module):
module = importlib.import_module(settings_module)
for setting in dir(module):
value = os.environ.get(setting, getattr(module, setting))
self._dict[setting] = value
Expand Down
5 changes: 5 additions & 0 deletions tests/samples/without_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-


SOME_TEXT = u'some text'
SIMPLE_INTEGER = 2
58 changes: 45 additions & 13 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@


def get_settings_by_cmd_line(module_name):
with patch.object(sys, 'argv', ['--settings={}'.format(module_name)]):
with patch.object(
sys, 'argv', ['', '--settings={}'.format(module_name)]
):
from simple_settings.core import _Settings
return _Settings()


def get_settings_by_enviroment(module_name):
def get_settings_by_environment(module_name):
with patch('os.environ.get') as mock:
mock.return_value = module_name
from simple_settings.core import _Settings
Expand All @@ -19,20 +21,33 @@ def get_settings_by_enviroment(module_name):

class TestSettings(object):

def test_should_load_module_by_cmd_line(self):
def test_should_read_cmd_line_settings_value(self):
expect_module = 'tests.samples.simple'
settings = get_settings_by_cmd_line(expect_module)

assert settings._settings_module == expect_module
assert settings._settings_list == [expect_module]

def test_should_load_module_by_enviroment(self):
def test_should_read_cmd_line_multiples_settings_value(self):
expect_modules = 'tests.samples.simple,tests.samples.complex'
settings = get_settings_by_cmd_line(expect_modules)

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

def test_should_read_environment_settings_value(self):
expect_module = 'tests.samples.complex'
with patch.object(sys, 'argv', []):
settings = get_settings_by_enviroment(expect_module)
settings = get_settings_by_environment(expect_module)

assert settings._settings_list == [expect_module]

def test_should_read_environment_multiples_settings_value(self):
expect_module = 'tests.samples.complex,tests.samples.complex'
with patch.object(sys, 'argv', []):
settings = get_settings_by_environment(expect_module)

assert settings._settings_module == expect_module
assert settings._settings_list == expect_module.split(',')

def test_simple_settings(self):
def test_load_a_simple_module_settings(self):
settings = get_settings_by_cmd_line('tests.samples.simple')

assert settings.SIMPLE_STRING == u'simple'
Expand All @@ -43,20 +58,35 @@ def test_should_inherit_settings(self):

assert settings.APPLICATION_NAME == u'Simple Settings'

def test_complex_settings(self):
def test_should_load_multiple_settings_by_pipeline(self):
settings = get_settings_by_cmd_line(
'tests.samples.simple,tests.samples.without_import'
)

assert settings.SIMPLE_STRING == u'simple'
assert settings.SOME_TEXT == u'some text'

def test_should_inherit_settings_with_pipeline(self):
settings = get_settings_by_cmd_line(
'tests.samples.simple,tests.samples.without_import'
)

assert settings.SIMPLE_INTEGER == 2

def test_load_a_complex_module_settings(self):
settings = get_settings_by_cmd_line('tests.samples.complex')

assert settings.COMPLEX_DICT['complex'] == 'settings'
assert settings.COMPLEX_DICT['foo'] == 'bar'

def test_settings_as_dict(self):
def test_read_settings_by_method_as_dict(self):
settings = get_settings_by_cmd_line('tests.samples.simple')

settings_dict = settings.as_dict()
assert settings_dict['SIMPLE_STRING'] == u'simple'
assert settings_dict['SIMPLE_INTEGER'] == 1

def test_settings_override_by_enviroment(self):
def test_setting_override_by_environment(self):
def _mock_env_side_effect(k, d=None):
return u'simple from env' if k == 'SIMPLE_STRING' else d

Expand All @@ -72,13 +102,15 @@ def test_setting_are_not_configured(self):
_Settings()

def test_setting_configured_wrong(self):
with patch.object(sys, 'argv', ['--settings', 'tests.samples.simple']):
with patch.object(
sys, 'argv', ['', '--settings', 'tests.samples.simple']
):
with pytest.raises(RuntimeError):
from simple_settings.core import _Settings
_Settings()

def test_setting_not_found(self):
with patch.object(sys, 'argv', ['--settings=foo']):
with patch.object(sys, 'argv', ['', '--settings=foo']):
with pytest.raises(ImportError):
from simple_settings.core import _Settings
_Settings()

0 comments on commit 1f2ae96

Please sign in to comment.