[rtems-central commit] items: Add recursive ItemMapper substitution
Sebastian Huber
sebh at rtems.org
Fri Sep 18 11:31:55 UTC 2020
Module: rtems-central
Branch: master
Commit: 12d50d1e3bdee7e60937cb19dfd23f5badbfcb8d
Changeset: http://git.rtems.org/rtems-central/commit/?id=12d50d1e3bdee7e60937cb19dfd23f5badbfcb8d
Author: Sebastian Huber <sebastian.huber at embedded-brains.de>
Date: Wed Aug 12 08:43:35 2020 +0200
items: Add recursive ItemMapper substitution
---
rtemsspec/items.py | 19 +++++++++++++++++--
rtemsspec/tests/spec-item-cache/d/c.yml | 1 +
rtemsspec/tests/spec-item-cache/p.yml | 6 ++++++
rtemsspec/tests/test_items_itemcache.py | 2 ++
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():
More information about the vc
mailing list