diff --git a/src/radical/utils/config.py b/src/radical/utils/config.py index f55db688..f795f540 100644 --- a/src/radical/utils/config.py +++ b/src/radical/utils/config.py @@ -27,10 +27,12 @@ module = 'module' module_path = radical.utils.debug.find_module(module) - usr_base_dir = os.environ.get('RADICAL_CONFIG_USER_DIR') or \ - os.environ.get('HOME', '/tmp') - sys_config_dir = '%s/configs' % (module_path) - usr_config_dir = '%s/.%s/configs' % (usr_base_dir, module.replace('.', '/')) + sys_config_dir = '%s/configs' % module_path + usr_base_dir = os.getenv('RADICAL_CONFIG_USER_DIR') or \ + os.getenv('HOME') + if usr_base_dir: + usr_config_dir = '%s/.%s/configs' % \ + (usr_base_dir, module.replace('.', '/')) so the location of the module's `__init__.py` is used to derive the location of the installed system config files, and the module name is used to derive @@ -282,14 +284,18 @@ def __init__(self, module=None, category=None, name=None, cfg=None, if not modpath: raise ValueError("Cannot find module %s" % module) - home = os.environ.get('HOME', '/tmp') - home = os.environ.get('RADICAL_CONFIG_USER_DIR', home) - sys_dir = "%s/configs" % (modpath) - usr_dir = "%s/.%s/configs" % (home, module.replace('.', '/')) - fname = '%s_%s.json' % (category.replace('.', '/'), name) + fname = '%s_%s.json' % (category.replace('.', '/'), name) - sys_fspec = '%s/%s' % (sys_dir, fname) - usr_fspec = '%s/%s' % (usr_dir, fname) + sys_dir = '%s/configs' % modpath + sys_fspec = '%s/%s' % (sys_dir, fname) + + home = os.getenv('RADICAL_CONFIG_USER_DIR') or os.getenv('HOME') + if home: + usr_dir = '%s/.%s/configs' % (home, module.replace('.', '/')) + usr_fspec = '%s/%s' % (usr_dir, fname) + else: + usr_dir = None + usr_fspec = None if '*' in fname: starred = True else : starred = False @@ -371,10 +377,13 @@ def __init__(self, module=None, category=None, name=None, cfg=None, fname = '%s.json' % (category.replace('.', '/')) sys_fname = '%s/%s' % (sys_dir, fname) - usr_fname = '%s/%s' % (usr_dir, fname) + if os.path.isfile(sys_fname): + sys_cfg = read_json(sys_fname) - if os.path.isfile(sys_fname): sys_cfg = read_json(sys_fname) - if os.path.isfile(usr_fname): usr_cfg = read_json(usr_fname) + if usr_dir: + usr_fname = '%s/%s' % (usr_dir, fname) + if os.path.isfile(usr_fname): + usr_cfg = read_json(usr_fname) # merge sys, usr and app cfg before expansion diff --git a/tests/unittests/test_config.py b/tests/unittests/test_config.py index e541d086..54258a82 100755 --- a/tests/unittests/test_config.py +++ b/tests/unittests/test_config.py @@ -2,7 +2,10 @@ # pylint: disable=protected-access +import glob import os +import shutil +import tempfile from unittest import TestCase @@ -13,6 +16,25 @@ # class ConfigTestCase(TestCase): + _cleanup_files = [] + + # -------------------------------------------------------------------------- + # + @classmethod + def tearDownClass(cls) -> None: + + for p in cls._cleanup_files: + if p is None: + continue + for f in glob.glob(p): + if os.path.isdir(f): + try: + shutil.rmtree(f) + except OSError as e: + print('[ERROR] %s - %s' % (e.filename, e.strerror)) + else: + os.unlink(f) + # -------------------------------------------------------------------------- # def test_config(self): @@ -68,5 +90,36 @@ def test_default_config(self): self.assertEqual(id(dc1), id(dc2)) self.assertEqual(dc1, dc2) + # -------------------------------------------------------------------------- + # + def test_user_config(self): + + cfg_dir = tempfile.mkdtemp() + self._cleanup_files.append(cfg_dir) + + os.environ['RADICAL_CONFIG_USER_DIR'] = cfg_dir + self.assertNotEqual(os.environ['RADICAL_CONFIG_USER_DIR'], + os.environ['HOME']) + + cfg_dir += '/.radical/utils/configs' + ru.rec_makedir(cfg_dir) + + cfg = {'foo_0': {'foo_1': {'foo2': 'bar'}}} + ru.write_json(cfg, cfg_dir + '/user_default.json') + + c = ru.Config(module='radical.utils', category='user') + self.assertEqual(c.foo_0.foo_1.foo2, 'bar') + self.assertIsInstance(c.foo_0.foo_1, ru.Config) + + saved_home_dir = os.environ['HOME'] + del os.environ['HOME'] + del os.environ['RADICAL_CONFIG_USER_DIR'] + + c2 = ru.Config(module='radical.utils', category='user') + # no config data collected + self.assertFalse(c2.as_dict()) + + os.environ['HOME'] = saved_home_dir + # ------------------------------------------------------------------------------