From c6a151bffd1375b410066dd85584b3bc5f28ad96 Mon Sep 17 00:00:00 2001 From: Josef Friedrich Date: Tue, 16 Jan 2024 21:08:17 +0100 Subject: [PATCH] Refactor cli helper --- mscxyz/cli_legacy.py | 10 ++-------- tests/helper.py | 35 ++++++++++++++++++++--------------- tests/test_meta.py | 23 ++++++++++++----------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/mscxyz/cli_legacy.py b/mscxyz/cli_legacy.py index 659038a..8d8aa60 100644 --- a/mscxyz/cli_legacy.py +++ b/mscxyz/cli_legacy.py @@ -18,6 +18,7 @@ from mscxyz import Score, cli_legacy, utils from mscxyz.meta import Interface, InterfaceReadWrite from mscxyz.rename import rename +from mscxyz.settings import parse_args def list_fields( @@ -586,14 +587,7 @@ def no_error(error: Type[LxmlError], errors: list[Exception]) -> bool: def execute(cli_args: typing.Sequence[str] | None = None) -> None: - args: settings.DefaultArguments = typing.cast( - settings.DefaultArguments, cli_legacy.parser.parse_args(cli_args) - ) - if args.general_config_file: - config = settings.parse_config_ini(args.general_config_file) - if config: - args = settings.merge_config_into_args(config, args) - settings.set_args(args) + args = parse_args(parser, cli_args) if args.subcommand == "help": show_all_help(args) diff --git a/tests/helper.py b/tests/helper.py index f511f7c..0195673 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -131,38 +131,44 @@ class Cli: __args: list[CliArg] __score_pre: Optional[Score] = None __score: Optional[Score] = None + __appended_score: Optional[Score] = None __executed: bool = False __stdout: Optional[str] = None __stderr: Optional[str] = None - __returncode: int __legacy: bool = False + __append_score: bool def __init__( self, *args: CliArg, append_score: bool = True, legacy: bool = False ) -> None: self.__args = list(args) - last = self.__args[-1] self.__legacy = legacy + self.__append_score = append_score - if not append_score: - return - - if isinstance(last, Score): - self.__set_score(last) - - if not (isinstance(last, str) and Path(last).exists()) and not isinstance( - last, Score - ): - self.__set_score(get_score("score.mscz", version=4), append_to_args=True) - - def __set_score(self, score: Score, append_to_args: bool = False) -> None: + def __set_score(self, score: Score, append_to_args: bool = False) -> Score: self.__score_pre = score self.__score = score if append_to_args: self.__args.append(score) + self.__appended_score = score + return score + + @property + def __last_arg(self) -> CliArg: + return self.__args[-1] @property def __stringified_args(self) -> list[str]: + if self.__append_score and self.__appended_score is None: + if isinstance(self.__last_arg, Score): + self.__set_score(self.__last_arg) + elif not ( + isinstance(self.__last_arg, str) and Path(self.__last_arg).exists() + ) and not isinstance(self.__last_arg, Score): + self.__set_score( + get_score("score.mscz", version=4), append_to_args=True + ) + result: list[str] = [] for arg in self.__args: if isinstance(arg, Score): @@ -235,7 +241,6 @@ def sysexit(self) -> str: capture_output=True, encoding="utf-8", ) - self.__returncode = result.returncode self.__stderr = result.stderr self.__stdout = result.stdout self.__executed = True diff --git a/tests/test_meta.py b/tests/test_meta.py index 8f9a20b..ee68edc 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -872,17 +872,18 @@ def test_multiple_times(self) -> None: assert i.vbox_composer == "vc" def test_with_templating_legacy(self) -> None: - tmp = helper.get_score("meta-all-values.mscx") - Cli( - "meta", - "--set-field", - "vbox_title", - "$vbox_title ($vbox_composer)", - tmp, - legacy=True, - ).execute() - i = reload(tmp) - assert i.vbox_title == "vbox_title (vbox_composer)" + c = ( + Cli( + "meta", + "--set-field", + "vbox_title", + "$vbox_title ($vbox_composer)", + legacy=True, + ) + .use_score("meta-all-values.mscz") + .execute() + ) + assert c.post.meta.interface.vbox_title == "vbox_title (vbox_composer)" def test_with_templating(self) -> None: c = (