Skip to content
Snippets Groups Projects
Commit 78264921 authored by Yannick DAYER's avatar Yannick DAYER
Browse files

Merge branch 'rc-toml' into 'master'

Use exposed rc implementation for toml support

See merge request !147
parents efd6074d c08087fa
No related branches found
No related tags found
1 merge request!147Use exposed rc implementation for toml support
Pipeline #66752 failed
......@@ -9,6 +9,8 @@
import contextlib
import logging
from copy import deepcopy
import pkg_resources
from .rc_config import _loadrc
......@@ -39,7 +41,7 @@ def rc_context(dict):
>>> a
1
"""
old_rc = rc.copy()
old_rc = deepcopy(rc)
try:
rc.update(dict)
yield
......
{
"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"
......@@ -3,11 +3,12 @@
"""Implements a global configuration system for bob using json."""
import json
import logging
import os
from collections import defaultdict
from warnings import warn
from exposed.rc import UserDefaults
logger = logging.getLogger(__name__)
......@@ -52,20 +53,28 @@ def _loadrc():
"""
def _default_none_dict(dct):
dct2 = defaultdict(lambda: None)
dct2.update(dct)
return dct2
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({})
# path = _get_rc_path()
# if not os.path.exists(path):
# logger.debug("No RC file found")
# return _default_none_dict({})
logger.debug("Loading RC file `%s'...", path)
# logger.debug("Loading RC file `%s'...", path)
with open(path, "rt") as f:
return json.load(f, object_hook=_default_none_dict)
# with open(path, "rt") as f:
# return json.load(f, object_hook=_default_none_dict)
# 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)
def _rc_to_str(context):
......@@ -82,7 +91,11 @@ def _rc_to_str(context):
The configurations in a JSON formatted string.
"""
return json.dumps(context, sort_keys=True, indent=4, separators=(",", ": "))
warn(
"rc from bob.extension is deprecated. Please use exposed.rc instead.",
DeprecationWarning,
)
return str(context)
def _saverc(context):
......@@ -94,6 +107,11 @@ 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))
......
......@@ -4,8 +4,10 @@ import logging
import click
from exposed.rc import UserDefaults
from .. import rc
from ..rc_config import _get_rc_path, _rc_to_str, _saverc
from ..rc_config import ENVNAME, RCFILENAME, _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
......@@ -88,8 +90,9 @@ def set(key, value):
* If something goes wrong.
"""
try:
rc = UserDefaults(path=RCFILENAME, envname=ENVNAME, logger=logger)
rc[key] = value
_saverc(rc)
rc.write()
except Exception:
logger.error("Could not configure the rc file", exc_info=True)
raise click.ClickException("Failed to change the configuration.")
......
......@@ -17,13 +17,14 @@ 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",
"bob.db.mobio.directory": "/home/bob/databases/mobio",
}
REFERENCE = """[bob.db.atnt]
directory = "/home/bob/databases/atnt"
assert c == REFERENCE
assert c["random"] is None
[bob.db.mobio]
directory = "/home/bob/databases/mobio"
"""
assert str(c) == REFERENCE
assert "random" not in c
def test_bob_config():
......@@ -40,9 +41,8 @@ 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", "bob.db.atnt"])
result = runner.invoke(main_cli, ["config", "get", "not.an.existing.key"])
assert_click_runner_result(result, 1)
# test config set
......@@ -67,41 +67,8 @@ 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)
......@@ -21,12 +21,14 @@ 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') }}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment