From 3c2f425deee757bfdee6e6830a6fc998d4101980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Tue, 27 Feb 2024 08:14:34 +0100 Subject: [PATCH] adds "no_frontend" decorator for emitting frontend rendering of method The method serialization now contains a "frontend_render" key with boolean value. --- src/pydase/utils/serializer.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pydase/utils/serializer.py b/src/pydase/utils/serializer.py index 2dfed20..9e364a7 100644 --- a/src/pydase/utils/serializer.py +++ b/src/pydase/utils/serializer.py @@ -29,6 +29,23 @@ class KeywordArgumentError(Exception): pass +# TODO: This decorator might be used on objects other than functions +def no_frontend(func: Callable[..., Any]) -> Callable[..., Any]: + """Decorator to mark a DataService method as excluded from frontend rendering.""" + + func._hide_from_frontend = True # type: ignore + return func + + +def render_in_frontend(func: Callable[..., Any]) -> bool: + """Determines if the method is not decorated with the `@no_frontend` decorator.""" + + try: + return func._hide_from_frontend # type: ignore + except AttributeError: + return True + + class Serializer: @staticmethod def serialize_object(obj: Any) -> dict[str, Any]: @@ -197,6 +214,7 @@ class Serializer: "doc": doc, "async": inspect.iscoroutinefunction(obj), "parameters": parameters, + "frontend_render": render_in_frontend(obj), } @staticmethod