From 01635151b77d17df8dc2542ecf79395e68244479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 18 May 2022 09:32:03 +0200 Subject: [PATCH] Relax typer version constraint To achieve this, we vendor the cli docs generator. --- docs/gen_cli_doc.py | 99 +++++++++++++++++++++++++++++++++++++++++++++ news/30.misc | 1 + pyproject.toml | 3 +- tox.ini | 2 +- 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 docs/gen_cli_doc.py create mode 100644 news/30.misc diff --git a/docs/gen_cli_doc.py b/docs/gen_cli_doc.py new file mode 100644 index 0000000..43bcb86 --- /dev/null +++ b/docs/gen_cli_doc.py @@ -0,0 +1,99 @@ +"""Generate markdown for the manifestoo cli. + +Extracted from https://github.com/tiangolo/typer-cli, to avoid a dependency +on typer-cli which is not compatible with the latest typer release. + +Copyright (c) 2020 Sebastián Ramírez (MIT License). +""" +from pathlib import Path +from typing import cast + +import typer +from click import Command, Context, Group + +from manifestoo.main import app + + +def get_docs_for_click( + *, + obj: Command, + ctx: typer.Context, + indent: int = 0, + name: str = "", + call_prefix: str = "", +) -> str: + docs = "#" * (1 + indent) + command_name = name or obj.name + if call_prefix: + command_name = f"{call_prefix} {command_name}" + title = f"`{command_name}`" if command_name else "CLI" + docs += f" {title}\n\n" + if obj.help: + docs += f"{obj.help}\n\n" + usage_pieces = obj.collect_usage_pieces(ctx) + if usage_pieces: + docs += "**Usage**:\n\n" + docs += "```console\n" + docs += "$ " + if command_name: + docs += f"{command_name} " + docs += f"{' '.join(usage_pieces)}\n" + docs += "```\n\n" + args = [] + opts = [] + for param in obj.get_params(ctx): + rv = param.get_help_record(ctx) + if rv is not None: + if param.param_type_name == "argument": + args.append(rv) + elif param.param_type_name == "option": + opts.append(rv) + if args: + docs += "**Arguments**:\n\n" + for arg_name, arg_help in args: + docs += f"* `{arg_name}`" + if arg_help: + docs += f": {arg_help}" + docs += "\n" + docs += "\n" + if opts: + docs += "**Options**:\n\n" + for opt_name, opt_help in opts: + docs += f"* `{opt_name}`" + if opt_help: + docs += f": {opt_help}" + docs += "\n" + docs += "\n" + if obj.epilog: + docs += f"{obj.epilog}\n\n" + if isinstance(obj, Group): + group: Group = cast(Group, obj) + commands = group.list_commands(ctx) + if commands: + docs += "**Commands**:\n\n" + for command in commands: + command_obj = group.get_command(ctx, command) + assert command_obj + docs += f"* `{command_obj.name}`" + command_help = command_obj.get_short_help_str() + if command_help: + docs += f": {command_help}" + docs += "\n" + docs += "\n" + for command in commands: + command_obj = group.get_command(ctx, command) + assert command_obj + use_prefix = "" + if command_name: + use_prefix += f"{command_name}" + docs += get_docs_for_click( + obj=command_obj, ctx=ctx, indent=indent + 1, call_prefix=use_prefix + ) + return docs + + +if __name__ == "__main__": + command = typer.main.get_command(app) + ctx = Context(command) + doc = get_docs_for_click(obj=command, ctx=ctx, name="manifestoo").strip() + "\n" + Path(__file__).parent.joinpath("cli.md").write_text(doc) diff --git a/news/30.misc b/news/30.misc new file mode 100644 index 0000000..cc9c510 --- /dev/null +++ b/news/30.misc @@ -0,0 +1 @@ +Relax the typer dependency version constraint. diff --git a/pyproject.toml b/pyproject.toml index fac9efb..2ac8b90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ ] readme = "README.md" dependencies = [ - "typer[all] ~=0.3.2", + "typer[all] >= 0.3.2", "importlib_resources ; python_version<'3.7'", "importlib_metadata ; python_version<'3.8'", ] @@ -36,7 +36,6 @@ doc = [ "sphinx", "furo", "myst-parser", - "typer-cli", # to generate docs/cli.md "towncrier", "sphinxcontrib-towncrier", ] diff --git a/tox.ini b/tox.ini index b5141c1..b2fa027 100644 --- a/tox.ini +++ b/tox.ini @@ -48,7 +48,7 @@ commands = basepython = python3.9 extras = doc commands = - typer manifestoo.main utils docs --name manifestoo --output docs/cli.md + python docs/gen_cli_doc.py make -C docs html allowlist_externals = make