[rtems-central commit] items: Move key path resolution to ItemMapper

Sebastian Huber sebh at rtems.org
Tue May 9 13:45:26 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    50914ac703c10b36d58be2a3ddf1542521f5644a
Changeset: http://git.rtems.org/rtems-central/commit/?id=50914ac703c10b36d58be2a3ddf1542521f5644a

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May  5 14:41:18 2023 +0200

items: Move key path resolution to ItemMapper

---

 rtemsspec/items.py                 | 57 ++++++++++++++++----------------------
 rtemsspec/tests/test_items_item.py | 18 ------------
 2 files changed, 24 insertions(+), 51 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index fb4a1d0b..7b23d6e5 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -239,35 +239,6 @@ class Item:
         """
         return self._data.get(key, default)
 
-    def get_by_normalized_key_path(self, normalized_key_path: str,
-                                   args: Optional[str],
-                                   get_value_map: ItemGetValueMap) -> Any:
-        """
-        Gets the attribute value corresponding to the normalized key path.
-        """
-        path = "/"
-        value = self._data
-        for key in normalized_key_path.strip("/").split("/"):
-            parts = key.split("[")
-            try:
-                index = int(parts[1].split("]")[0])
-            except IndexError:
-                index = -1
-            ctx = ItemGetValueContext(self, path, value, parts[0], index, args)
-            get_value, get_value_map = get_value_map.get(
-                parts[0], (_get_value, {}))
-            value = get_value(ctx)
-            path = os.path.join(path, key)
-        return value
-
-    def get_by_key_path(self,
-                        key_path: str,
-                        prefix: str = "",
-                        args: Optional[str] = None) -> Any:
-        """ Gets the attribute value corresponding to the key path. """
-        return self.get_by_normalized_key_path(
-            normalize_key_path(key_path, prefix), args, {})
-
     @property
     def uid(self) -> str:
         """ Returns the UID of the item. """
@@ -556,13 +527,34 @@ class ItemMapper:
         """ Returns the get value map for the item. """
         return self._get_value_map.get(item.type, {})
 
+    def _get_by_normalized_key_path(self, item: Item, normalized_key_path: str,
+                                    args: Optional[str]) -> Any:
+        """
+        Gets the attribute value associated with the normalized key path.
+        """
+        get_value_map = self.get_value_map(item)
+        path = "/"
+        value = item.data
+        for key in normalized_key_path.strip("/").split("/"):
+            parts = key.split("[")
+            try:
+                index = int(parts[1].split("]")[0])
+            except IndexError:
+                index = -1
+            ctx = ItemGetValueContext(item, path, value, parts[0], index, args)
+            get_value, get_value_map = get_value_map.get(
+                parts[0], (_get_value, {}))
+            value = get_value(ctx)
+            path = os.path.join(path, key)
+        return value
+
     def map(self,
             identifier: str,
             item: Optional[Item] = None,
             prefix: Optional[str] = None) -> Tuple[Item, str, Any]:
         """
-        Maps an identifier with item and prefix to the corresponding item and
-        attribute value.
+        Maps the identifier with item and prefix to the associated item, key
+        path, and attribute value.
         """
         colon = identifier.find(":")
         if colon >= 0:
@@ -594,8 +586,7 @@ class ItemMapper:
                 raise ValueError(msg) from err
         key_path = normalize_key_path(key_path, prefix)
         try:
-            value = item.get_by_normalized_key_path(key_path, args,
-                                                    self.get_value_map(item))
+            value = self._get_by_normalized_key_path(item, key_path, args)
         except Exception as err:
             msg = (f"cannot get value for '{key_path}' of {item.spec} "
                    f"specified by '{identifier}'")
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index 6afa38a6..9c57b16f 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -105,24 +105,6 @@ def test_digest():
     assert i.digest == "_sNRYXk0DTOp1lptrqqd2kb5hIlg-SGeynVVLGnbmKs="
 
 
-def test_get_key_path():
-    data = {}
-    data["a"] = {"b": "c", "d": [1, 2, 3]}
-    data["x"] = "y"
-    item = Item(EmptyItemCache(), "z", data)
-    assert item.get_by_key_path("x") == "y"
-    assert item.get_by_key_path("a/d[2]") == 3
-    assert item.get_by_key_path("a/b/../d[0]") == 1
-    assert item.get_by_key_path("/a/b/../d[0]") == 1
-    assert item.get_by_key_path("../d[0]", "a/b") == 1
-    with pytest.raises(KeyError):
-        assert item.get_by_key_path("y")
-    with pytest.raises(ValueError):
-        assert item.get_by_key_path("[")
-    with pytest.raises(ValueError):
-        assert item.get_by_key_path("x[y]")
-
-
 def test_getitem():
     data = {}
     data["x"] = "y"



More information about the vc mailing list