mirror of
https://github.com/ChristianLight/tutor.git
synced 2024-09-27 11:49:02 +00:00
Compare commits
No commits in common. "60a5f25c9b9f301c05622d6cbc0f895221f3a9f2" and "2924b92ceab8aca550ec5ecae559a367fb1b713a" have entirely different histories.
60a5f25c9b
...
2924b92cea
@ -23,7 +23,6 @@ Functions
|
||||
---------
|
||||
|
||||
.. autofunction:: tutor.core.hooks::clear_all
|
||||
.. autofunction:: tutor.hooks::lru_cache
|
||||
|
||||
Priorities
|
||||
----------
|
||||
|
1
tests/openedx-lms-common-settings
Normal file
1
tests/openedx-lms-common-settings
Normal file
@ -0,0 +1 @@
|
||||
ORA2_FILEUPLOAD_BACKEND = "s3"
|
@ -9,7 +9,7 @@ from tutor.plugins import v0 as plugins_v0
|
||||
from tutor.types import Config, get_typed
|
||||
|
||||
|
||||
class PluginsV0Tests(PluginsTestCase):
|
||||
class PluginsTests(PluginsTestCase):
|
||||
def test_iter_installed(self) -> None:
|
||||
self.assertEqual([], list(plugins.iter_installed()))
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import typing as t
|
||||
|
||||
|
@ -2,35 +2,8 @@
|
||||
__license__ = "Apache 2.0"
|
||||
|
||||
import typing as t
|
||||
import functools
|
||||
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
# The imports that follow are the hooks API
|
||||
from tutor.core.hooks import clear_all, priorities
|
||||
from tutor.types import Config
|
||||
|
||||
from .catalog import Actions, Contexts, Filters
|
||||
|
||||
|
||||
def lru_cache(func: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]:
|
||||
"""
|
||||
LRU cache decorator similar to `functools.lru_cache
|
||||
<https://docs.python.org/3/library/functools.html#functools.lru_cache>`__ that is
|
||||
automatically cleared whenever plugins are updated.
|
||||
|
||||
Use this to decorate functions that need to be called multiple times with a return
|
||||
value that depends on which plugins are loaded. Typically: functions that depend on
|
||||
the output of filters.
|
||||
"""
|
||||
decorated = functools.lru_cache(func)
|
||||
|
||||
@Actions.PLUGIN_LOADED.add()
|
||||
def _clear_func_cache_on_load(_plugin: str) -> None:
|
||||
decorated.cache_clear()
|
||||
|
||||
@Actions.PLUGIN_UNLOADED.add()
|
||||
def _clear_func_cache_on_unload(_plugin: str, _root: str, _config: Config) -> None:
|
||||
decorated.cache_clear()
|
||||
|
||||
return decorated
|
||||
|
@ -3,7 +3,6 @@ Provide API for plugin features.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import typing as t
|
||||
from copy import deepcopy
|
||||
|
||||
@ -13,6 +12,38 @@ from tutor.types import Config, get_typed
|
||||
# Import modules to trigger hook creation
|
||||
from . import openedx, v0, v1
|
||||
|
||||
# Cache of plugin patches, for efficiency
|
||||
ENV_PATCHES_DICT: dict[str, list[str]] = {}
|
||||
|
||||
|
||||
@hooks.Actions.PLUGINS_LOADED.add()
|
||||
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.
|
||||
"""
|
||||
ENV_PATCHES_DICT.clear()
|
||||
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)
|
||||
|
||||
|
||||
def is_installed(name: str) -> bool:
|
||||
"""
|
||||
@ -96,19 +127,7 @@ def iter_patches(name: str) -> t.Iterator[str]:
|
||||
"""
|
||||
Yields: patch (str)
|
||||
"""
|
||||
yield from _env_patches().get(name, [])
|
||||
|
||||
|
||||
@hooks.lru_cache
|
||||
def _env_patches() -> dict[str, list[str]]:
|
||||
"""
|
||||
Dictionary of patches, implemented for performance reasons.
|
||||
"""
|
||||
patches: dict[str, list[str]] = {}
|
||||
for name, content in hooks.Filters.ENV_PATCHES.iterate():
|
||||
patches.setdefault(name, [])
|
||||
patches[name].append(content)
|
||||
return patches
|
||||
yield from ENV_PATCHES_DICT.get(name, [])
|
||||
|
||||
|
||||
def unload(plugin: str) -> None:
|
||||
|
@ -4,7 +4,8 @@ import os
|
||||
import re
|
||||
import typing as t
|
||||
|
||||
from tutor import bindmount, hooks
|
||||
from tutor import bindmount
|
||||
from tutor import hooks
|
||||
from tutor.__about__ import __version_suffix__
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user