Better test support for Python Plugins (#1108)

* add plugins testing utils module

* add plugins manager fixture and init plugins tests helper in conftest

* add _custom_elements attribute to pyscript.Plugin to allow plugins to track the CE they register

* add test for py_tutor

* remove unrelated code from prims js script

* ensure a Plugin always has the app attribute and improve tests

* add tests for py_tutor create_code_section

* implement PluginsManager reset and add teardown on plugins_manager fixture to clean it up after a test

* add test to check if plugin has been registered

* add docstrings to new tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add docstrings to plugins tester

* add changes from main

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* lint

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add todo to add remaining PluginsManager lifecycle events

Co-authored-by: Fabio Pliger <fpliger@anaconda.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Fabio Pliger
2023-01-24 15:32:16 -06:00
committed by GitHub
parent 0de8cd9ab7
commit d55340a817
7 changed files with 305 additions and 6 deletions

View File

@@ -90,10 +90,6 @@ class PyTutor:
# Add the JS file
script = js.document.createElement("script")
script.type = "text/javascript"
try:
script.appendChild(js.document.createTextNode(PAGE_SCRIPT))
except BaseException:
script.text = PAGE_SCRIPT
script.src = "./assets/prism/prism.js"
js.document.head.appendChild(script)

View File

@@ -494,11 +494,27 @@ class Plugin:
name = self.__class__.__name__
self.name = name
self._custom_elements = []
self.app = None
def init(self, app):
self.app = app
def register_custom_element(self, tag):
"""
Decorator to register a new custom element as part of a Plugin and associate
tag to it. Internally, it delegates the registration to the PyScript internal
[JS] plugin manager, who actually creates the JS custom element that can be
attached to the page and instantiate an instance of the class passing the custom
element to the plugin constructor.
Exammple:
>> plugin = Plugin("PyTutorial")
>> @plugin.register_custom_element("py-tutor")
>> class PyTutor:
>> def __init__(self, element):
>> self.element = element
"""
# TODO: Ideally would be better to use the logger.
js.console.info(f"Defining new custom element {tag}")
@@ -507,6 +523,7 @@ class Plugin:
# until we have JS interface that works across interpreters
define_custom_element(tag, create_proxy(class_)) # noqa: F821
self._custom_elements.append(tag)
return create_proxy(wrapper)