2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Provide API for plugin features.
|
|
|
|
"""
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
from __future__ import annotations
|
2023-01-06 18:02:17 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
import typing as t
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from tutor import exceptions, fmt, hooks
|
|
|
|
from tutor.types import Config, get_typed
|
|
|
|
|
|
|
|
# Import modules to trigger hook creation
|
2023-09-04 17:08:15 +00:00
|
|
|
from . import openedx, v0, v1
|
2022-02-07 17:11:43 +00:00
|
|
|
|
2023-04-28 15:11:14 +00:00
|
|
|
# Cache of plugin patches, for efficiency
|
|
|
|
ENV_PATCHES_DICT: dict[str, list[str]] = {}
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
@hooks.Actions.PLUGINS_LOADED.add()
|
2023-12-08 10:55:55 +00:00
|
|
|
def _fill_patch_cache_on_load() -> None:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
This action is run after plugins have been loaded.
|
|
|
|
"""
|
2023-12-08 10:55:55 +00:00
|
|
|
_fill_patches_cache()
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
2023-11-06 16:35:01 +00:00
|
|
|
@hooks.Actions.PLUGIN_UNLOADED.add()
|
2023-12-08 10:55:55 +00:00
|
|
|
def _fill_patch_cache_on_unload(plugin: str, root: str, _config: Config) -> None:
|
2023-11-06 16:35:01 +00:00
|
|
|
"""
|
|
|
|
This action is run after plugins have been unloaded.
|
|
|
|
"""
|
2023-12-08 10:55:55 +00:00
|
|
|
_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.
|
|
|
|
"""
|
2023-11-06 16:35:01 +00:00
|
|
|
ENV_PATCHES_DICT.clear()
|
2023-12-08 10:55:55 +00:00
|
|
|
patches: t.Iterable[tuple[str, str]] = hooks.Filters.ENV_PATCHES.iterate()
|
|
|
|
for name, content in patches:
|
|
|
|
ENV_PATCHES_DICT.setdefault(name, [])
|
|
|
|
ENV_PATCHES_DICT[name].append(content)
|
2023-11-06 16:35:01 +00:00
|
|
|
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
def is_installed(name: str) -> bool:
|
|
|
|
"""
|
|
|
|
Return true if the plugin is installed.
|
|
|
|
"""
|
|
|
|
return name in iter_installed()
|
|
|
|
|
|
|
|
|
|
|
|
def iter_installed() -> t.Iterator[str]:
|
|
|
|
"""
|
|
|
|
Iterate on all installed plugins, sorted by name.
|
|
|
|
|
|
|
|
This will yield all plugins, including those that have the same name.
|
|
|
|
|
|
|
|
The CORE_READY action must have been triggered prior to calling this function,
|
|
|
|
otherwise no installed plugin will be detected.
|
|
|
|
"""
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
yield from sorted(hooks.Filters.PLUGINS_INSTALLED.iterate())
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
def iter_info() -> t.Iterator[tuple[str, t.Optional[str]]]:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Iterate on the information of all installed plugins.
|
|
|
|
|
|
|
|
Yields (<plugin name>, <info>) tuples.
|
|
|
|
"""
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
def plugin_info_name(info: tuple[str, t.Optional[str]]) -> str:
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
return info[0]
|
|
|
|
|
|
|
|
yield from sorted(hooks.Filters.PLUGINS_INFO.iterate(), key=plugin_info_name)
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_loaded(name: str) -> bool:
|
|
|
|
return name in iter_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
def load_all(names: t.Iterable[str]) -> None:
|
|
|
|
"""
|
|
|
|
Load all plugins one by one.
|
|
|
|
|
|
|
|
Plugins are loaded in alphabetical order. We ignore plugins which failed to load.
|
|
|
|
After all plugins have been loaded, the PLUGINS_LOADED action is triggered.
|
|
|
|
"""
|
|
|
|
names = sorted(set(names))
|
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
load(name)
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
except Exception as e: # pylint: disable=broad-except
|
2022-05-24 09:09:54 +00:00
|
|
|
fmt.echo_alert(f"Failed to enable plugin '{name}': {e}")
|
2022-02-07 17:11:43 +00:00
|
|
|
hooks.Actions.PLUGINS_LOADED.do()
|
|
|
|
|
|
|
|
|
|
|
|
def load(name: str) -> None:
|
|
|
|
"""
|
|
|
|
Load a given plugin, thus declaring all its hooks.
|
|
|
|
|
|
|
|
Loading a plugin is done within a context, such that we can remove all hooks when a
|
|
|
|
plugin is disabled, or during unit tests.
|
|
|
|
"""
|
|
|
|
if not is_installed(name):
|
|
|
|
raise exceptions.TutorError(f"plugin '{name}' is not installed.")
|
|
|
|
with hooks.Contexts.PLUGINS.enter():
|
2023-04-28 15:11:14 +00:00
|
|
|
with hooks.Contexts.app(name).enter():
|
|
|
|
hooks.Actions.PLUGIN_LOADED.do(name)
|
2022-02-07 17:11:43 +00:00
|
|
|
hooks.Filters.PLUGINS_LOADED.add_item(name)
|
|
|
|
|
|
|
|
|
|
|
|
def iter_loaded() -> t.Iterator[str]:
|
|
|
|
"""
|
|
|
|
Iterate on the list of loaded plugin names, sorted in alphabetical order.
|
|
|
|
|
|
|
|
Note that loaded plugin names are deduplicated. Thus, if two plugins have
|
|
|
|
the same name, just one name will be displayed.
|
|
|
|
"""
|
|
|
|
plugins: t.Iterable[str] = hooks.Filters.PLUGINS_LOADED.iterate()
|
|
|
|
yield from sorted(set(plugins))
|
|
|
|
|
|
|
|
|
|
|
|
def iter_patches(name: str) -> t.Iterator[str]:
|
|
|
|
"""
|
|
|
|
Yields: patch (str)
|
|
|
|
"""
|
2023-04-28 15:11:14 +00:00
|
|
|
yield from ENV_PATCHES_DICT.get(name, [])
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unload(plugin: str) -> None:
|
|
|
|
"""
|
|
|
|
Remove all filters and actions associated to a given plugin.
|
|
|
|
"""
|
2023-04-28 15:11:14 +00:00
|
|
|
hooks.clear_all(context=hooks.Contexts.app(plugin).name)
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
2023-12-08 10:55:55 +00:00
|
|
|
@hooks.Actions.PLUGIN_UNLOADED.add(priority=hooks.priorities.HIGH)
|
2022-02-07 17:11:43 +00:00
|
|
|
def _unload_on_disable(plugin: str, _root: str, _config: Config) -> None:
|
|
|
|
unload(plugin)
|