6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2025-01-08 08:08:20 +00:00

fix: type tests

Type tests were broken following the upgrade of click. We take the
opportunity to simplify the TutorCli implementation.
This commit is contained in:
Régis Behmo 2023-08-29 16:21:43 +02:00
parent 7e443ac40d
commit 39d3eaf801

View File

@ -32,7 +32,7 @@ def main() -> None:
sys.exit(1) sys.exit(1)
class TutorCli(click.MultiCommand): class TutorCli(click.Group):
""" """
Dynamically load subcommands at runtime. Dynamically load subcommands at runtime.
@ -43,26 +43,14 @@ class TutorCli(click.MultiCommand):
IS_ROOT_READY = False IS_ROOT_READY = False
@classmethod def get_command(
def iter_commands(cls, ctx: click.Context) -> t.Iterator[click.Command]: self, ctx: click.Context, cmd_name: str
) -> t.Optional[click.Command]:
""" """
Return the list of subcommands (click.Command). This is run when passing a command from the CLI. E.g: tutor config ...
""" """
cls.ensure_plugins_enabled(ctx) self.ensure_plugins_enabled(ctx)
yield from hooks.Filters.CLI_COMMANDS.iterate() return super().get_command(ctx, cmd_name=cmd_name)
@classmethod
def ensure_plugins_enabled(cls, ctx: click.Context) -> None:
"""
We enable plugins as soon as possible to have access to commands.
"""
if not "root" in ctx.params:
# When generating docs, this function is called with empty args.
# That's ok, we just ignore it.
return
if not cls.IS_ROOT_READY:
hooks.Actions.PROJECT_ROOT_READY.do(ctx.params["root"])
cls.IS_ROOT_READY = True
def list_commands(self, ctx: click.Context) -> list[str]: def list_commands(self, ctx: click.Context) -> list[str]:
""" """
@ -70,20 +58,22 @@ class TutorCli(click.MultiCommand):
- shell autocompletion: tutor <tab> - shell autocompletion: tutor <tab>
- print help: tutor, tutor -h - print help: tutor, tutor -h
""" """
return sorted( self.ensure_plugins_enabled(ctx)
[command.name or "<undefined>" for command in self.iter_commands(ctx)] return super().list_commands(ctx)
)
def get_command( def ensure_plugins_enabled(self, ctx: click.Context) -> None:
self, ctx: click.Context, cmd_name: str
) -> t.Optional[click.Command]:
""" """
This is run when passing a command from the CLI. E.g: tutor config ... We enable plugins as soon as possible to have access to commands.
""" """
for command in self.iter_commands(ctx): if not "root" in ctx.params:
if cmd_name == command.name: # When generating docs, this function is called with empty args.
return command # That's ok, we just ignore it.
return None return
if not self.IS_ROOT_READY:
hooks.Actions.PROJECT_ROOT_READY.do(ctx.params["root"])
self.IS_ROOT_READY = True
for cmd in hooks.Filters.CLI_COMMANDS.iterate():
self.add_command(cmd)
@click.group( @click.group(