fix: enable/disable/enable plugin

This fix is possible thanks to a change in tutor core.
This commit is contained in:
Régis Behmo 2024-12-20 10:59:07 +01:00
parent a182556872
commit 7982ba96db
3 changed files with 17 additions and 44 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import click
from tutor import hooks
from tutor.commands.context import Context
from .__about__ import __version__
from .server import app
@ -31,12 +30,11 @@ def dash() -> None:
"--dev/--no-dev",
help="Enable development mode, with auto-reload and debug templates.",
)
@click.pass_obj
def dash_run(obj: Context, host: str, port: int, dev: bool) -> None:
def dash_run(host: str, port: int, dev: bool) -> None:
"""
Run the dash server.
"""
app.run(obj.root, host=host, port=port, debug=dev, use_reloader=dev)
app.run(host=host, port=port, debug=dev, use_reloader=dev)
hooks.Filters.CLI_COMMANDS.add_item(dash)

View File

@ -25,12 +25,10 @@ app = Quart(
)
def run(root: str, **app_kwargs: t.Any) -> None:
def run(**app_kwargs: t.Any) -> None:
"""
Bootstrap the Quart app and run it.
"""
tutorclient.Project.connect(root)
# TODO app.run() should be called only in development
app.run(**app_kwargs)

View File

@ -22,35 +22,6 @@ from . import constants
logger = logging.getLogger(__name__)
class Project:
"""
Provide access to the current Tutor project root and configuration.
"""
CONFIG: dict[str, t.Any] = {} # TODO this attribute is unused. Delete it?
ROOT: str = ""
@classmethod
def connect(cls, root: str) -> None:
"""
Call whenever we are ready to connect to the Tutor hooks API.
"""
if not cls.ROOT:
# Hook up TutorProject with Tutor hooks -- just once
hooks.Actions.PROJECT_ROOT_READY.add()(cls._dash_on_project_root_ready)
hooks.Actions.CONFIG_LOADED.add()(cls._dash_on_config_loaded)
hooks.Actions.CORE_READY.do() # discover plugins
hooks.Actions.PROJECT_ROOT_READY.do(root)
@classmethod
def _dash_on_project_root_ready(cls, root: str) -> None:
cls.ROOT = root
@classmethod
def _dash_on_config_loaded(cls, config: Config) -> None:
cls.CONFIG = config
class Cli:
"""
Run Tutor commands and capture the output in a file.
@ -275,6 +246,16 @@ class CliPool:
class Client:
# Full user config, updated whenever it's loaded in tutor.
CONFIG: Config = {}
@classmethod
def update_config(cls, config: Config) -> None:
"""
Store config on load.
"""
cls.CONFIG = config
@classmethod
def installed_plugins(cls) -> list[str]:
return sorted(set(hooks.Filters.PLUGINS_INSTALLED.iterate()))
@ -288,14 +269,7 @@ class Client:
plugin_config = hooks.Filters.CONFIG_UNIQUE.iterate_from_context(
hooks.Contexts.app(name).name
)
# TODO IMPORTANT enable/disable/enable does not work because of the Python
# module import cache. After a plugin module is imported, the plugin is
# disabled, and when we try to enable it again the module is not imported,
# because of the import cache.
user_config = {
key: Project.CONFIG.get(key, value) for key, value in plugin_config
}
return user_config
return {key: cls.CONFIG.get(key, value) for key, value in plugin_config}
@classmethod
def plugin_config_defaults(cls, name: str) -> Config:
@ -303,3 +277,6 @@ class Client:
hooks.Contexts.app(name).name
)
return dict(config)
hooks.Actions.CONFIG_LOADED.add()(Client.update_config)