diff options
| author | Dennis Fink | 2025-08-14 23:12:47 +0200 |
|---|---|---|
| committer | Dennis Fink | 2025-08-14 23:12:47 +0200 |
| commit | f12ec039aba104e95d152ca001a5d03053e7e952 (patch) | |
| tree | 976d16581cb12b8ff47fd42c887f2eabafce4b75 | |
| parent | bd3e6604802280147bb5913e5021b64aadd25d0c (diff) | |
| download | patternutils-f12ec039aba104e95d152ca001a5d03053e7e952.tar.gz patternutils-f12ec039aba104e95d152ca001a5d03053e7e952.zip | |
Better timing
| -rw-r--r-- | patternutils/commands/patterncfg.py | 6 | ||||
| -rw-r--r-- | patternutils/commands/targs.py | 17 | ||||
| -rw-r--r-- | patternutils/commands/tln.py | 16 | ||||
| -rw-r--r-- | patternutils/config.py | 2 | ||||
| -rw-r--r-- | patternutils/template.py | 4 | ||||
| -rw-r--r-- | patternutils/types.py | 6 |
6 files changed, 24 insertions, 27 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py index ee71d86..5972397 100644 --- a/patternutils/commands/patterncfg.py +++ b/patternutils/commands/patterncfg.py @@ -18,14 +18,10 @@ else: from .. import config from ..types import ( PredefinedCommands, - RegexCommands, - TemplateCommands, TemplateExtensions, ) -CURRENT_SUPPORTED_PROGRAMS: tuple[RegexCommands | TemplateCommands, ...] = tuple( - itertools.chain(get_args(RegexCommands), get_args(TemplateCommands)) -) +CURRENT_SUPPORTED_PROGRAMS: tuple[PredefinedCommands] = get_args(PredefinedCommands) @click.group(context_settings={"help_option_names": ("-h", "--help", "-?")}) diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index 3bb0ce8..a90313a 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -15,7 +15,7 @@ def run_command( command: str, run: functools.partial[subprocess.CompletedProcess[str]] | Callable[..., None], shell: bool, -) -> None: +) -> subprocess.CompletedProcess[str] | None: """Executes a given command using the provided run function. Args: @@ -23,7 +23,7 @@ def run_command( run: A callable function for execution (e.g., subprocess.run or print). shell: Whether to run the command in a shell. """ - run(shlex.split(command) if not shell else command) + return run(shlex.split(command) if not shell else command) @click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) @@ -105,16 +105,17 @@ def run_command( def targs( command: str, read_from: TextIO, - shell: bool, + stream: bool, + interactive: bool, + editor: bool, template_engine: TemplateExtensions, - max_procs: int, + use_predefined_template: bool, + shell: bool, redirect_stdout: bool, redirect_stderr: bool, - use_predefined_template: bool, - stream: bool, - editor: bool, - verbose: bool, + max_procs: int, dry_run: bool, + verbose: bool, ) -> None: """Execute commands based on a template string. diff --git a/patternutils/commands/tln.py b/patternutils/commands/tln.py index 1de4ccd..4e1e616 100644 --- a/patternutils/commands/tln.py +++ b/patternutils/commands/tln.py @@ -9,14 +9,14 @@ from .. import config, template, utils from ..types import TemplateExtensions -def link_file(old_filename: str, new_filename: str, symbolic: bool) -> None: - if dirs := os.path.dirname(new_filename): +def link_file(source: str, destination: str, symbolic: bool) -> None: + if dirs := os.path.dirname(destination): os.makedirs(dirs, exist_ok=True) if symbolic: - os.symlink(old_filename, new_filename) + os.symlink(source, destination) else: - os.link(old_filename, new_filename) + os.link(source, destination) @click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) @@ -164,12 +164,12 @@ def tln( if has_existing and abort_on_path_exist: raise SystemExit - files_to_move = new_filenames.items() if force else non_existing.items() + files_to_link = new_filenames.items() if force else non_existing.items() - for old, new in files_to_move: + for src, dst in files_to_link: if verbose or interactive: - click.echo(f"{old} -> {new}") + click.echo(f"{src} -> {dst}") if not (interactive and not click.confirm("Is the match correct?")): if not dry_run: - link_file(old, new, symbolic) + link_file(src, dst, symbolic) diff --git a/patternutils/config.py b/patternutils/config.py index b236ec2..4d38b21 100644 --- a/patternutils/config.py +++ b/patternutils/config.py @@ -62,7 +62,7 @@ def load_template( output_pattern = template_lines.replace("\n", "") extension = os.path.splitext(template_file)[1][1:].lower() - if extension not in ("regex", "python", "jinja2"): + if extension not in get_args(PredefinedExtensions): raise ValueError( f"Invalid template extension '{extension}' in '{template_file}'." ) diff --git a/patternutils/template.py b/patternutils/template.py index 0e9e1f7..19de66c 100644 --- a/patternutils/template.py +++ b/patternutils/template.py @@ -1,7 +1,7 @@ import os.path import shlex from datetime import datetime -from typing import Callable, cast +from typing import Callable import jinja2 @@ -83,6 +83,6 @@ def get_render_function( ) ) env_template = jinja_env_overlay.from_string(template) - return cast(Callable[..., str], env_template.render) + return env_template.render else: return template.format diff --git a/patternutils/types.py b/patternutils/types.py index cf5c8f0..ad27ebf 100644 --- a/patternutils/types.py +++ b/patternutils/types.py @@ -8,7 +8,7 @@ RegexExtensions = Literal["regex"] TemplateCommands = Literal["tmv", "targs", "tln"] TemplateExtensions = Literal["python", "jinja2"] -PredefinedCommands = RegexCommands | TemplateCommands -PredefinedExtensions = TemplateExtensions | RegexExtensions +PredefinedCommands = Literal[RegexCommands, TemplateCommands] +PredefinedExtensions = Literal[TemplateExtensions, RegexExtensions] -CommandNames = PlainCommands | TemplateCommands | RegexCommands +CommandNames = Literal[PlainCommands, TemplateCommands, RegexCommands] |
