From 487ef504a888a051a1fea8da68f48daa976b28a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 23 Sep 2024 11:40:33 +0200 Subject: [PATCH] normalizes full access path strings containing dict keys with double quotes Full access paths containing stringed dictionary keys are sent with double quotes from the frontend. The quotes have to be changed to single quotes s.t. the comparison with the property dependency dictionary works. --- .../data_service/data_service_observer.py | 8 +++++-- src/pydase/utils/helpers.py | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/pydase/data_service/data_service_observer.py b/src/pydase/data_service/data_service_observer.py index 28569bc..75a78b5 100644 --- a/src/pydase/data_service/data_service_observer.py +++ b/src/pydase/data_service/data_service_observer.py @@ -8,7 +8,10 @@ from pydase.observer_pattern.observable.observable_object import ObservableObjec from pydase.observer_pattern.observer.property_observer import ( PropertyObserver, ) -from pydase.utils.helpers import get_object_attr_from_path +from pydase.utils.helpers import ( + get_object_attr_from_path, + normalize_full_access_path_string, +) from pydase.utils.serialization.serializer import ( SerializationPathError, SerializedObject, @@ -99,7 +102,8 @@ class DataServiceObserver(PropertyObserver): ) def _notify_dependent_property_changes(self, changed_attr_path: str) -> None: - changed_props = self.property_deps_dict.get(changed_attr_path, []) + normalized_attr_path = normalize_full_access_path_string(changed_attr_path) + changed_props = self.property_deps_dict.get(normalized_attr_path, []) for prop in changed_props: # only notify about changing attribute if it is not currently being # "changed" e.g. when calling the getter of a property within another diff --git a/src/pydase/utils/helpers.py b/src/pydase/utils/helpers.py index f47af3e..748213d 100644 --- a/src/pydase/utils/helpers.py +++ b/src/pydase/utils/helpers.py @@ -223,3 +223,25 @@ def current_event_loop_exists() -> bool: import asyncio return asyncio.get_event_loop_policy()._local._loop is not None # type: ignore + + +def normalize_full_access_path_string(s: str) -> str: + """Normalizes a string representing a full access path by converting double quotes + to single quotes. + + This function is useful for ensuring consistency in strings that represent access + paths containing dictionary keys, by replacing all double quotes (`"`) with single + quotes (`'`). + + Args: + s (str): The input string to be normalized. + + Returns: + A new string with all double quotes replaced by single quotes. + + Example: + >>> normalize_full_access_path_string('dictionary["first"].my_task') + "dictionary['first'].my_task" + """ + + return s.replace('"', "'")