From c7f602eb1015337d4a9ddb9c99afcb7b96efab77 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 14 Aug 2025 23:16:29 +0200 Subject: As jinja in not optional anymore, simplify getting the render function --- patternutils/commands/targs.py | 11 +++++------ patternutils/commands/tln.py | 27 +++++++++++++++++++-------- patternutils/commands/tmv.py | 13 +++++-------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index b6fc509..a4f91e0 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -143,12 +143,11 @@ def targs( ) raise SystemExit(1) - try: - render = template.get_render_function(command, "targs", engine=template_engine) - except RuntimeError: - click.secho("Jinja2 not installed!", fg="red", err=True) - raise SystemExit(1) - + render = template.get_render_function( + command, + "targs", + engine=template_engine, + ) commands = [render(r) for r in data] if editor: diff --git a/patternutils/commands/tln.py b/patternutils/commands/tln.py index 4e1e616..cd2d4fe 100644 --- a/patternutils/commands/tln.py +++ b/patternutils/commands/tln.py @@ -85,6 +85,12 @@ def link_file(source: str, destination: str, symbolic: bool) -> None: default=False, help="exit if a renamed path already exists", ) +@click.option( + "--abort-on-key-not-found", + is_flag=True, + default=False, + help="Exit if the specified key (specified by the --key option) is not found in the input JSON.", +) @click.option( "-n", "--dry-run", @@ -113,6 +119,7 @@ def tln( key: str, stream: bool, abort_on_path_exist: bool, + abort_on_key_not_found: bool, dry_run: bool, verbose: bool, ) -> None: @@ -134,15 +141,19 @@ def tln( click.secho("Multiple files found!", fg="red", err=True) raise SystemExit + render = template.get_render_function( + output_pattern, + "tln", + engine=template_engine, + ) + new_filenames = dict() try: - render = template.get_render_function( - output_pattern, "tln", engine=template_engine - ) - except RuntimeError: - click.secho("jinja2 not installed!", fg="red", err=True) - raise SystemExit - - new_filenames = dict((entry["_abspath"], render(**entry)) for entry in data) + for entry in data: + new_filenames[entry[key]] = render(**entry) + except KeyError: + click.secho(f"Key '{key}' not found in input data.", fg="red", err=True) + if abort_on_key_not_found: + raise SystemExit(1) if editor: try: diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py index 7ddcf8e..58a75e5 100644 --- a/patternutils/commands/tmv.py +++ b/patternutils/commands/tmv.py @@ -147,14 +147,11 @@ def tmv( ) raise SystemExit(1) - try: - render = template.get_render_function( - output_pattern, "tmv", engine=template_engine - ) - except RuntimeError: - click.secho("Jinja2 not installed!", fg="red", err=True) - raise SystemExit(1) - + render = template.get_render_function( + output_pattern, + "tmv", + engine=template_engine, + ) new_filenames = dict() try: for entry in data: -- cgit v1.3.1