summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2023-11-21 11:13:15 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2023-11-21 11:15:24 +0100
commit93a268baa1f00f2a7f182b414a8e61bee78c8ffb (patch)
tree0ad445d5001332df39f606e524f6a858bfdd7a44
parentf69934e7bcbb53e40cc676126f47ec4cca1ae15c (diff)
items: Add ItemMapper.scope()
-rw-r--r--rtemsspec/items.py8
-rw-r--r--rtemsspec/tests/test_items_itemcache.py4
2 files changed, 12 insertions, 0 deletions
diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index c3deb376..4b0d2600 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -584,6 +584,14 @@ class ItemMapper:
yield
self.pop_prefix()
+ @contextmanager
+ def scope(self, item: Item) -> Iterator[None]:
+ """ Opens an item scope context. """
+ previous = self._item
+ self._item = item
+ yield
+ self._item = previous
+
def get_value_map(self, item: Item) -> ItemGetValueMap:
""" Returns the get value map for the item. """
return self._get_value_map.get(item.type, {})
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index 864ef70a..11aa4b5a 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -146,6 +146,10 @@ def test_item_mapper(tmpdir):
assert key_path_2 == "/v"
assert value_2 == "p"
assert mapper.substitute("$$${.:.}") == "$p"
+ assert mapper.item == item
+ with mapper.scope(item_cache["/spec/root"]):
+ assert mapper.item == item_cache["/spec/root"]
+ assert mapper.item == item
assert mapper.substitute("$$${.:.}", prefix="v") == "$p"
with mapper.prefix("x"):
with mapper.prefix("y"):