From 6582e3a9e016561c2fa5978c47cf342a81ff8e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Fri, 8 Dec 2023 11:55:55 +0100 Subject: [PATCH] fix: fill patch catch on plugin load/unload Also, update docs on `tutor config save`. Note that we had to fix an issue where the plugin unload callback was being called too late. --- docs/plugins/intro.rst | 4 ---- docs/plugins/v0/gettingstarted.rst | 4 ---- docs/tutorials/plugin.rst | 4 ++-- tutor/config.py | 6 ++++-- tutor/plugins/__init__.py | 32 ++++++++++++++++-------------- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/docs/plugins/intro.rst b/docs/plugins/intro.rst index cb75f63..57fd8d4 100644 --- a/docs/plugins/intro.rst +++ b/docs/plugins/intro.rst @@ -29,10 +29,6 @@ Enable/disable a plugin:: tutor plugins enable myplugin tutor plugins disable myplugin -After enabling or disabling a plugin, the environment should be re-generated with:: - - tutor config save - The full plugins CLI is described in the :ref:`reference documentation `. .. _existing_plugins: diff --git a/docs/plugins/v0/gettingstarted.rst b/docs/plugins/v0/gettingstarted.rst index 8fdaaf4..1f597e1 100644 --- a/docs/plugins/v0/gettingstarted.rst +++ b/docs/plugins/v0/gettingstarted.rst @@ -41,10 +41,6 @@ You can then enable your newly-created plugin:: tutor plugins enable googleanalytics -Update your environment to apply changes from your plugin:: - - tutor config save - You should be able to view your changes in every LMS and CMS settings file:: grep -r googleanalytics "$(tutor config printroot)/env/apps/openedx/settings/" diff --git a/docs/tutorials/plugin.rst b/docs/tutorials/plugin.rst index 55d91bd..6469f49 100644 --- a/docs/tutorials/plugin.rst +++ b/docs/tutorials/plugin.rst @@ -38,9 +38,9 @@ Our plugin is disabled, for now. To enable it, we run:: $ tutor plugins enable myplugin Plugin myplugin enabled Configuration saved to /home/yourusername/.local/share/tutor/config.yml - You should now re-generate your environment with `tutor config save`. + Environment generated in /home/yourusername/.local/share/tutor/env -At this point you could re-generate your environment with ``tutor config save``, but there would not be any change to your environment... because the plugin does not do anything. So let's get started and make some changes. +At this point your environment was updated, but there would not be any change there... because the plugin does not do anything. So let's get started and make some changes. Modifying existing files with patches ------------------------------------- diff --git a/tutor/config.py b/tutor/config.py index 35e35c1..35ee801 100644 --- a/tutor/config.py +++ b/tutor/config.py @@ -328,7 +328,9 @@ def _enable_plugins(root: str) -> None: enable_plugins(config) -@hooks.Actions.PLUGIN_UNLOADED.add() +# This is run with a very high priority such that it is called before the plugin hooks +# are actually cleared. +@hooks.Actions.PLUGIN_UNLOADED.add(priority=hooks.priorities.HIGH - 1) def _remove_plugin_config_overrides_on_unload( plugin: str, _root: str, config: Config ) -> None: @@ -342,7 +344,7 @@ def _remove_plugin_config_overrides_on_unload( fmt.echo_info(f" config - removing entry: {key}={value}") -@hooks.Actions.PLUGIN_UNLOADED.add(priority=100) +@hooks.Actions.PLUGIN_UNLOADED.add(priority=hooks.priorities.LOW) def _update_enabled_plugins_on_unload(_plugin: str, _root: str, config: Config) -> None: """ Update the list of enabled plugins. diff --git a/tutor/plugins/__init__.py b/tutor/plugins/__init__.py index f96afab..d9dcc21 100644 --- a/tutor/plugins/__init__.py +++ b/tutor/plugins/__init__.py @@ -17,13 +17,26 @@ ENV_PATCHES_DICT: dict[str, list[str]] = {} @hooks.Actions.PLUGINS_LOADED.add() -def _convert_plugin_patches() -> None: +def _fill_patch_cache_on_load() -> None: + """ + This action is run after plugins have been loaded. + """ + _fill_patches_cache() + + +@hooks.Actions.PLUGIN_UNLOADED.add() +def _fill_patch_cache_on_unload(plugin: str, root: str, _config: Config) -> None: + """ + This action is run after plugins have been unloaded. + """ + _fill_patches_cache() + + +def _fill_patches_cache() -> None: """ Some patches are added as (name, content) tuples with the ENV_PATCHES filter. We convert these patches to add them to ENV_PATCHES_DICT. This makes it easier for end-user to declare patches, and it's more performant. - - This action is run after plugins have been loaded. """ ENV_PATCHES_DICT.clear() patches: t.Iterable[tuple[str, str]] = hooks.Filters.ENV_PATCHES.iterate() @@ -32,17 +45,6 @@ def _convert_plugin_patches() -> None: ENV_PATCHES_DICT[name].append(content) -@hooks.Actions.PLUGIN_UNLOADED.add() -def _clear_plugin_patches(plugin: str, root: str, _config: Config) -> None: - """ - After disabling a plugin, ENV_PATCHES_DICT should be cleared - and re-calculated. - - This action is run after plugins have been unloaded. - """ - ENV_PATCHES_DICT.clear() - - def is_installed(name: str) -> bool: """ Return true if the plugin is installed. @@ -135,6 +137,6 @@ def unload(plugin: str) -> None: hooks.clear_all(context=hooks.Contexts.app(plugin).name) -@hooks.Actions.PLUGIN_UNLOADED.add(priority=50) +@hooks.Actions.PLUGIN_UNLOADED.add(priority=hooks.priorities.HIGH) def _unload_on_disable(plugin: str, _root: str, _config: Config) -> None: unload(plugin)