mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 03:35:31 -05:00
Add REPL plugin hooks; Add output, output-mode, stderr attributes (#1106)
* Add before, after REPL hooks * Re-introduce 'output-mode' attribute for py-repl * Add plugin execution tests * Documentation * Changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mariana <marianameireles@protonmail.com>
This commit is contained in:
@@ -54,7 +54,7 @@ plugin = TestLogger()
|
||||
|
||||
# Source of script that defines a plugin with only beforePyScriptExec and
|
||||
# afterPyScriptExec methods
|
||||
EXEC_HOOKS_PLUGIN_CODE = """
|
||||
PYSCRIPT_HOOKS_PLUGIN_CODE = """
|
||||
from pyscript import Plugin
|
||||
from js import console
|
||||
|
||||
@@ -75,6 +75,31 @@ class ExecTestLogger(Plugin):
|
||||
plugin = ExecTestLogger()
|
||||
"""
|
||||
|
||||
# Source of script that defines a plugin with only beforePyScriptExec and
|
||||
# afterPyScriptExec methods
|
||||
PYREPL_HOOKS_PLUGIN_CODE = """
|
||||
from pyscript import Plugin
|
||||
from js import console
|
||||
|
||||
console.warn("This is in pyrepl hooks file")
|
||||
|
||||
class PyReplTestLogger(Plugin):
|
||||
|
||||
def beforePyReplExec(self, interpreter, outEl, src, pyReplTag):
|
||||
console.log(f'beforePyReplExec called')
|
||||
console.log(f'before_src:{src}')
|
||||
console.log(f'before_id:{pyReplTag.id}')
|
||||
|
||||
def afterPyReplExec(self, interpreter, src, outEl, pyReplTag, result):
|
||||
console.log(f'afterPyReplExec called')
|
||||
console.log(f'after_src:{src}')
|
||||
console.log(f'after_id:{pyReplTag.id}')
|
||||
console.log(f'result:{result}')
|
||||
|
||||
|
||||
plugin = PyReplTestLogger()
|
||||
"""
|
||||
|
||||
# Source of a script that doesn't call define a `plugin` attribute
|
||||
NO_PLUGIN_CODE = """
|
||||
from pyscript import Plugin
|
||||
@@ -195,6 +220,8 @@ class TestPlugin(PyScriptTest):
|
||||
"beforeLaunch",
|
||||
"beforePyScriptExec",
|
||||
"afterPyScriptExec",
|
||||
"beforePyReplExec",
|
||||
"afterPyReplExec",
|
||||
]
|
||||
|
||||
# EXPECT it to log the correct logs for the events it intercepts
|
||||
@@ -211,7 +238,7 @@ class TestPlugin(PyScriptTest):
|
||||
|
||||
@prepare_test(
|
||||
"exec_test_logger",
|
||||
EXEC_HOOKS_PLUGIN_CODE,
|
||||
PYSCRIPT_HOOKS_PLUGIN_CODE,
|
||||
template=HTML_TEMPLATE_NO_TAG + "\n<py-script id='pyid'>x=2; x</py-script>",
|
||||
)
|
||||
def test_pyscript_exec_hooks(self):
|
||||
@@ -231,6 +258,28 @@ class TestPlugin(PyScriptTest):
|
||||
assert "after_id:pyid" in log_lines
|
||||
assert "result:2" in log_lines
|
||||
|
||||
@prepare_test(
|
||||
"pyrepl_test_logger",
|
||||
PYREPL_HOOKS_PLUGIN_CODE,
|
||||
template=HTML_TEMPLATE_NO_TAG + "\n<py-repl id='pyid'>x=2; x</py-repl>",
|
||||
)
|
||||
def test_pyrepl_exec_hooks(self):
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
log_lines: list[str] = self.console.log.lines
|
||||
|
||||
assert "beforePyReplExec called" in log_lines
|
||||
assert "afterPyReplExec called" in log_lines
|
||||
|
||||
# These could be made better with a utility function that found log lines
|
||||
# that match a filter function, or start with something
|
||||
assert "before_src:x=2; x" in log_lines
|
||||
assert "before_id:pyid" in log_lines
|
||||
assert "after_src:x=2; x" in log_lines
|
||||
assert "after_id:pyid" in log_lines
|
||||
assert "result:2" in log_lines
|
||||
|
||||
@prepare_test("no_plugin", NO_PLUGIN_CODE)
|
||||
def test_no_plugin_attribute_error(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user