moves frontend decorator into decorators module

This commit is contained in:
Mose Müller 2024-02-27 17:34:34 +01:00
parent a05b703bb8
commit 990add216c
3 changed files with 42 additions and 38 deletions

View File

@ -0,0 +1,27 @@
from collections.abc import Callable
from typing import Any
from pydase.utils.helpers import function_has_arguments
class FunctionDefinitionError(Exception):
pass
def frontend(func: Callable[..., Any]) -> Callable[..., Any]:
"""
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.
"""
if function_has_arguments(func):
raise FunctionDefinitionError(
"The @frontend decorator requires functions without arguments. Function "
f"'{func.__name__}' has at least one argument. "
"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 # type: ignore
return func

View File

@ -209,3 +209,17 @@ def function_has_arguments(func: Callable[..., Any]) -> bool:
if len(parameters) > 0: if len(parameters) > 0:
return True return True
return False return False
def render_in_frontend(func: Callable[..., Any]) -> bool:
"""Determines if the method should be rendered in the frontend.
It checks if the "@frontend" decorator was used or the method is a coroutine."""
if inspect.iscoroutinefunction(func):
return True
try:
return func._display_in_frontend # type: ignore
except AttributeError:
return False

View File

@ -9,11 +9,11 @@ import pydase.units as u
from pydase.data_service.abstract_data_service import AbstractDataService from pydase.data_service.abstract_data_service import AbstractDataService
from pydase.data_service.task_manager import TaskStatus from pydase.data_service.task_manager import TaskStatus
from pydase.utils.helpers import ( from pydase.utils.helpers import (
function_has_arguments,
get_attribute_doc, get_attribute_doc,
get_component_classes, get_component_classes,
get_data_service_class_reference, get_data_service_class_reference,
parse_list_attr_and_index, parse_list_attr_and_index,
render_in_frontend,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -27,43 +27,6 @@ class SerializationValueError(Exception):
pass pass
class FunctionDefinitionError(Exception):
pass
def frontend(func: Callable[..., Any]) -> Callable[..., Any]:
"""
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.
"""
if function_has_arguments(func):
raise FunctionDefinitionError(
"The @frontend decorator requires functions without arguments. Function "
f"'{func.__name__}' has at least one argument. "
"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 # type: ignore
return func
def render_in_frontend(func: Callable[..., Any]) -> bool:
"""Determines if the method should be rendered in the frontend.
It checks if the "@frontend" decorator was used or the method is a coroutine."""
if inspect.iscoroutinefunction(func):
return True
try:
return func._display_in_frontend # type: ignore
except AttributeError:
return False
class Serializer: class Serializer:
@staticmethod @staticmethod
def serialize_object(obj: Any) -> dict[str, Any]: def serialize_object(obj: Any) -> dict[str, Any]: