From bc9aa45b49a2527532759eb48f0df4b301243e57 Mon Sep 17 00:00:00 2001 From: GeorgianaElena Date: Tue, 28 May 2019 16:13:42 +0300 Subject: [PATCH] Add unset property option --- docs/topic/tljh-config.rst | 15 +++++++- tests/test_config.py | 63 ++++++++++++++++++++++++++++++++ tljh/config.py | 75 +++++++++++++++++++++++++++++++++++++- 3 files changed, 150 insertions(+), 3 deletions(-) diff --git a/docs/topic/tljh-config.rst b/docs/topic/tljh-config.rst index 4c62e17..577d9a1 100644 --- a/docs/topic/tljh-config.rst +++ b/docs/topic/tljh-config.rst @@ -22,8 +22,8 @@ You can run ``tljh-config`` in two ways: .. _tljh-set: -Set a configuration property -============================ +Set / Unset a configuration property +==================================== TLJH's configuration is organized in a nested tree structure. You can 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. +To unset a configuration property you can use the following command: + +.. code-block:: bash + + sudo tljh-config unset + +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 ```` are listed below by categories: diff --git a/tests/test_config.py b/tests/test_config.py index ce61330..d7bba6c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -58,6 +58,57 @@ def test_set_overwrite(): 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(): conf = {} @@ -161,6 +212,18 @@ def test_cli_set_int(tljh_dir): 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): config.main(["add-item", "foo.bar", "1.25"]) cfg = configurer.load_config() diff --git a/tljh/config.py b/tljh/config.py index 39edbd3..ee0150f 100644 --- a/tljh/config.py +++ b/tljh/config.py @@ -61,6 +61,50 @@ def set_item_in_config(config, property_path, value): 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): """ 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) for i, cur_path in enumerate(path_components): 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]): raise ValueError(f'{property_path} is not a list') cur_part = cur_part[cur_path] @@ -141,6 +185,24 @@ def set_config_value(config_path, key_path, value): 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): """ Add value to list at key_path @@ -260,6 +322,15 @@ def main(argv=None): 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', help='Set a configuration property' @@ -317,6 +388,8 @@ def main(argv=None): show_config(args.config_path) elif args.action == 'set': 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': add_config_value(args.config_path, args.key_path, parse_value(args.value)) elif args.action == 'remove-item':