Skip to content

Commit

Permalink
Refactor cli helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 16, 2024
1 parent 4ea8904 commit c6a151b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
10 changes: 2 additions & 8 deletions mscxyz/cli_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 20 additions & 15 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit c6a151b

Please sign in to comment.