diff --git a/docs/user/configuration.rst b/docs/user/configuration.rst index e05197d..c3b9908 100644 --- a/docs/user/configuration.rst +++ b/docs/user/configuration.rst @@ -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 ========= diff --git a/palladium/config.py b/palladium/config.py index f3cb278..a5a2146 100644 --- a/palladium/config.py +++ b/palladium/config.py @@ -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. @@ -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(',')] diff --git a/palladium/tests/test_config.py b/palladium/tests/test_config.py index a3ab706..02fec6b 100644 --- a/palladium/tests/test_config.py +++ b/palladium/tests/test_config.py @@ -1,3 +1,4 @@ +from contextlib import contextmanager from functools import reduce import operator import os @@ -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: @@ -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, @@ -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') diff --git a/palladium/tests/test_persistence.py b/palladium/tests/test_persistence.py index ef18502..071ab25 100644 --- a/palladium/tests/test_persistence.py +++ b/palladium/tests/test_persistence.py @@ -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') @@ -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')