From 59af84161d88c672075c60255233271f9c042c07 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 7 May 2021 08:46:12 +0200 Subject: Catch UsageError raised from click.edit, if it is unable to open a editor --- patternutils/utils.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/patternutils/utils.py b/patternutils/utils.py index d78d990..ca07e2f 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -19,18 +19,22 @@ def edit(data: Union[dict[str, str], list[str]]) -> dict[str, str]: else: temporary = [f"{value.ljust(longest)}\t{value}\n" for value in data] - if (edited := click.edit("".join(temporary))) is not None: - new_data = dict() - for line in edited.split("\n"): - if line and "\t" in line: - old, new = line.split("\t") - new_data[old.strip()] = new - if new_data: - return new_data + try: + if (edited := click.edit("".join(temporary))) is not None: + new_data = dict() + for line in edited.split("\n"): + if line and "\t" in line: + old, new = line.split("\t") + new_data[old.strip()] = new + if new_data: + return new_data + else: + raise RuntimeError else: - raise RuntimeError - else: - return data + return data + except click.UsageError: + click.secho("The editor could not be launced!", fg="red", err=True) + raise SystemExit def json_dumps(data: Any, human_readable: bool = False) -> str: -- cgit v1.3.1