updates data service cache (methods to set and get values)

This commit is contained in:
Mose Müller 2023-11-30 17:15:06 +01:00
parent 114a1c6fdc
commit 3497962fca

View File

@ -1,7 +1,12 @@
import logging
from typing import TYPE_CHECKING, Any
from pydase.utils.serializer import set_nested_value_by_path
from pydase.utils.serializer import (
SerializationPathError,
SerializationValueError,
get_nested_dict_by_path,
set_nested_value_by_path,
)
if TYPE_CHECKING:
from pydase import DataService
@ -24,11 +29,11 @@ class DataServiceCache:
logger.debug("Initializing cache.")
self._cache = self.service.serialize()
def update_cache(self, parent_path: str, name: str, value: Any) -> None:
# Remove the part before the first "." in the parent_path
parent_path = ".".join(parent_path.split(".")[1:])
def update_cache(self, full_access_path: str, value: Any) -> None:
set_nested_value_by_path(self._cache, full_access_path, value)
# Construct the full path
full_path = f"{parent_path}.{name}" if parent_path else name
set_nested_value_by_path(self._cache, full_path, value)
def get_value_dict_from_cache(self, full_access_path: str) -> dict[str, Any] | None:
try:
return get_nested_dict_by_path(self._cache, full_access_path)
except (SerializationPathError, SerializationValueError, KeyError):
return None