mirror of
https://github.com/jupyterhub/the-littlest-jupyterhub.git
synced 2025-12-18 21:54:05 +08:00
Merge pull request #352 from GeorgianaElena/issue203
Add "tljh-config unset" option
This commit is contained in:
@@ -22,8 +22,8 @@ You can run ``tljh-config`` in two ways:
|
|||||||
.. _tljh-set:
|
.. _tljh-set:
|
||||||
|
|
||||||
|
|
||||||
Set a configuration property
|
Set / Unset a configuration property
|
||||||
============================
|
====================================
|
||||||
|
|
||||||
TLJH's configuration is organized in a nested tree structure. You can
|
TLJH's configuration is organized in a nested tree structure. You can
|
||||||
set a particular property with the following command:
|
set a particular property with the following command:
|
||||||
@@ -50,6 +50,17 @@ do so with the following:
|
|||||||
|
|
||||||
This can only set string and numerical properties, not lists.
|
This can only set string and numerical properties, not lists.
|
||||||
|
|
||||||
|
To unset a configuration property you can use the following command:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
sudo tljh-config unset <property-path>
|
||||||
|
|
||||||
|
Unsetting a configuration property removes the property from the configuration
|
||||||
|
file. If what you want is only to change the property's value, you should use
|
||||||
|
``set`` and overwrite it with the desired value.
|
||||||
|
|
||||||
|
|
||||||
Some of the existing ``<property-path>`` are listed below by categories:
|
Some of the existing ``<property-path>`` are listed below by categories:
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,57 @@ def test_set_overwrite():
|
|||||||
assert new_conf == {'a': 'hi'}
|
assert new_conf == {'a': 'hi'}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unset_no_mutate():
|
||||||
|
conf = {'a': 'b'}
|
||||||
|
|
||||||
|
new_conf = config.unset_item_from_config(conf, 'a')
|
||||||
|
assert conf == {'a': 'b'}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unset_one_level():
|
||||||
|
conf = {'a': 'b'}
|
||||||
|
|
||||||
|
new_conf = config.unset_item_from_config(conf, 'a')
|
||||||
|
assert new_conf == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unset_multi_level():
|
||||||
|
conf = {
|
||||||
|
'a': {'b': 'c', 'd': 'e'},
|
||||||
|
'f': 'g'
|
||||||
|
}
|
||||||
|
|
||||||
|
new_conf = config.unset_item_from_config(conf, 'a.b')
|
||||||
|
assert new_conf == {
|
||||||
|
'a': {'d': 'e'},
|
||||||
|
'f': 'g'
|
||||||
|
}
|
||||||
|
new_conf = config.unset_item_from_config(new_conf, 'a.d')
|
||||||
|
assert new_conf == {'f': 'g'}
|
||||||
|
new_conf = config.unset_item_from_config(new_conf, 'f')
|
||||||
|
assert new_conf == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unset_and_clean_empty_configs():
|
||||||
|
conf = {
|
||||||
|
'a': {'b': {'c': {'d': {'e': 'f'}}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
new_conf = config.unset_item_from_config(conf, 'a.b.c.d.e')
|
||||||
|
assert new_conf == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_unset_config_error():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
config.unset_item_from_config({}, 'a')
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
config.unset_item_from_config({'a': 'b'}, 'b')
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
config.unset_item_from_config({'a': {'b': 'c'}}, 'a.z')
|
||||||
|
|
||||||
|
|
||||||
def test_add_to_config_one_level():
|
def test_add_to_config_one_level():
|
||||||
conf = {}
|
conf = {}
|
||||||
|
|
||||||
@@ -161,6 +212,18 @@ def test_cli_set_int(tljh_dir):
|
|||||||
assert cfg['https']['port'] == 123
|
assert cfg['https']['port'] == 123
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_unset(tljh_dir):
|
||||||
|
config.main(["set", "foo.bar", "1"])
|
||||||
|
config.main(["set", "foo.bar2", "2"])
|
||||||
|
cfg = configurer.load_config()
|
||||||
|
assert cfg['foo'] == {'bar': 1, 'bar2': 2}
|
||||||
|
|
||||||
|
config.main(["unset", "foo.bar"])
|
||||||
|
cfg = configurer.load_config()
|
||||||
|
|
||||||
|
assert cfg['foo'] == {'bar2': 2}
|
||||||
|
|
||||||
|
|
||||||
def test_cli_add_float(tljh_dir):
|
def test_cli_add_float(tljh_dir):
|
||||||
config.main(["add-item", "foo.bar", "1.25"])
|
config.main(["add-item", "foo.bar", "1.25"])
|
||||||
cfg = configurer.load_config()
|
cfg = configurer.load_config()
|
||||||
|
|||||||
@@ -61,6 +61,50 @@ def set_item_in_config(config, property_path, value):
|
|||||||
|
|
||||||
return config_copy
|
return config_copy
|
||||||
|
|
||||||
|
def unset_item_from_config(config, property_path):
|
||||||
|
"""
|
||||||
|
Unset key at property_path in config & return new config.
|
||||||
|
|
||||||
|
config is not mutated.
|
||||||
|
|
||||||
|
property_path is a series of dot separated values.
|
||||||
|
"""
|
||||||
|
path_components = property_path.split('.')
|
||||||
|
|
||||||
|
# Mutate a copy of the config, not config itself
|
||||||
|
cur_part = config_copy = deepcopy(config)
|
||||||
|
|
||||||
|
def remove_empty_configs(configuration, path):
|
||||||
|
"""
|
||||||
|
Delete the keys that hold an empty dict.
|
||||||
|
|
||||||
|
This might happen when we delete a config property
|
||||||
|
that has no siblings from a multi-level config.
|
||||||
|
"""
|
||||||
|
if not path:
|
||||||
|
return configuration
|
||||||
|
conf_iter = configuration
|
||||||
|
for cur_path in path:
|
||||||
|
if conf_iter[cur_path] == {}:
|
||||||
|
del conf_iter[cur_path]
|
||||||
|
remove_empty_configs(configuration, path[:-1])
|
||||||
|
else:
|
||||||
|
conf_iter = conf_iter[cur_path]
|
||||||
|
|
||||||
|
for i, cur_path in enumerate(path_components):
|
||||||
|
if i == len(path_components) - 1:
|
||||||
|
if cur_path not in cur_part:
|
||||||
|
raise ValueError(f'{property_path} does not exist in config!')
|
||||||
|
del cur_part[cur_path]
|
||||||
|
remove_empty_configs(config_copy, path_components[:-1])
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
if cur_path not in cur_part:
|
||||||
|
raise ValueError(f'{property_path} does not exist in config!')
|
||||||
|
cur_part = cur_part[cur_path]
|
||||||
|
|
||||||
|
return config_copy
|
||||||
|
|
||||||
def add_item_to_config(config, property_path, value):
|
def add_item_to_config(config, property_path, value):
|
||||||
"""
|
"""
|
||||||
Add an item to a list in config.
|
Add an item to a list in config.
|
||||||
@@ -97,7 +141,7 @@ def remove_item_from_config(config, property_path, value):
|
|||||||
cur_part = config_copy = deepcopy(config)
|
cur_part = config_copy = deepcopy(config)
|
||||||
for i, cur_path in enumerate(path_components):
|
for i, cur_path in enumerate(path_components):
|
||||||
if i == len(path_components) - 1:
|
if i == len(path_components) - 1:
|
||||||
# Final component, it must be a list and we append to it
|
# Final component, it must be a list and we delete from it
|
||||||
if cur_path not in cur_part or not _is_list(cur_part[cur_path]):
|
if cur_path not in cur_part or not _is_list(cur_part[cur_path]):
|
||||||
raise ValueError(f'{property_path} is not a list')
|
raise ValueError(f'{property_path} is not a list')
|
||||||
cur_part = cur_part[cur_path]
|
cur_part = cur_part[cur_path]
|
||||||
@@ -141,6 +185,24 @@ def set_config_value(config_path, key_path, value):
|
|||||||
yaml.dump(config, f)
|
yaml.dump(config, f)
|
||||||
|
|
||||||
|
|
||||||
|
def unset_config_value(config_path, key_path):
|
||||||
|
"""
|
||||||
|
Unset key at key_path in config_path
|
||||||
|
"""
|
||||||
|
# FIXME: Have a file lock here
|
||||||
|
# FIXME: Validate schema here
|
||||||
|
try:
|
||||||
|
with open(config_path) as f:
|
||||||
|
config = yaml.load(f)
|
||||||
|
except FileNotFoundError:
|
||||||
|
config = {}
|
||||||
|
|
||||||
|
config = unset_item_from_config(config, key_path)
|
||||||
|
|
||||||
|
with open(config_path, 'w') as f:
|
||||||
|
yaml.dump(config, f)
|
||||||
|
|
||||||
|
|
||||||
def add_config_value(config_path, key_path, value):
|
def add_config_value(config_path, key_path, value):
|
||||||
"""
|
"""
|
||||||
Add value to list at key_path
|
Add value to list at key_path
|
||||||
@@ -260,6 +322,15 @@ def main(argv=None):
|
|||||||
help='Show current configuration'
|
help='Show current configuration'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unset_parser = subparsers.add_parser(
|
||||||
|
'unset',
|
||||||
|
help='Unset a configuration property'
|
||||||
|
)
|
||||||
|
unset_parser.add_argument(
|
||||||
|
'key_path',
|
||||||
|
help='Dot separated path to configuration key to unset'
|
||||||
|
)
|
||||||
|
|
||||||
set_parser = subparsers.add_parser(
|
set_parser = subparsers.add_parser(
|
||||||
'set',
|
'set',
|
||||||
help='Set a configuration property'
|
help='Set a configuration property'
|
||||||
@@ -317,6 +388,8 @@ def main(argv=None):
|
|||||||
show_config(args.config_path)
|
show_config(args.config_path)
|
||||||
elif args.action == 'set':
|
elif args.action == 'set':
|
||||||
set_config_value(args.config_path, args.key_path, parse_value(args.value))
|
set_config_value(args.config_path, args.key_path, parse_value(args.value))
|
||||||
|
elif args.action == 'unset':
|
||||||
|
unset_config_value(args.config_path, args.key_path)
|
||||||
elif args.action == 'add-item':
|
elif args.action == 'add-item':
|
||||||
add_config_value(args.config_path, args.key_path, parse_value(args.value))
|
add_config_value(args.config_path, args.key_path, parse_value(args.value))
|
||||||
elif args.action == 'remove-item':
|
elif args.action == 'remove-item':
|
||||||
|
|||||||
Reference in New Issue
Block a user