-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to plugins.v1 with filters & actions
This is a very large refactoring which aims at making Tutor both more extendable and more generic. Historically, the Tutor plugin system was designed as an ad-hoc solution to allow developers to modify their own Open edX platforms without having to fork Tutor. The plugin API was simple, but limited, because of its ad-hoc nature. As a consequence, there were many things that plugin developers could not do, such as extending different parts of the CLI or adding custom template filters. Here, we refactor the whole codebase to make use of a generic plugin system. This system was inspired by the Wordpress plugin API and the Open edX "hooks and filters" API. The various components are added to a small core thanks to a set of actions and filters. Actions are callback functions that can be triggered at different points of the application lifecycle. Filters are functions that modify some data. Both actions and filters are collectively named as "hooks". Hooks can optionally be created within a certain context, which makes it easier to keep track of which application created which callback. This new hooks system allows us to provide a Python API that developers can use to extend their applications. The API reference is added to the documentation, along with a new plugin development tutorial. The plugin v0 API remains supported for backward compatibility of existing plugins. Done: - Do not load commands from plugins which are not enabled. - Load enabled plugins once on start. - Implement contexts for actions and filters, which allow us to keep track of the source of every hook. - Migrate patches - Migrate commands - Migrate plugin detection - Migrate templates_root - Migrate config - Migrate template environment globals and filters - Migrate hooks to tasks - Generate hook documentation - Generate patch reference documentation - Add the concept of action priority Close #499.
- Loading branch information
Showing
70 changed files
with
3,585 additions
and
1,538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
#!/usr/bin/env python3 | ||
from tutor.plugins import OfficialPlugin | ||
from tutor import hooks | ||
from tutor.commands.cli import main | ||
from tutor.plugins.v0 import OfficialPlugin | ||
|
||
|
||
@hooks.Actions.CORE_READY.add() | ||
def _discover_official_plugins() -> None: | ||
# Manually discover plugins: that's because entrypoint plugins are not properly | ||
# detected within the binary bundle. | ||
with hooks.Contexts.PLUGINS.enter(): | ||
OfficialPlugin.discover_all() | ||
|
||
# Manually install plugins (this is for creating the bundle) | ||
for plugin_name in [ | ||
"android", | ||
"discovery", | ||
"ecommerce", | ||
"forum", | ||
"license", | ||
"mfe", | ||
"minio", | ||
"notes", | ||
"richie", | ||
"webui", | ||
"xqueue", | ||
]: | ||
try: | ||
OfficialPlugin.load(plugin_name) | ||
except ImportError: | ||
pass | ||
|
||
if __name__ == "__main__": | ||
# Call the regular main function, which will not detect any entrypoint plugin | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
This module is heavily inspired by Django's djangodocs.py: | ||
https://github.com/django/django/blob/main/docs/_ext/djangodocs.py | ||
""" | ||
from sphinx.application import Sphinx | ||
|
||
|
||
def setup(app: Sphinx) -> None: | ||
# https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_crossref_type | ||
app.add_crossref_type( | ||
directivename="patch", | ||
rolename="patch", | ||
indextemplate="pair: %s; patch", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
docs/plugins/gettingstarted.rst → docs/plugins/v0/gettingstarted.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. warning:: The v0 plugin API is no longer the recommended way of developing new plugins for Tutor, starting from Tutor v13.2.0. See our :ref:`plugin creation tutorial <plugin_development_tutorial>` to learn more about the v1alpha plugin API. Existing v0 plugins will remain supported for some time but developers are encouraged to start migrating their plugins as soon as possible to make use of the new API. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. _actions: | ||
|
||
======= | ||
Actions | ||
======= | ||
|
||
Actions are one of the two types of hooks (the other being :ref:`filters`) that can be used to extend Tutor. Each action represents an event that can occur during the application life cycle. Each action has a name, and callback functions can be attached to it. When an action is triggered, these callback functions are called in sequence. Each callback function can trigger side effects, independently from one another. | ||
|
||
.. autofunction:: tutor.hooks.actions::get | ||
.. autofunction:: tutor.hooks.actions::get_template | ||
.. autofunction:: tutor.hooks.actions::add | ||
.. autofunction:: tutor.hooks.actions::do | ||
.. autofunction:: tutor.hooks.actions::clear | ||
.. autofunction:: tutor.hooks.actions::clear_all | ||
|
||
.. autoclass:: tutor.hooks.actions.Action | ||
.. autoclass:: tutor.hooks.actions.ActionTemplate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
========= | ||
Constants | ||
========= | ||
|
||
Here we lists named :ref:`actions`, :ref:`filters` and :ref:`contexts` that are used across Tutor. These are simply hook variables that we can refer to across the Tutor codebase without having to hard-code string names. The API is slightly different and less verbose than "native" hooks. | ||
|
||
Actions | ||
======= | ||
|
||
.. autoclass:: tutor.hooks.Actions | ||
:members: | ||
|
||
Filters | ||
======= | ||
|
||
.. autoclass:: tutor.hooks.Filters | ||
:members: | ||
|
||
Contexts | ||
======== | ||
|
||
.. autoclass:: tutor.hooks.Contexts | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. _contexts: | ||
|
||
======== | ||
Contexts | ||
======== | ||
|
||
Contexts are a feature of the hook-based extension system in Tutor, which allows us to keep track of which components of the code created which callbacks. Contexts are very much an internal concept that most plugin developers should not have to worry about. | ||
|
||
.. autofunction:: tutor.hooks.contexts::enter | ||
|
||
.. autoclass:: tutor.hooks.contexts.Context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.. _filters: | ||
|
||
======= | ||
Filters | ||
======= | ||
|
||
Filters are one of the two types of hooks (the other being :ref:`actions`) that can be used to extend Tutor. Filters allow one to modify the application behavior by transforming data. Each filter has a name, and callback functions can be attached to it. When a filter is applied, these callback functions are called in sequence; the result of each callback function is passed as the first argument to the next callback function. The result of the final callback function is returned to the application as the filter's output. | ||
|
||
.. autofunction:: tutor.hooks.filters::get | ||
.. autofunction:: tutor.hooks.filters::get_template | ||
.. autofunction:: tutor.hooks.filters::add | ||
.. autofunction:: tutor.hooks.filters::add_item | ||
.. autofunction:: tutor.hooks.filters::add_items | ||
.. autofunction:: tutor.hooks.filters::apply | ||
.. autofunction:: tutor.hooks.filters::iterate | ||
.. autofunction:: tutor.hooks.filters::clear | ||
.. autofunction:: tutor.hooks.filters::clear_all | ||
|
||
.. autoclass:: tutor.hooks.filters.Filter | ||
.. autoclass:: tutor.hooks.filters.FilterTemplate |
Oops, something went wrong.