From a9297d5051e8fd55677cb2e288aa42a0e0ed51bc Mon Sep 17 00:00:00 2001 From: Rikard Gillemyr Date: Sun, 7 Apr 2024 21:38:32 +0200 Subject: [PATCH] more template work --- src/bygg/cmd/dispatcher.py | 17 +++++++ src/bygg/cmd/templates.py | 47 +++++++++++++++++++ src/bygg/templates/standard/description.txt | 1 + src/bygg/templates/taskrunner/description.txt | 1 + 4 files changed, 66 insertions(+) create mode 100644 src/bygg/cmd/templates.py create mode 100644 src/bygg/templates/standard/description.txt create mode 100644 src/bygg/templates/taskrunner/description.txt diff --git a/src/bygg/cmd/dispatcher.py b/src/bygg/cmd/dispatcher.py index aeeb758..c78b4e1 100644 --- a/src/bygg/cmd/dispatcher.py +++ b/src/bygg/cmd/dispatcher.py @@ -26,6 +26,7 @@ dump_schema, read_config_file, ) +from bygg.cmd.templates import complete_templates, describe_templates from bygg.cmd.tree import display_tree from bygg.core.runner import ProcessRunner from bygg.core.scheduler import Scheduler @@ -290,6 +291,10 @@ def dispatcher(): print_version() sys.exit(0) + if args.describe_templates: + describe_templates() + sys.exit(0) + directory = args.directory[0] if args.directory else None is_restarted_with_env = ( args.is_restarted_with_env[0] if args.is_restarted_with_env else None @@ -599,5 +604,17 @@ def create_argument_parser(): action="store_true", help="Output instructions for how to set up shell completions via the shell's startup script.", ) + meta_group.add_argument( + "--describe-templates", + action="store_true", + help="Describe templates.", + ) + meta_group.add_argument( + "--init-template", + nargs=1, + type=str, + choices=complete_templates(), + help="Create init files from a template.", + ) return parser diff --git a/src/bygg/cmd/templates.py b/src/bygg/cmd/templates.py new file mode 100644 index 0000000..1feef79 --- /dev/null +++ b/src/bygg/cmd/templates.py @@ -0,0 +1,47 @@ +from pathlib import Path +import shutil +import textwrap + +from bygg.output.output import ( + TerminalStyle as TS, +) + +TEMPLATE_PATH = Path(__file__).parent.parent / "templates" + + +def complete_templates() -> list[str]: + return [x.name for x in list_template_dirs()] + + +def describe_templates(): + terminal_cols, terminal_rows = shutil.get_terminal_size() + output = [f"{TS.BOLD}Available templates:{TS.RESET}"] + + for name, description in [ + create_template_description(template) for template in list_template_dirs() + ]: + output.append(f"{TS.BOLD}{name}{TS.RESET}") + for paragraph in description.split("\n\n"): + output.append( + textwrap.fill( + paragraph.strip(), + initial_indent=" ", + subsequent_indent=" ", + width=min(80, terminal_cols), + ) + ) + + print("\n\n".join(output)) + + +def create_template_description(path: Path) -> tuple[str, str]: + description_file = path / "description.txt" + try: + description = description_file.read_text("utf-8").strip() + except FileNotFoundError: + description = "No description provided" + return path.name, description + + +def list_template_dirs(): + return (x for x in TEMPLATE_PATH.iterdir() if x.is_dir()) diff --git a/src/bygg/templates/standard/description.txt b/src/bygg/templates/standard/description.txt new file mode 100644 index 0000000..aa1a205 --- /dev/null +++ b/src/bygg/templates/standard/description.txt @@ -0,0 +1 @@ +Setup with a YAML file and Python files. \ No newline at end of file diff --git a/src/bygg/templates/taskrunner/description.txt b/src/bygg/templates/taskrunner/description.txt new file mode 100644 index 0000000..26643c1 --- /dev/null +++ b/src/bygg/templates/taskrunner/description.txt @@ -0,0 +1 @@ +Only a YAML file for running tasks. \ No newline at end of file