mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-05-05 23:10:08 +02:00
moves get_attribute_doc to helpers
This commit is contained in:
parent
b6953251b9
commit
615bf294e1
@ -1,3 +1,4 @@
|
|||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
@ -17,6 +18,16 @@ STANDARD_TYPES = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_attribute_doc(attr: Any) -> Optional[str]:
|
||||||
|
"""This function takes an input attribute attr and returns its documentation
|
||||||
|
string if it's different from the documentation of its type, otherwise,
|
||||||
|
it returns None.
|
||||||
|
"""
|
||||||
|
attr_doc = inspect.getdoc(attr)
|
||||||
|
attr_class_doc = inspect.getdoc(type(attr))
|
||||||
|
return attr_doc if attr_class_doc != attr_doc else None
|
||||||
|
|
||||||
|
|
||||||
def get_class_and_instance_attributes(obj: object) -> dict[str, Any]:
|
def get_class_and_instance_attributes(obj: object) -> dict[str, Any]:
|
||||||
"""Dictionary containing all attributes (both instance and class level) of a
|
"""Dictionary containing all attributes (both instance and class level) of a
|
||||||
given object.
|
given object.
|
||||||
|
@ -2,12 +2,13 @@ import inspect
|
|||||||
import logging
|
import logging
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Optional, cast
|
from typing import Any, Optional
|
||||||
|
|
||||||
import pydase.units as u
|
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.utils.helpers import (
|
from pydase.utils.helpers import (
|
||||||
STANDARD_TYPES,
|
STANDARD_TYPES,
|
||||||
|
get_attribute_doc,
|
||||||
get_component_class_names,
|
get_component_class_names,
|
||||||
parse_list_attr_and_index,
|
parse_list_attr_and_index,
|
||||||
)
|
)
|
||||||
@ -15,17 +16,8 @@ from pydase.utils.helpers import (
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Serializer:
|
|
||||||
@staticmethod
|
|
||||||
def get_attribute_doc(attr: Any) -> Optional[str]:
|
|
||||||
"""This function takes an input attribute attr and returns its documentation
|
|
||||||
string if it's different from the documentation of its type, otherwise,
|
|
||||||
it returns None.
|
|
||||||
"""
|
|
||||||
attr_doc = inspect.getdoc(attr)
|
|
||||||
attr_class_doc = inspect.getdoc(type(attr))
|
|
||||||
return attr_doc if attr_class_doc != attr_doc else None
|
|
||||||
|
|
||||||
|
class Serializer:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def serialize_object(obj: Any) -> dict[str, Any]:
|
def serialize_object(obj: Any) -> dict[str, Any]:
|
||||||
result: dict[str, Any] = {}
|
result: dict[str, Any] = {}
|
||||||
@ -54,7 +46,7 @@ class Serializer:
|
|||||||
obj_type = type(obj).__name__
|
obj_type = type(obj).__name__
|
||||||
value = obj
|
value = obj
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
result = {
|
result = {
|
||||||
"type": obj_type,
|
"type": obj_type,
|
||||||
"value": value,
|
"value": value,
|
||||||
@ -68,7 +60,7 @@ class Serializer:
|
|||||||
def _serialize_enum(obj: Enum) -> dict[str, Any]:
|
def _serialize_enum(obj: Enum) -> dict[str, Any]:
|
||||||
value = obj.name
|
value = obj.name
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
if type(obj).__base__.__name__ == "ColouredEnum":
|
if type(obj).__base__.__name__ == "ColouredEnum":
|
||||||
obj_type = "ColouredEnum"
|
obj_type = "ColouredEnum"
|
||||||
else:
|
else:
|
||||||
@ -88,7 +80,7 @@ class Serializer:
|
|||||||
def _serialize_Quantity(obj: u.Quantity) -> dict[str, Any]:
|
def _serialize_Quantity(obj: u.Quantity) -> dict[str, Any]:
|
||||||
obj_type = "Quantity"
|
obj_type = "Quantity"
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
value = {"magnitude": obj.m, "unit": str(obj.u)}
|
value = {"magnitude": obj.m, "unit": str(obj.u)}
|
||||||
return {
|
return {
|
||||||
"type": obj_type,
|
"type": obj_type,
|
||||||
@ -101,7 +93,7 @@ class Serializer:
|
|||||||
def _serialize_dict(obj: dict[str, Any]) -> dict[str, Any]:
|
def _serialize_dict(obj: dict[str, Any]) -> dict[str, Any]:
|
||||||
obj_type = "dict"
|
obj_type = "dict"
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
value = {key: Serializer.serialize_object(val) for key, val in obj.items()}
|
value = {key: Serializer.serialize_object(val) for key, val in obj.items()}
|
||||||
return {
|
return {
|
||||||
"type": obj_type,
|
"type": obj_type,
|
||||||
@ -114,7 +106,7 @@ class Serializer:
|
|||||||
def _serialize_list(obj: list[Any]) -> dict[str, Any]:
|
def _serialize_list(obj: list[Any]) -> dict[str, Any]:
|
||||||
obj_type = "list"
|
obj_type = "list"
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
value = [Serializer.serialize_object(o) for o in obj]
|
value = [Serializer.serialize_object(o) for o in obj]
|
||||||
return {
|
return {
|
||||||
"type": obj_type,
|
"type": obj_type,
|
||||||
@ -128,7 +120,7 @@ class Serializer:
|
|||||||
obj_type = "method"
|
obj_type = "method"
|
||||||
value = None
|
value = None
|
||||||
readonly = True
|
readonly = True
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
|
|
||||||
# Store parameters and their anotations in a dictionary
|
# Store parameters and their anotations in a dictionary
|
||||||
sig = inspect.signature(obj)
|
sig = inspect.signature(obj)
|
||||||
@ -158,7 +150,7 @@ class Serializer:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _serialize_DataService(obj: AbstractDataService) -> dict[str, Any]:
|
def _serialize_DataService(obj: AbstractDataService) -> dict[str, Any]:
|
||||||
readonly = False
|
readonly = False
|
||||||
doc = Serializer.get_attribute_doc(obj)
|
doc = get_attribute_doc(obj)
|
||||||
obj_type = type(obj).__name__
|
obj_type = type(obj).__name__
|
||||||
if type(obj).__name__ not in get_component_class_names():
|
if type(obj).__name__ not in get_component_class_names():
|
||||||
obj_type = "DataService"
|
obj_type = "DataService"
|
||||||
@ -205,9 +197,7 @@ class Serializer:
|
|||||||
if isinstance(getattr(obj.__class__, key, None), property):
|
if isinstance(getattr(obj.__class__, key, None), property):
|
||||||
prop: property = getattr(obj.__class__, key)
|
prop: property = getattr(obj.__class__, key)
|
||||||
value[key]["readonly"] = prop.fset is None
|
value[key]["readonly"] = prop.fset is None
|
||||||
value[key]["doc"] = Serializer.get_attribute_doc(
|
value[key]["doc"] = get_attribute_doc(prop) # overwrite the doc
|
||||||
prop
|
|
||||||
) # overwrite the doc
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type": obj_type,
|
"type": obj_type,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user