6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-12 14:17:46 +00:00

fix: type DO_* filters and actions

This commit is contained in:
Régis Behmo 2022-11-24 18:20:23 +01:00
parent c06ac5b020
commit 478d44c299
2 changed files with 56 additions and 47 deletions

View File

@ -25,16 +25,6 @@ class DoGroup(click.Group):
do_group = click.group(cls=DoGroup, subcommand_metavar="JOB [ARGS]...")
def add_job_commands(do_command_group: click.Group) -> None:
"""
This is meant to be called with the `local/dev/k8s do` group commands, to add the
different `do` subcommands.
"""
subcommands: t.Iterator[click.Command] = hooks.Filters.CLI_DO_COMMANDS.iterate()
for subcommand in subcommands:
do_command_group.add_command(subcommand)
@hooks.Actions.CORE_READY.add()
def _add_core_init_tasks() -> None:
"""
@ -205,39 +195,14 @@ def assign_theme(name, domain):
return f'./manage.py lms shell -c "{python_command}"'
hooks.Filters.CLI_DO_COMMANDS.add_items(
[
createuser,
importdemocourse,
initialise,
settheme,
]
)
def do_callback(service_commands: t.Iterable[t.Tuple[str, str]]) -> None:
def add_job_commands(do_command_group: click.Group) -> None:
"""
This function must be added as a callback to all `do` subcommands.
`do` subcommands don't actually run any task. They just yield tuples of (service
name, unrendered script string). This function is responsible for actually running
the scripts. It does the following:
- Prefix the script with a base command
- Render the script string
- Run a job in the right container
In order to be added as a callback to the do subcommands, the
`_patch_do_commands_callbacks` must be called.
This is meant to be called with the `local/dev/k8s do` group commands, to add the
different `do` subcommands.
"""
context = click.get_current_context().obj
config = tutor_config.load(context.root)
runner = context.job_runner(config)
base_openedx_command = """
echo "Loading settings $DJANGO_SETTINGS_MODULE"
"""
for service, command in service_commands:
runner.run_task_from_str(service, base_openedx_command + command)
for subcommand in hooks.Filters.CLI_DO_COMMANDS.iterate():
assert isinstance(subcommand, click.Command)
do_command_group.add_command(subcommand)
@hooks.Actions.PLUGINS_LOADED.add()
@ -245,9 +210,15 @@ def _patch_do_commands_callbacks() -> None:
"""
After plugins have been loaded, patch `do` subcommands such that their output is
forwarded to `do_callback`.
This function is not called as part of add_job_commands because subcommands must be
patched just once.
"""
subcommands: t.Iterator[click.Command] = hooks.Filters.CLI_DO_COMMANDS.iterate()
for subcommand in subcommands:
for subcommand in hooks.Filters.CLI_DO_COMMANDS.iterate():
if not isinstance(subcommand, click.Command):
raise ValueError(
f"Command {subcommand} which was added to the CLI_DO_COMMANDS filter must be an instance of click.Command"
)
# Modify the subcommand callback such that job results are processed by do_callback
if subcommand.callback is None:
raise ValueError("Cannot patch None callback")
@ -274,3 +245,37 @@ def _patch_callback(
functools.update_wrapper(new_callback, func)
return new_callback
def do_callback(service_commands: t.Iterable[t.Tuple[str, str]]) -> None:
"""
This function must be added as a callback to all `do` subcommands.
`do` subcommands don't actually run any task. They just yield tuples of (service
name, unrendered script string). This function is responsible for actually running
the scripts. It does the following:
- Prefix the script with a base command
- Render the script string
- Run a job in the right container
This callback is added to the "do" subcommands by the `add_job_commands` function.
"""
context = click.get_current_context().obj
config = tutor_config.load(context.root)
runner = context.job_runner(config)
base_openedx_command = """
echo "Loading settings $DJANGO_SETTINGS_MODULE"
"""
for service, command in service_commands:
runner.run_task_from_str(service, base_openedx_command + command)
hooks.Filters.CLI_DO_COMMANDS.add_items(
[
createuser,
importdemocourse,
initialise,
settheme,
]
)

View File

@ -7,7 +7,7 @@ from __future__ import annotations
# The Tutor plugin system is licensed under the terms of the Apache 2.0 license.
__license__ = "Apache 2.0"
from typing import Any, Callable
from typing import Any, Callable, Iterable
import click
@ -64,7 +64,7 @@ class Actions:
#: :parameter: str job: job name.
#: :parameter: args: job positional arguments.
#: :parameter: kwargs: job named arguments.
DO_JOB = actions.get("do:job")
DO_JOB: Action[[str, Any]] = actions.get("do:job")
#: Called as soon as we have access to the Tutor project root.
#:
@ -135,7 +135,9 @@ class Filters:
#: added as subcommands to the `local/dev/k8s do` commands. They must return a list of
#: ("service name", "service command") tuples. Each "service command" will be executed
#: in the "service" container, both in local, dev and k8s mode.
CLI_DO_COMMANDS = filters.get("cli:commands:do")
CLI_DO_COMMANDS: Filter[
list[Callable[[Any], Iterable[tuple[str, str]]]], []
] = filters.get("cli:commands:do")
#: List of initialization tasks (scripts) to be run in the `init` job. This job
#: includes all database migrations, setting up, etc. To run some tasks before or
@ -144,7 +146,9 @@ class Filters:
#: :parameter list[tuple[str, str]] tasks: list of ``(service, task)`` tuples. Each
#: task is essentially a bash script to be run in the "service" container. Scripts
#: may contain Jinja markup, similar to templates.
CLI_DO_INIT_TASKS = filters.get("cli:commands:do:init")
CLI_DO_INIT_TASKS: Filter[list[tuple[str, str]], []] = filters.get(
"cli:commands:do:init"
)
#: DEPRECATED use :py:data:`CLI_DO_INIT_TASKS` instead.
#: