|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Callable |
| 3 | + |
| 4 | +import click |
| 5 | +from click import Context, Parameter |
| 6 | + |
| 7 | +from ..core.config import list_presentation_configs |
| 8 | +from ..core.defaults import CONFIG_PATH, FOLDER_PATH |
| 9 | +from ..core.logger import logger |
| 10 | + |
| 11 | +F = Callable[..., Any] |
| 12 | +Wrapper = Callable[[F], F] |
| 13 | + |
| 14 | + |
| 15 | +def config_path_option(function: F) -> F: |
| 16 | + """Wrap a function to add configuration path option.""" |
| 17 | + wrapper: Wrapper = click.option( |
| 18 | + "-c", |
| 19 | + "--config", |
| 20 | + "config_path", |
| 21 | + metavar="FILE", |
| 22 | + default=CONFIG_PATH, |
| 23 | + type=click.Path(dir_okay=False, path_type=Path), |
| 24 | + help="Set path to configuration file.", |
| 25 | + show_default=True, |
| 26 | + ) |
| 27 | + return wrapper(function) |
| 28 | + |
| 29 | + |
| 30 | +def config_options(function: F) -> F: |
| 31 | + """Wrap a function to add configuration options.""" |
| 32 | + function = config_path_option(function) |
| 33 | + function = click.option( |
| 34 | + "-f", "--force", is_flag=True, help="Overwrite any existing configuration file." |
| 35 | + )(function) |
| 36 | + function = click.option( |
| 37 | + "-m", |
| 38 | + "--merge", |
| 39 | + is_flag=True, |
| 40 | + help="Merge any existing configuration file with the new configuration.", |
| 41 | + )(function) |
| 42 | + return function |
| 43 | + |
| 44 | + |
| 45 | +def verbosity_option(function: F) -> F: |
| 46 | + """Wrap a function to add verbosity option.""" |
| 47 | + |
| 48 | + def callback(ctx: Context, param: Parameter, value: str) -> None: |
| 49 | + if not value or ctx.resilient_parsing: |
| 50 | + return |
| 51 | + |
| 52 | + logger.setLevel(value) |
| 53 | + |
| 54 | + wrapper: Wrapper = click.option( |
| 55 | + "-v", |
| 56 | + "--verbosity", |
| 57 | + type=click.Choice( |
| 58 | + ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
| 59 | + case_sensitive=False, |
| 60 | + ), |
| 61 | + help="Verbosity of CLI output.", |
| 62 | + default=None, |
| 63 | + expose_value=False, |
| 64 | + envvar="MANIM_SLIDES_VERBOSITY", |
| 65 | + show_envvar=True, |
| 66 | + callback=callback, |
| 67 | + ) |
| 68 | + |
| 69 | + return wrapper(function) |
| 70 | + |
| 71 | + |
| 72 | +def folder_path_option(function: F) -> F: |
| 73 | + """Wrap a function to add folder path option.""" |
| 74 | + wrapper: Wrapper = click.option( |
| 75 | + "--folder", |
| 76 | + metavar="DIRECTORY", |
| 77 | + default=FOLDER_PATH, |
| 78 | + type=click.Path(exists=True, file_okay=False, path_type=Path), |
| 79 | + help="Set slides folder.", |
| 80 | + show_default=True, |
| 81 | + is_eager=True, # Needed to expose its value to other callbacks |
| 82 | + ) |
| 83 | + |
| 84 | + return wrapper(function) |
| 85 | + |
| 86 | + |
| 87 | +def scenes_argument(function: F) -> F: |
| 88 | + """ |
| 89 | + Wrap a function to add a scenes arguments. |
| 90 | +
|
| 91 | + This function assumes that :func:`folder_path_option` is also used |
| 92 | + on the same decorated function. |
| 93 | + """ |
| 94 | + |
| 95 | + def callback(ctx: Context, param: Parameter, value: tuple[str]) -> list[Path]: |
| 96 | + folder: Path = ctx.params.get("folder") |
| 97 | + |
| 98 | + presentation_config_paths = list_presentation_configs(folder) |
| 99 | + scene_names = [path.stem for path in presentation_config_paths] |
| 100 | + num_scenes = len(scene_names) |
| 101 | + num_digits = len(str(num_scenes)) |
| 102 | + |
| 103 | + if num_scenes == 0: |
| 104 | + raise click.UsageError( |
| 105 | + f"Folder {folder} does not contain " |
| 106 | + "any valid config file, did you render the animations first?" |
| 107 | + ) |
| 108 | + |
| 109 | + paths = [] |
| 110 | + |
| 111 | + if value: |
| 112 | + for scene_name in value: |
| 113 | + try: |
| 114 | + i = scene_names.index(scene_name) |
| 115 | + paths.append(presentation_config_paths[i]) |
| 116 | + except ValueError: |
| 117 | + raise click.UsageError( |
| 118 | + f"Could not find scene `{scene_name}` in: " |
| 119 | + + ", ".join(scene_names) |
| 120 | + + ". Did you make a typo or forgot to render the animations first?" |
| 121 | + ) from None |
| 122 | + else: |
| 123 | + click.echo( |
| 124 | + "Choose at least one or more scenes from " |
| 125 | + "(enter the corresponding number):\n" |
| 126 | + + "\n".join( |
| 127 | + f"- {i:{num_digits}d}: {name}" |
| 128 | + for i, name in enumerate(scene_names, start=1) |
| 129 | + ) |
| 130 | + ) |
| 131 | + continue_prompt = True |
| 132 | + while continue_prompt: |
| 133 | + index = click.prompt( |
| 134 | + "Please enter a value", type=click.IntRange(1, num_scenes) |
| 135 | + ) |
| 136 | + paths.append(presentation_config_paths[index - 1]) |
| 137 | + continue_prompt = click.confirm( |
| 138 | + "Do you want to enter an additional scene?" |
| 139 | + ) |
| 140 | + |
| 141 | + return paths |
| 142 | + |
| 143 | + wrapper: Wrapper = click.argument("scenes", nargs=-1, callback=callback) |
| 144 | + |
| 145 | + return wrapper(function) |
0 commit comments