From 1200dc3cdbacf16de42a60403a9102c24c731e37 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Mon, 18 Sep 2023 13:08:48 -0400 Subject: [PATCH] Reworking 'config update' to allow more general entry updates --- README.md | 1 + src/transpose/console.py | 10 +++++++--- src/transpose/transpose.py | 8 +++++--- tests/test_console.py | 5 +++-- tests/test_transpose.py | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 09ae9a2..ccb444d 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ transpose config add "NewEntry" "/path/to/location" transpose config get "NewEntry" transpose config list transpose config remove "NewEntry" +transpose config update "NewEntry" "path" "/path/to/new/location" ``` diff --git a/src/transpose/console.py b/src/transpose/console.py index 985670b..6af4213 100644 --- a/src/transpose/console.py +++ b/src/transpose/console.py @@ -43,7 +43,7 @@ def run(args, config_path) -> None: t.config.remove(args.name) t.config.save(config_path) elif args.config_action == "update": - t.config.update(args.name, args.path) + t.config.update(args.name, args.field_key, args.field_value) t.config.save(config_path) @@ -205,8 +205,12 @@ def parse_arguments(args=None): help="The name of the entry in the store path", ) config_update_parser.add_argument( - "path", - help="The path of the directory that should be symlinked to the store", + "field_key", + help="The config key to be updated", + ) + config_update_parser.add_argument( + "field_value", + help="The value to updated in the config", ) return parser.parse_args(args) diff --git a/src/transpose/transpose.py b/src/transpose/transpose.py index 0948644..20ab575 100644 --- a/src/transpose/transpose.py +++ b/src/transpose/transpose.py @@ -1,5 +1,6 @@ from dataclasses import asdict, dataclass, field from pathlib import Path +from typing import Any # from typing import Self @@ -77,19 +78,20 @@ class TransposeConfig: except KeyError: raise TransposeError(f"'{name}' does not exist in Transpose config entries") - def update(self, name: str, path: str) -> None: + def update(self, name: str, field_key: str, field_value: Any) -> None: """ Update an entry by name Args: name: The name of the entry (must exist) - path: The path where the entry originally exists + field_key: The key to update + field_value: The value to update Returns: None """ try: - self.entries[name].path = path + setattr(self.entries[name], field_key, field_value) except KeyError: raise TransposeError(f"'{name}' does not exist in Transpose config entries") diff --git a/tests/test_console.py b/tests/test_console.py index eee8341..950f5ad 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -217,9 +217,10 @@ def test_run_config_remove(): @setup_restore() def test_run_config_update(): args = RunConfigArgs("update") - args.path = "/var/tmp/something" + args.field_key = "path" + args.field_value = "/var/tmp/something" run_console(args, TRANSPOSE_CONFIG_PATH) config = TransposeConfig().load(TRANSPOSE_CONFIG_PATH) - assert config.entries[args.name].path == args.path + assert config.entries[args.name].path == args.field_value diff --git a/tests/test_transpose.py b/tests/test_transpose.py index 06cd87f..a147a98 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -157,9 +157,9 @@ def test_config_update(): with pytest.raises( TransposeError, match="does not exist in Transpose config entries" ): - config.update("UnknownEntry", "/some/new/path") + config.update("UnknownEntry", "path", "/some/new/path") - config.update(ENTRY_NAME, "/some/new/path") + config.update(ENTRY_NAME, "path", "/some/new/path") assert config.entries[ENTRY_NAME].path == "/some/new/path"