Skip to content

Commit

Permalink
merge from devel
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-merzky committed Oct 25, 2023
2 parents 5caf54c + d88410e commit 354604c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/radical/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions tests/unittests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

# pylint: disable=protected-access

import glob
import os
import shutil
import tempfile

from unittest import TestCase

Expand All @@ -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):
Expand Down Expand Up @@ -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

# ------------------------------------------------------------------------------

0 comments on commit 354604c

Please sign in to comment.