From 618e5c315da50b88e1fbaf69aa3712b248354203 Mon Sep 17 00:00:00 2001 From: Yannick DAYER <yannick.dayer@idiap.ch> Date: Fri, 16 Dec 2022 15:08:50 +0000 Subject: [PATCH] Revert "Merge branch 'rc-toml' into 'master'" This reverts merge request !147 --- bob/extension/__init__.py | 4 +-- bob/extension/data/defaults-config | 9 +++-- bob/extension/rc_config.py | 46 ++++++++----------------- bob/extension/scripts/config.py | 7 ++-- bob/extension/test_rc.py | 55 ++++++++++++++++++++++++------ conda/meta.yaml | 2 -- 6 files changed, 65 insertions(+), 58 deletions(-) diff --git a/bob/extension/__init__.py b/bob/extension/__init__.py index 9394f8b..16c9116 100644 --- a/bob/extension/__init__.py +++ b/bob/extension/__init__.py @@ -9,8 +9,6 @@ import contextlib import logging -from copy import deepcopy - import pkg_resources from .rc_config import _loadrc @@ -41,7 +39,7 @@ def rc_context(dict): >>> a 1 """ - old_rc = deepcopy(rc) + old_rc = rc.copy() try: rc.update(dict) yield diff --git a/bob/extension/data/defaults-config b/bob/extension/data/defaults-config index cf5ebda..f332e2b 100644 --- a/bob/extension/data/defaults-config +++ b/bob/extension/data/defaults-config @@ -1,5 +1,4 @@ -[bob.db.atnt] -directory = "/home/bob/databases/atnt" - -[bob.db.mobio] -directory = "/home/bob/databases/mobio" +{ + "bob.db.atnt.directory": "/home/bob/databases/atnt", + "bob.db.mobio.directory": "/home/bob/databases/mobio" +} diff --git a/bob/extension/rc_config.py b/bob/extension/rc_config.py index dafd269..536a421 100644 --- a/bob/extension/rc_config.py +++ b/bob/extension/rc_config.py @@ -3,12 +3,11 @@ """Implements a global configuration system for bob using json.""" +import json import logging import os -from warnings import warn - -from exposed.rc import UserDefaults +from collections import defaultdict logger = logging.getLogger(__name__) @@ -53,28 +52,20 @@ def _loadrc(): """ - warn( - "rc from bob.extension is deprecated. Please use exposed.rc instead.", - DeprecationWarning, - ) - # def _default_none_dict(dct): - # dct2 = defaultdict(lambda: None) - # dct2.update(dct) - # return dct2 - - # path = _get_rc_path() - # if not os.path.exists(path): - # logger.debug("No RC file found") - # return _default_none_dict({}) + def _default_none_dict(dct): + dct2 = defaultdict(lambda: None) + dct2.update(dct) + return dct2 - # logger.debug("Loading RC file `%s'...", path) + path = _get_rc_path() + if not os.path.exists(path): + logger.debug("No RC file found") + return _default_none_dict({}) - # with open(path, "rt") as f: - # return json.load(f, object_hook=_default_none_dict) + logger.debug("Loading RC file `%s'...", path) - # XXX ydayer202211 This will use exposed in the background while transitioning away - # from bob.extension. This has the effect to switch the format of ~/.bobrc to toml. - return UserDefaults(path=RCFILENAME, envname=ENVNAME, logger=logger) + with open(path, "rt") as f: + return json.load(f, object_hook=_default_none_dict) def _rc_to_str(context): @@ -91,11 +82,7 @@ def _rc_to_str(context): The configurations in a JSON formatted string. """ - warn( - "rc from bob.extension is deprecated. Please use exposed.rc instead.", - DeprecationWarning, - ) - return str(context) + return json.dumps(context, sort_keys=True, indent=4, separators=(",", ": ")) def _saverc(context): @@ -107,11 +94,6 @@ def _saverc(context): All the configurations to save into the rc file. """ - warn( - "rc from bob.extension is deprecated. Please use exposed.rc instead.", - DeprecationWarning, - ) - path = _get_rc_path() with open(path, "wt") as f: f.write(_rc_to_str(context)) diff --git a/bob/extension/scripts/config.py b/bob/extension/scripts/config.py index 83e60ba..3776304 100644 --- a/bob/extension/scripts/config.py +++ b/bob/extension/scripts/config.py @@ -4,10 +4,8 @@ import logging import click -from exposed.rc import UserDefaults - from .. import rc -from ..rc_config import ENVNAME, RCFILENAME, _get_rc_path, _rc_to_str, _saverc +from ..rc_config import _get_rc_path, _rc_to_str, _saverc from .click_helper import AliasedGroup, verbosity_option # Use the normal logging module. Verbosity and format of logging will be set by @@ -90,9 +88,8 @@ def set(key, value): * If something goes wrong. """ try: - rc = UserDefaults(path=RCFILENAME, envname=ENVNAME, logger=logger) rc[key] = value - rc.write() + _saverc(rc) except Exception: logger.error("Could not configure the rc file", exc_info=True) raise click.ClickException("Failed to change the configuration.") diff --git a/bob/extension/test_rc.py b/bob/extension/test_rc.py index cba564e..74c57c0 100644 --- a/bob/extension/test_rc.py +++ b/bob/extension/test_rc.py @@ -17,14 +17,13 @@ def test_rc_env(): os.environ[ENVNAME] = os.path.join(path, "defaults-config") c = _loadrc() # should load from environment variable - REFERENCE = """[bob.db.atnt] -directory = "/home/bob/databases/atnt" + REFERENCE = { + "bob.db.atnt.directory": "/home/bob/databases/atnt", + "bob.db.mobio.directory": "/home/bob/databases/mobio", + } -[bob.db.mobio] -directory = "/home/bob/databases/mobio" -""" - assert str(c) == REFERENCE - assert "random" not in c + assert c == REFERENCE + assert c["random"] is None def test_bob_config(): @@ -41,8 +40,9 @@ def test_bob_config(): result = runner.invoke(main_cli, ["config", "get", "bob.db.atnt.directory"]) assert_click_runner_result(result, 0) assert result.output == "/home/bob/databases/atnt\n", result.output + # test config get (non-existing key) - result = runner.invoke(main_cli, ["config", "get", "not.an.existing.key"]) + result = runner.invoke(main_cli, ["config", "get", "bob.db.atnt"]) assert_click_runner_result(result, 1) # test config set @@ -67,8 +67,41 @@ def test_bob_config(): ) assert_click_runner_result(result, 0) expected_output = """Displaying `bobrc': -[bob.db.atnt] -directory = "/home/bob/databases/orl_faces" - +{ + "bob.db.atnt.directory": "/home/bob/databases/orl_faces" +} """ assert expected_output == result.output, result.output + + # test config unset (with starting substring) + result = runner.invoke( + main_cli, + ["config", "unset", "bob.db.atnt"], + env={ENVNAME: bobrcfile}, + ) + result = runner.invoke( + main_cli, ["config", "get", "bob.db.atnt"], env={ENVNAME: bobrcfile} + ) + assert_click_runner_result(result, 1) + + # test config unset (with substring contained) + # reset the key / value pair + result = runner.invoke( + main_cli, + [ + "config", + "set", + "bob.db.atnt.directory", + "/home/bob/databases/orl_faces", + ], + env={ENVNAME: bobrcfile}, + ) + result = runner.invoke( + main_cli, + ["config", "unset", "--contain", "atnt"], + env={ENVNAME: bobrcfile}, + ) + result = runner.invoke( + main_cli, ["config", "get", "bob.db.atnt"], env={ENVNAME: bobrcfile} + ) + assert_click_runner_result(result, 1) diff --git a/conda/meta.yaml b/conda/meta.yaml index 665c7ba..2bdfc5c 100644 --- a/conda/meta.yaml +++ b/conda/meta.yaml @@ -21,14 +21,12 @@ requirements: - python {{ python }} - setuptools {{ setuptools }} - pip {{ pip }} - - exposed - click >=8 - click {{ click }} - click-plugins {{ click_plugins }} run: - python - setuptools - - exposed - {{ pin_compatible('click') }} - {{ pin_compatible('click-plugins') }} -- GitLab