summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2020-08-12 08:43:35 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2020-09-18 13:27:36 +0200
commit12d50d1e3bdee7e60937cb19dfd23f5badbfcb8d (patch)
tree988ad06f4a6bbdb1789656a1b3896ba2657ea6ff
parent877756ff037bc2df61aa6ee98362fc7c8ae0f44f (diff)
downloadrtems-central-12d50d1e3bdee7e60937cb19dfd23f5badbfcb8d.tar.bz2
items: Add recursive ItemMapper substitution
-rw-r--r--rtemsspec/items.py19
-rw-r--r--rtemsspec/tests/spec-item-cache/d/c.yml1
-rw-r--r--rtemsspec/tests/spec-item-cache/p.yml6
-rw-r--r--rtemsspec/tests/test_items_itemcache.py2
4 files changed, 26 insertions, 2 deletions
diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index fa64f00..f44728c 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -314,8 +314,9 @@ class ItemTemplate(string.Template):
class ItemMapper(Mapping[str, object]):
""" Maps identifiers to items and attribute values. """
- def __init__(self, item: Item):
+ def __init__(self, item: Item, recursive: bool = False):
self._item = item
+ self._recursive = recursive
self._prefix = [""]
self._get_value = {} # type: Dict[str, ItemGetValue]
@@ -373,8 +374,22 @@ class ItemMapper(Mapping[str, object]):
value = getattr(self, func)(value)
return item, key_path, value
+ @contextmanager
+ def _item_and_prefix(self, item: Item, prefix: str) -> Iterator[None]:
+ item_2 = self._item
+ prefix_2 = self._prefix
+ self._item = item
+ self._prefix = [prefix]
+ yield
+ self._item = item_2
+ self._prefix = prefix_2
+
def __getitem__(self, identifier):
- return self.map(identifier)[2]
+ item, key_path, value = self.map(identifier)
+ if self._recursive:
+ with self._item_and_prefix(item, os.path.dirname(key_path)):
+ return self.substitute(value)
+ return value
def __iter__(self):
raise StopIteration
diff --git a/rtemsspec/tests/spec-item-cache/d/c.yml b/rtemsspec/tests/spec-item-cache/d/c.yml
index f901443..4b9febc 100644
--- a/rtemsspec/tests/spec-item-cache/d/c.yml
+++ b/rtemsspec/tests/spec-item-cache/d/c.yml
@@ -11,3 +11,4 @@ links:
- role: null
uid: ../p
v: c
+r6: ${../p:/r7}
diff --git a/rtemsspec/tests/spec-item-cache/p.yml b/rtemsspec/tests/spec-item-cache/p.yml
index 4c3a296..409b169 100644
--- a/rtemsspec/tests/spec-item-cache/p.yml
+++ b/rtemsspec/tests/spec-item-cache/p.yml
@@ -2,3 +2,9 @@ links: []
v: p
x:
y: z
+r1:
+ r2:
+ r3: ${.:../r4}
+ r4: ${.:../r5}
+r5: ${/d/c:/r6}
+r7: foobar
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index 2323760..b8507fc 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -128,6 +128,8 @@ def test_item_mapper(tmpdir):
pass
with pytest.raises(AttributeError):
len(mapper)
+ recursive_mapper = ItemMapper(item, recursive=True)
+ assert recursive_mapper.substitute("${.:/r1/r2/r3}") == "foobar"
def test_empty_item_mapper():