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

Use the exposed rc implementation for toml support

parent efd6074d
No related branches found
No related tags found
1 merge request!147Use exposed rc implementation for toml support
Pipeline #66737 failed
......@@ -11,6 +11,8 @@ import logging
import pkg_resources
from copy import deepcopy
from .rc_config import _loadrc
logger = logging.getLogger(__name__)
......@@ -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"
......@@ -7,7 +7,9 @@ import json
import logging
import os
from collections import defaultdict
from exposed.rc import UserDefaults
from warnings import warn
logger = logging.getLogger(__name__)
......@@ -52,20 +54,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 +92,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(UserDefaults(path=RCFILENAME, envname=ENVNAME, logger=logger))
def _saverc(context):
......@@ -94,6 +108,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))
......
......@@ -89,7 +89,7 @@ def set(key, value):
"""
try:
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.
Finish editing this message first!
Please register or to comment