diff options
-rw-r--r-- | rtemsspec/items.py | 8 | ||||
-rw-r--r-- | rtemsspec/tests/test_items_itemcache.py | 4 |
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"): |