mirror of
https://github.com/tiqi-group/pydase.git
synced 2026-02-13 05:48:40 +01:00
replaces no_frontend decorator with "frontend" decorator
This commit is contained in:
@@ -29,21 +29,39 @@ class KeywordArgumentError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# TODO: This decorator might be used on objects other than functions
|
def frontend(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
def no_frontend(func: Callable[..., Any]) -> Callable[..., Any]:
|
"""
|
||||||
"""Decorator to mark a DataService method as excluded from frontend rendering."""
|
Decorator to mark a DataService method for frontend rendering. Ensures that the
|
||||||
|
method does not contain arguments, as they are not supported for frontend rendering.
|
||||||
|
"""
|
||||||
|
|
||||||
func._hide_from_frontend = True # type: ignore
|
sig = inspect.signature(func)
|
||||||
|
parameters = dict(sig.parameters)
|
||||||
|
# Remove 'self' parameter for instance methods.
|
||||||
|
parameters.pop("self", None)
|
||||||
|
|
||||||
|
# Check if there are any parameters left which would indicate additional arguments.
|
||||||
|
if len(parameters) > 0:
|
||||||
|
parameter_list = ", ".join([f"{name!r}" for name, _ in parameters.items()])
|
||||||
|
raise Exception(
|
||||||
|
"The @frontend decorator requires functions without arguments. Function "
|
||||||
|
f"'{func.__name__}' has argument(s): {parameter_list}. "
|
||||||
|
"Please remove the argument(s) from this function to use it with the "
|
||||||
|
"@frontend decorator."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mark the function for frontend display.
|
||||||
|
func._display_in_frontend = True
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def render_in_frontend(func: Callable[..., Any]) -> bool:
|
def render_in_frontend(func: Callable[..., Any]) -> bool:
|
||||||
"""Determines if the method is not decorated with the `@no_frontend` decorator."""
|
"""Determines if the method is decorated with the `@frontend` decorator."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return func._hide_from_frontend # type: ignore
|
return func._display_in_frontend # type: ignore
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return True
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Serializer:
|
class Serializer:
|
||||||
|
|||||||
Reference in New Issue
Block a user