summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2023-05-05 14:41:18 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2023-05-08 14:34:56 +0200
commit50914ac703c10b36d58be2a3ddf1542521f5644a (patch)
treeb2c0828f76fd9862260eccc39ce3ac3bb513aee0
parentspecview.py: Fix API filter (diff)
downloadrtems-central-50914ac703c10b36d58be2a3ddf1542521f5644a.tar.bz2
items: Move key path resolution to ItemMapper
-rw-r--r--rtemsspec/items.py57
-rw-r--r--rtemsspec/tests/test_items_item.py18
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"