[rtems-central commit] items: Allow multiple roles to follow links

Sebastian Huber sebh at rtems.org
Thu Oct 8 13:25:41 UTC 2020


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Oct  2 16:31:57 2020 +0200

items: Allow multiple roles to follow links

---

 rtemsspec/items.py                 | 26 ++++++++++++++++++++------
 rtemsspec/tests/test_items_item.py | 10 ++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 4565316..658b68c 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -29,8 +29,8 @@ import os
 import pickle
 import string
 import stat
-from typing import Any, Callable, Dict, Iterator, List, NamedTuple, Mapping, \
-    Optional, Tuple
+from typing import Any, Callable, Dict, Iterable, Iterator, List, NamedTuple, \
+    Mapping, Optional, Tuple, Union
 import yaml
 
 
@@ -239,29 +239,43 @@ class Item:
         """ Yields the links to the parents of this items. """
         yield from self._links_to_parents
 
-    def parents(self, role: Optional[str] = None) -> Iterator["Item"]:
+    def parents(
+            self,
+            role: Optional[Union[str,
+                                 Iterable[str]]] = None) -> Iterator["Item"]:
         """ Yields the parents of this items. """
         if role is None:
             for link in self._links_to_parents:
                 yield link.item
-        else:
+        elif isinstance(role, str):
             for link in self._links_to_parents:
                 if link.role == role:
                     yield link.item
+        else:
+            for link in self._links_to_parents:
+                if link.role in role:
+                    yield link.item
 
     def links_to_children(self) -> Iterator[Link]:
         """ Yields the links to the children of this items. """
         yield from self._links_to_children
 
-    def children(self, role: Optional[str] = None) -> Iterator["Item"]:
+    def children(
+            self,
+            role: Optional[Union[str,
+                                 Iterable[str]]] = None) -> Iterator["Item"]:
         """ Yields the children of this items. """
         if role is None:
             for link in self._links_to_children:
                 yield link.item
-        else:
+        elif isinstance(role, str):
             for link in self._links_to_children:
                 if link.role == role:
                     yield link.item
+        else:
+            for link in self._links_to_children:
+                if link.role in role:
+                    yield link.item
 
     def init_parents(self, item_cache: "ItemCache"):
         """ Initializes the list of links to parents of this items. """
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index 0a376d2..fcc2258 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -137,6 +137,11 @@ def test_children():
     children = [item for item in parent.children("c")]
     assert len(children) == 1
     assert children[0] == child
+    children = [item for item in parent.children(["c", "d"])]
+    assert len(children) == 1
+    assert children[0] == child
+    children = [item for item in parent.children([])]
+    assert len(children) == 0
     children = [item for item in parent.children("d")]
     assert len(children) == 0
     links = [link for link in parent.links_to_children()]
@@ -165,6 +170,11 @@ def test_parents():
     parents = [item for item in child.parents("c")]
     assert len(parents) == 1
     assert parents[0] == parent
+    parents = [item for item in child.parents(["c", "d"])]
+    assert len(parents) == 1
+    assert parents[0] == parent
+    parents = [item for item in child.parents([])]
+    assert len(parents) == 0
     parents = [item for item in child.parents("d")]
     assert len(parents) == 0
     links = [link for link in child.links_to_parents()]



More information about the vc mailing list