[rtems-central commit] items: Add ItemMapper.scope()

Sebastian Huber sebh at rtems.org
Tue Nov 21 13:35:40 UTC 2023


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 21 11:13:15 2023 +0100

items: Add ItemMapper.scope()

---

 rtemsspec/items.py                      | 8 ++++++++
 rtemsspec/tests/test_items_itemcache.py | 4 ++++
 2 files changed, 12 insertions(+)

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"):



More information about the vc mailing list