Skip to content

Commit

Permalink
Merge 644ed35 into f3a4372
Browse files Browse the repository at this point in the history
  • Loading branch information
dnouri committed May 13, 2019
2 parents f3a4372 + 644ed35 commit 03aa420
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
7 changes: 6 additions & 1 deletion docs/user/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ Configuration files use Python syntax. For an introduction, please
visit the :ref:`tutorial`.

Palladium uses an environment variable called ``PALLADIUM_CONFIG`` to
look up the location of the configuration file.
look up the location of one or more configuration files. If
``PALLADIUM_CONFIG`` is not set, Palladium will try to find a
configuration file at these locations:

- ``palladium-config.py``
- ``etc/palladium-config.py``

Variables
=========
Expand Down
13 changes: 13 additions & 0 deletions palladium/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
refer to the manual for more details.
"""

DEFAULT_CONFIG_FILE_LOCATIONS = (
'palladium-config.py',
os.path.join('etc', 'palladium-config.py'),
)


class Config(dict):
"""A dictionary that represents the app's configuration.
Expand Down Expand Up @@ -212,7 +217,15 @@ def _get_config(**extra):
if not _config.initialized:
_config.update(extra)
_config.initialized = True

fnames = os.environ.get('PALLADIUM_CONFIG')
if fnames is None:
for fname in DEFAULT_CONFIG_FILE_LOCATIONS:
if os.path.exists(fname): # pragma: no cover
fnames = fname
print("Using configuration at {}".format(fname))
break

if fnames is not None:
configs = []
fnames = [fname.strip() for fname in fnames.split(',')]
Expand Down
18 changes: 17 additions & 1 deletion palladium/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from functools import reduce
import operator
import os
Expand Down Expand Up @@ -38,6 +39,14 @@ def __init__(self):
self.cfg = get_config().copy()


@contextmanager
def cwd(path):
before = os.getcwd()
os.chdir(path)
yield
os.chdir(before)


def test_config_class_keyerror():
from palladium.config import Config
with pytest.raises(KeyError) as e:
Expand Down Expand Up @@ -71,7 +80,7 @@ def get_config(self):

@pytest.fixture
def config1_fname(self, tmpdir):
path = tmpdir.join('config1.py')
path = tmpdir.join('palladium-config.py')
path.write("""{
'env': environ['ENV1'],
'here': here,
Expand Down Expand Up @@ -100,6 +109,13 @@ def config3_fname(self, tmpdir):
def test_extras(self, get_config):
assert get_config(foo='bar')['foo'] == 'bar'

def test_default_config(self, get_config, config1_fname, monkeypatch):
here = os.path.dirname(config1_fname)
monkeypatch.setitem(os.environ, 'ENV1', 'one')
with cwd(here):
config = get_config()
assert config['here'] == here

def test_variables(self, get_config, config1_fname, monkeypatch):
monkeypatch.setitem(os.environ, 'PALLADIUM_CONFIG', config1_fname)
monkeypatch.setitem(os.environ, 'ENV1', 'one')
Expand Down
4 changes: 2 additions & 2 deletions palladium/tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_read(self, File):
patch('palladium.persistence.pickle.load') as load:
lm.return_value = [{'version': 99}]
lp.return_value = {'active-model': '99'}
exists.return_value = True
exists.side_effect = lambda fn: fn == '/models/model-99.pkl.gz'
open.return_value = MagicMock()
result = File('/models/model-{version}').read()
open.assert_called_with('/models/model-99.pkl.gz', 'rb')
Expand All @@ -90,7 +90,7 @@ def test_read_with_version(self, File):
patch('palladium.persistence.gzip.open') as gzopen,\
patch('palladium.persistence.pickle.load') as load:
lm.return_value = [{'version': 99}]
exists.return_value = True
exists.side_effect = lambda fn: fn == '/models/model-432.pkl.gz'
open.return_value = MagicMock()
result = File('/models/model-{version}').read(432)
open.assert_called_with('/models/model-432.pkl.gz', 'rb')
Expand Down

0 comments on commit 03aa420

Please sign in to comment.