summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2021-10-01 08:00:14 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2021-10-01 08:06:42 +0200
commitf62530514a798103833660c299ec9fcee3773b78 (patch)
treed1ec4999cf295ebd440dbc84db3c175af400f2de
parentitems: Remove support for pipe substitution (diff)
downloadrtems-central-f62530514a798103833660c299ec9fcee3773b78.tar.bz2
items: Add support for arguments in substitutions
-rw-r--r--rtemsspec/items.py29
-rw-r--r--rtemsspec/tests/test_items_itemcache.py5
2 files changed, 25 insertions, 9 deletions
diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index d0127492..969a62f4 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -42,6 +42,7 @@ class ItemGetValueContext(NamedTuple):
value: Any
key: str
index: Any # should be int, but this triggers a mypy error
+ args: Optional[str]
ItemMap = Dict[str, "Item"]
@@ -213,6 +214,7 @@ 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.
@@ -225,17 +227,20 @@ class Item:
index = int(parts[1].split("]")[0])
except IndexError:
index = -1
- ctx = ItemGetValueContext(self, path, value, parts[0], index)
+ 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 = "") -> Any:
+ 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), {})
+ normalize_key_path(key_path, prefix), args, {})
@property
def uid(self) -> str:
@@ -416,7 +421,7 @@ class Item:
class ItemTemplate(string.Template):
""" String template for item mapper identifiers. """
- idpattern = "[a-zA-Z0-9._/-]+(:[][a-zA-Z0-9._/-]+)?"
+ idpattern = "[a-zA-Z0-9._/-]+(:[a-zA-Z0-9._/-]+)?(:[^${}]*)?"
class _ItemMapperContext(dict):
@@ -519,9 +524,19 @@ class ItemMapper:
"""
colon = identifier.find(":")
if colon >= 0:
- uid, key_path = identifier[:colon], identifier[colon + 1:]
+ uid = identifier[:colon]
+ more = identifier[colon + 1:]
+ colon = more.find(":")
+ if colon < 0:
+ key_path = more
+ args = None
+ else:
+ key_path = more[:colon]
+ args = more[colon + 1:]
else:
- uid, key_path = identifier, "/_uid"
+ uid = identifier
+ key_path = "/_uid"
+ args = None
if item is None:
item = self._item
if uid == ".":
@@ -537,7 +552,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,
+ value = item.get_by_normalized_key_path(key_path, args,
self.get_value_map(item))
except Exception as err:
msg = (f"cannot get value for '{key_path}' of {item.spec} "
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index 99f2cf5f..2636286d 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -106,7 +106,8 @@ expected <block end>, but found ':'
def get_x_to_b_value(ctx):
assert ctx.key == "x-to-b"
- return ctx.value["b"]
+ args = ctx.args if ctx.args is not None else ""
+ return ctx.value["b"] + args
def get_value_dict(ctx):
@@ -139,7 +140,7 @@ def test_item_mapper(tmpdir):
assert mapper["d/c:v"] == "c"
assert mapper["d/c:a/b"] == "e"
mapper.add_get_value(":/a/x-to-b", get_x_to_b_value)
- assert mapper["d/c:a/x-to-b"] == "e"
+ assert mapper["d/c:a/x-to-b:args:0:%"] == "eargs:0:%"
assert mapper["d/c:a/f[1]"] == 2
assert mapper["d/c:a/../a/f[3]/g[0]"] == 4
item_3, key_path_3, value_3 = mapper.map("/p:/v")