mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-21 11:01:26 -05:00
Python Plugins (#961)
* add test and example files * update config to include python plugins in build * add markdown plugin * remove full pyscript execution from pyodide * move loading of pyscript.py from pyodide loagInterpreter to main setupVirtualEnv and add function to create python CE plugins * add plugin class to pyscript.py * add missing import * fix plugin path * add fetchPythonPlugins to PyScriptApp * remove old comments * fix test * add support for python plugins beyond custom elements and add app to python namespace in main * inject reference to PyScript app onto python plugins * add example hook onto markdown plugin * change plugin events logs * remove unused PyPlugin * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix type import * add docstring to fetchPythonPlugins * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * rename addPythonPlugin method * address PR comment * call python plugins on hooks after the interpreted is ready * add test for event hooks and split the test in 2 separate plugins to isolte type of plugins tests * change python plugins initialization and registration, to inject the app from app itself instead of on the plugins themselves * handle case when plugin cannot load due to missing plugin attribute * add test for fail scenario when a plugin module does not have a plugin attribute * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add deprecation warning for pyscript objects loaded in global namespace * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove all from global scope * remove create_custom_element from global scope * rename create_custom_element to define_custom_element * rename attributes in define_custom_element and add docstrings * better handle connect event output * add warning to py_markdown plugin * remove debugging logs * improve tests * remove debugging log * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove unused import * add executable shebang * add pyodide mock module * fmt and lint * Update to pyodide.ffi.create_proxy per pyodide v21 api change * Mock pyodide as package instead of mdoule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add __init__ to pyodide package * Update pyscriptjs/src/plugin.ts fix logger name Co-authored-by: Antonio Cuni <anto.cuni@gmail.com> * fix pyodide import but handling the diff in their API change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * oops, conflict resolution blooper * Fix failing integration tests Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jeff Glass <glass.jeffrey@gmail.com> Co-authored-by: Antonio Cuni <anto.cuni@gmail.com> Co-authored-by: FabioRosado <fabiorosado@outlook.com>
This commit is contained in:
184
pyscriptjs/tests/integration/test_plugins.py
Normal file
184
pyscriptjs/tests/integration/test_plugins.py
Normal file
@@ -0,0 +1,184 @@
|
||||
from .support import PyScriptTest
|
||||
|
||||
# Source code of a simple plugin that creates a Custom Element for testing purposes
|
||||
CE_PLUGIN_CODE = """
|
||||
from pyscript import Plugin, console
|
||||
|
||||
plugin = Plugin('py-upper')
|
||||
|
||||
console.log("py_upper Plugin loaded")
|
||||
|
||||
@plugin.register_custom_element('py-up')
|
||||
class Upper:
|
||||
def __init__(self, element):
|
||||
self.element = element
|
||||
|
||||
def connect(self):
|
||||
console.log("Upper plugin connected")
|
||||
return self.element.originalInnerHTML.upper()
|
||||
"""
|
||||
|
||||
# Source of a plugin hooks into the PyScript App lifecycle events
|
||||
HOOKS_PLUGIN_CODE = """
|
||||
from pyscript import Plugin, console
|
||||
|
||||
class TestLogger(Plugin):
|
||||
def configure(self, config):
|
||||
console.log('configure called')
|
||||
|
||||
def beforeLaunch(self, config):
|
||||
console.log('beforeLaunch called')
|
||||
|
||||
def afterSetup(self, config):
|
||||
console.log('afterSetup called')
|
||||
|
||||
def afterStartup(self, config):
|
||||
console.log('afterStartup called')
|
||||
|
||||
def onUserError(self, config):
|
||||
console.log('onUserError called')
|
||||
|
||||
|
||||
plugin = TestLogger()
|
||||
"""
|
||||
|
||||
# Source of a script that doesn't call define a `plugin` attribute
|
||||
NO_PLUGIN_CODE = """
|
||||
from pyscript import Plugin, console
|
||||
|
||||
class TestLogger(Plugin):
|
||||
pass
|
||||
"""
|
||||
|
||||
# Source code of a simple plugin that creates a Custom Element for testing purposes
|
||||
CODE_CE_PLUGIN_BAD_RETURNS = """
|
||||
from pyscript import Plugin, console
|
||||
|
||||
plugin = Plugin('py-broken')
|
||||
|
||||
@plugin.register_custom_element('py-up')
|
||||
class Upper:
|
||||
def __init__(self, element):
|
||||
self.element = element
|
||||
|
||||
def connect(self):
|
||||
# Just returning something... anything other than a string should be ignore
|
||||
return Plugin
|
||||
"""
|
||||
HTML_TEMPLATE_WITH_TAG = """
|
||||
<py-config>
|
||||
plugins = [
|
||||
"./{plugin_name}.py"
|
||||
]
|
||||
</py-config>
|
||||
|
||||
<{tagname}>
|
||||
{html}
|
||||
</{tagname}>
|
||||
"""
|
||||
HTML_TEMPLATE_NO_TAG = """
|
||||
<py-config>
|
||||
plugins = [
|
||||
"./{plugin_name}.py"
|
||||
]
|
||||
</py-config>
|
||||
"""
|
||||
|
||||
|
||||
def prepare_test(
|
||||
plugin_name, code, tagname="", html="", template=HTML_TEMPLATE_WITH_TAG
|
||||
):
|
||||
"""
|
||||
Prepares the test by writing a new plugin file named `plugin_name`.py, with `code` as its
|
||||
content and run `pyscript_run` on `template` formatted with the above inputs to create the
|
||||
page HTML code.
|
||||
|
||||
For example:
|
||||
|
||||
>> @prepare_test('py-upper', CE_PLUGIN_CODE, tagname='py-up', html="Hello World")
|
||||
>> def my_foo(...):
|
||||
>> ...
|
||||
|
||||
will:
|
||||
|
||||
* write a new `py-upper.py` file to the FS
|
||||
* the contents of `py-upper.py` is equal to CE_PLUGIN_CODE
|
||||
* call self.pyscript_run with the following string:
|
||||
'''
|
||||
<py-config>
|
||||
plugins = [
|
||||
"./py-upper.py"
|
||||
]
|
||||
</py-config>
|
||||
|
||||
<py-up>
|
||||
{html}
|
||||
</py-up>
|
||||
'''
|
||||
* call `my_foo` just like a normal decorator would
|
||||
|
||||
"""
|
||||
|
||||
def dec(f):
|
||||
def _inner(self, *args, **kws):
|
||||
self.writefile(f"{plugin_name}.py", code)
|
||||
page_html = template.format(
|
||||
plugin_name=plugin_name, tagname=tagname, html=html
|
||||
)
|
||||
self.pyscript_run(page_html)
|
||||
return f(self, *args, **kws)
|
||||
|
||||
return _inner
|
||||
|
||||
return dec
|
||||
|
||||
|
||||
class TestPlugin(PyScriptTest):
|
||||
@prepare_test("py-upper", CE_PLUGIN_CODE, tagname="py-up", html="Hello World")
|
||||
def test_py_plugin_inline(self):
|
||||
"""Test that a regular plugin that returns new HTML content from connected works"""
|
||||
# GIVEN a plugin that returns the all caps version of the tag innerHTML and logs text
|
||||
# during it's execution/hooks
|
||||
|
||||
# EXPECT the plugin logs to be present in the console logs
|
||||
log_lines = self.console.log.lines
|
||||
for log_line in ["py_upper Plugin loaded", "Upper plugin connected"]:
|
||||
assert log_line in log_lines
|
||||
|
||||
# EXPECT the inner text of the Plugin CustomElement to be all caps
|
||||
rendered_text = self.page.locator("py-up").inner_text()
|
||||
assert rendered_text == "HELLO WORLD"
|
||||
|
||||
@prepare_test("hooks_logger", HOOKS_PLUGIN_CODE, template=HTML_TEMPLATE_NO_TAG)
|
||||
def test_execution_hooks(self):
|
||||
"""Test that a Plugin that hooks into the PyScript App events, gets called
|
||||
for each one of them"""
|
||||
# GIVEN a plugin that logs specific strings for each app execution event
|
||||
hooks_available = ["afterSetup", "afterStartup"]
|
||||
hooks_unavailable = ["configure", "beforeLaunch"]
|
||||
|
||||
# EXPECT it to log the correct logs for the events it intercepts
|
||||
log_lines = self.console.log.lines
|
||||
for method in hooks_available:
|
||||
assert f"{method} called" in log_lines
|
||||
|
||||
# EXPECT it to NOT be called (hence not log anything) the events that happen
|
||||
# before it's ready, hence is not called
|
||||
for method in hooks_unavailable:
|
||||
assert f"{method} called" not in log_lines
|
||||
|
||||
# TODO: It'd be actually better to check that the events get called in order
|
||||
|
||||
@prepare_test("no_plugin", NO_PLUGIN_CODE)
|
||||
def test_no_plugin_attribute_error(self):
|
||||
"""
|
||||
Test a plugin that do not add the `plugin` attribute to its module
|
||||
"""
|
||||
# GIVEN a Plugin NO `plugin` attribute in it's module
|
||||
error_msg = (
|
||||
"[pyscript/main] Cannot find plugin on Python module no_plugin! Python plugins "
|
||||
'modules must contain a "plugin" attribute. For more information check the '
|
||||
"plugins documentation."
|
||||
)
|
||||
# EXPECT an error for the missing attribute
|
||||
assert error_msg in self.console.error.lines
|
||||
Reference in New Issue
Block a user