[rtems-central commit] items: Add Item parent_link() and child_link()

Sebastian Huber sebh at rtems.org
Tue Dec 15 07:28:18 UTC 2020


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Dec 15 08:24:09 2020 +0100

items: Add Item parent_link() and child_link()

---

 rtemsspec/items.py                 | 64 +++++++++++++++++++++++++++-----------
 rtemsspec/tests/test_items_item.py |  6 ++++
 2 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 32a9eea..3a2d5e3 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -266,26 +266,30 @@ class Item:
         """
         return self._cache[self.to_abs_uid(abs_or_rel_uid)]
 
-    def links_to_parents(self) -> Iterator[Link]:
-        """ Yields the links to the parents of this items. """
-        yield from self._links_to_parents
-
-    def parents(
+    def links_to_parents(
             self,
             role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator["Item"]:
-        """ Yields the parents of this items. """
+                                 Iterable[str]]] = None) -> Iterator[Link]:
+        """ Yields the links to the parents of this items. """
         if role is None:
             for link in self._links_to_parents:
-                yield link.item
+                yield link
         elif isinstance(role, str):
             for link in self._links_to_parents:
                 if link.role == role:
-                    yield link.item
+                    yield link
         else:
             for link in self._links_to_parents:
                 if link.role in role:
-                    yield link.item
+                    yield link
+
+    def parents(
+            self,
+            role: Optional[Union[str,
+                                 Iterable[str]]] = None) -> Iterator["Item"]:
+        """ Yields the parents of this items. """
+        for link in self.links_to_parents(role):
+            yield link.item
 
     def parent(self,
                role: Optional[Union[str, Iterable[str]]] = None,
@@ -296,26 +300,39 @@ class Item:
                 return item
         raise IndexError
 
-    def links_to_children(self) -> Iterator[Link]:
-        """ Yields the links to the children of this items. """
-        yield from self._links_to_children
+    def parent_link(self,
+                    role: Optional[Union[str, Iterable[str]]] = None,
+                    index: Optional[int] = 0) -> Link:
+        """ Returns the parent link with the specified role and index. """
+        for link_index, link in enumerate(self.links_to_parents(role)):
+            if link_index == index:
+                return link
+        raise IndexError
 
-    def children(
+    def links_to_children(
             self,
             role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator["Item"]:
-        """ Yields the children of this items. """
+                                 Iterable[str]]] = None) -> Iterator[Link]:
+        """ Yields the links to the children of this items. """
         if role is None:
             for link in self._links_to_children:
-                yield link.item
+                yield link
         elif isinstance(role, str):
             for link in self._links_to_children:
                 if link.role == role:
-                    yield link.item
+                    yield link
         else:
             for link in self._links_to_children:
                 if link.role in role:
-                    yield link.item
+                    yield link
+
+    def children(
+            self,
+            role: Optional[Union[str,
+                                 Iterable[str]]] = None) -> Iterator["Item"]:
+        """ Yields the children of this items. """
+        for link in self.links_to_children(role):
+            yield link.item
 
     def child(self,
               role: Optional[Union[str, Iterable[str]]] = None,
@@ -326,6 +343,15 @@ class Item:
                 return item
         raise IndexError
 
+    def child_link(self,
+                   role: Optional[Union[str, Iterable[str]]] = None,
+                   index: Optional[int] = 0) -> Link:
+        """ Returns the child link with the specified role and index. """
+        for link_index, link in enumerate(self.links_to_children(role)):
+            if link_index == index:
+                return link
+        raise IndexError
+
     def init_parents(self, item_cache: "ItemCache") -> None:
         """ Initializes the list of links to parents of this items. """
         for data in self._data["links"]:
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index 9a7c119..8ea44ee 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -171,6 +171,9 @@ def test_children():
     assert parent.child("c") == child
     with pytest.raises(IndexError):
         parent.child("c", 1)
+    assert parent.child_link("c").item == child
+    with pytest.raises(IndexError):
+        parent.child_link("c", 1)
 
 
 def test_parents():
@@ -209,6 +212,9 @@ def test_parents():
     assert child.parent("c") == parent
     with pytest.raises(IndexError):
         child.parent("c", 1)
+    assert child.parent_link("c").item == parent
+    with pytest.raises(IndexError):
+        child.parent_link("c", 1)
 
 
 def _is_enabled(enabled, enabled_by):



More information about the vc mailing list