[rtems-central commit] items: Yield only links of enabled items

Sebastian Huber sebh at rtems.org
Tue May 9 13:45:26 UTC 2023


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May  5 14:41:18 2023 +0200

items: Yield only links of enabled items

---

 rtemsspec/items.py                 | 106 ++++++++++++++++++++++++-------------
 rtemsspec/tests/test_build.py      |   4 +-
 rtemsspec/tests/test_items_item.py |  28 +++++++---
 rtemsspec/tests/test_validation.py |   4 +-
 spec2modules.py                    |   3 +-
 specview.py                        |  15 ++----
 6 files changed, 100 insertions(+), 60 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 4819aa03..d9cf8801 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -188,6 +188,10 @@ def _match_to_upper(match: Match) -> str:
     return match.group(1).upper()
 
 
+def _is_link_enabled(link: Link) -> bool:
+    return link.item._data["_enabled"]  # pylint: disable=protected-access
+
+
 class Item:
     """ Objects of this class represent a specification item. """
 
@@ -274,87 +278,108 @@ class Item:
         return self._cache[self.to_abs_uid(abs_or_rel_uid)]
 
     def links_to_parents(
-            self,
-            role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator[Link]:
+        self,
+        role: Optional[Union[str, Iterable[str]]] = None,
+        is_link_enabled: Callable[[Link], bool] = _is_link_enabled
+    ) -> Iterator[Link]:
         """ Yields the links to the parents of this items. """
         if role is None:
             for link in self._links_to_parents:
-                yield link
+                if is_link_enabled(link):
+                    yield link
         elif isinstance(role, str):
             for link in self._links_to_parents:
-                if link.role == role:
+                if link.role == role and is_link_enabled(link):
                     yield link
         else:
             for link in self._links_to_parents:
-                if link.role in role:
+                if link.role in role and is_link_enabled(link):
                     yield link
 
     def parents(
-            self,
-            role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator["Item"]:
+        self,
+        role: Optional[Union[str, Iterable[str]]] = None,
+        is_link_enabled: Callable[[Link], bool] = _is_link_enabled
+    ) -> Iterator["Item"]:
         """ Yields the parents of this items. """
-        for link in self.links_to_parents(role):
+        for link in self.links_to_parents(role, is_link_enabled):
             yield link.item
 
-    def parent(self,
-               role: Optional[Union[str, Iterable[str]]] = None,
-               index: Optional[int] = 0) -> "Item":
+    def parent(
+            self,
+            role: Optional[Union[str, Iterable[str]]] = None,
+            index: Optional[int] = 0,
+            is_link_enabled: Callable[[Link],
+                                      bool] = _is_link_enabled) -> "Item":
         """ Returns the parent with the specified role and index. """
-        for item_index, item in enumerate(self.parents(role)):
+        for item_index, item in enumerate(self.parents(role, is_link_enabled)):
             if item_index == index:
                 return item
         raise IndexError
 
-    def parent_link(self,
-                    role: Optional[Union[str, Iterable[str]]] = None,
-                    index: Optional[int] = 0) -> Link:
+    def parent_link(
+            self,
+            role: Optional[Union[str, Iterable[str]]] = None,
+            index: Optional[int] = 0,
+            is_link_enabled: Callable[[Link],
+                                      bool] = _is_link_enabled) -> Link:
         """ Returns the parent link with the specified role and index. """
-        for link_index, link in enumerate(self.links_to_parents(role)):
+        for link_index, link in enumerate(
+                self.links_to_parents(role, is_link_enabled)):
             if link_index == index:
                 return link
         raise IndexError
 
     def links_to_children(
-            self,
-            role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator[Link]:
+        self,
+        role: Optional[Union[str, Iterable[str]]] = None,
+        is_link_enabled: Callable[[Link], bool] = _is_link_enabled
+    ) -> Iterator[Link]:
         """ Yields the links to the children of this items. """
         if role is None:
             for link in self._links_to_children:
-                yield link
+                if is_link_enabled(link):
+                    yield link
         elif isinstance(role, str):
             for link in self._links_to_children:
-                if link.role == role:
+                if link.role == role and is_link_enabled(link):
                     yield link
         else:
             for link in self._links_to_children:
-                if link.role in role:
+                if link.role in role and is_link_enabled(link):
                     yield link
 
     def children(
-            self,
-            role: Optional[Union[str,
-                                 Iterable[str]]] = None) -> Iterator["Item"]:
+        self,
+        role: Optional[Union[str, Iterable[str]]] = None,
+        is_link_enabled: Callable[[Link], bool] = _is_link_enabled
+    ) -> Iterator["Item"]:
         """ Yields the children of this items. """
-        for link in self.links_to_children(role):
+        for link in self.links_to_children(role, is_link_enabled):
             yield link.item
 
-    def child(self,
-              role: Optional[Union[str, Iterable[str]]] = None,
-              index: Optional[int] = 0) -> "Item":
+    def child(
+            self,
+            role: Optional[Union[str, Iterable[str]]] = None,
+            index: Optional[int] = 0,
+            is_link_enabled: Callable[[Link],
+                                      bool] = _is_link_enabled) -> "Item":
         """ Returns the child with the specified role and index. """
-        for item_index, item in enumerate(self.children(role)):
+        for item_index, item in enumerate(self.children(role,
+                                                        is_link_enabled)):
             if item_index == index:
                 return item
         raise IndexError
 
-    def child_link(self,
-                   role: Optional[Union[str, Iterable[str]]] = None,
-                   index: Optional[int] = 0) -> Link:
+    def child_link(
+            self,
+            role: Optional[Union[str, Iterable[str]]] = None,
+            index: Optional[int] = 0,
+            is_link_enabled: Callable[[Link],
+                                      bool] = _is_link_enabled) -> Link:
         """ Returns the child link with the specified role and index. """
-        for link_index, link in enumerate(self.links_to_children(role)):
+        for link_index, link in enumerate(
+                self.links_to_children(role, is_link_enabled)):
             if link_index == index:
                 return link
         raise IndexError
@@ -372,7 +397,7 @@ class Item:
 
     def init_children(self) -> None:
         """ Initializes the list of links to children of this items. """
-        for link in self.links_to_parents():
+        for link in self._links_to_parents:
             link.item.add_link_to_child(Link.create(link, self))
 
     def add_link_to_parent(self, link: Link):
@@ -636,7 +661,7 @@ class _SpecType(NamedTuple):
 
 def _gather_spec_refinements(item: Item) -> Optional[_SpecType]:
     new_type: Optional[_SpecType] = None
-    for link in item.links_to_children():
+    for link in item._links_to_children:  # pylint: disable=protected-access
         if link.role == "spec-refinement":
             key = link["spec-key"]
             if new_type is None:
@@ -677,6 +702,11 @@ def _is_item_enabled(enabled: List[str], item: Item) -> bool:
     return is_enabled(enabled, item["enabled-by"])
 
 
+def item_is_enabled(_enabled: List[str], _item: Item) -> bool:
+    """ Returns true. """
+    return True
+
+
 class ItemCache:
     """ This class provides a cache of specification items. """
 
diff --git a/rtemsspec/tests/test_build.py b/rtemsspec/tests/test_build.py
index 7634ad29..a1e1a995 100644
--- a/rtemsspec/tests/test_build.py
+++ b/rtemsspec/tests/test_build.py
@@ -25,14 +25,14 @@
 # POSSIBILITY OF SUCH DAMAGE.
 
 from rtemsspec.build import gather_files
-from rtemsspec.items import ItemCache
+from rtemsspec.items import ItemCache, item_is_enabled
 from rtemsspec.tests.util import create_item_cache_config_and_copy_spec
 
 
 def test_build(tmpdir):
     item_cache_config = create_item_cache_config_and_copy_spec(
         tmpdir, "spec-build", with_spec_types=True)
-    item_cache = ItemCache(item_cache_config)
+    item_cache = ItemCache(item_cache_config, is_item_enabled=item_is_enabled)
 
     build_config = {}
     build_config["arch"] = "foo"
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index 9c57b16f..efbe4b7d 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -131,8 +131,13 @@ def test_get():
 
 def test_children():
     child = Item(EmptyItemCache(), "c", {})
+    child["_enabled"] = True
+    child_2 = Item(EmptyItemCache(), "c2", {})
+    child_2["_enabled"] = False
     parent = Item(EmptyItemCache(), "p", {})
+    parent["_enabled"] = True
     parent.add_link_to_child(Link(child, {"a": "b", "role": "c"}))
+    parent.add_link_to_child(Link(child_2, {"a": "b", "role": "c2"}))
     children = [item for item in parent.children()]
     assert len(children) == 1
     assert children[0] == child
@@ -161,14 +166,25 @@ def test_children():
 
 def test_parents():
     item_cache = EmptyItemCache()
-    child = Item(item_cache, "c",
-                 {"links": [{
-                     "a": "b",
-                     "role": "c",
-                     "uid": "p"
-                 }]})
+    child = Item(
+        item_cache, "c", {
+            "links": [{
+                "a": "b",
+                "role": "c",
+                "uid": "p"
+            }, {
+                "a": "b",
+                "role": "c",
+                "uid": "p2"
+            }]
+        })
+    child["_enabled"] = True
     parent = Item(item_cache, "p", {"links": []})
+    parent["_enabled"] = True
     item_cache._items["p"] = parent
+    parent_2 = Item(EmptyItemCache(), "p2", {})
+    parent_2["_enabled"] = False
+    item_cache._items["p2"] = parent_2
     child.init_parents(item_cache)
     for link in child.links_to_parents():
         link.item.add_link_to_child(Link.create(link, child))
diff --git a/rtemsspec/tests/test_validation.py b/rtemsspec/tests/test_validation.py
index 51c209a4..698d2dfe 100644
--- a/rtemsspec/tests/test_validation.py
+++ b/rtemsspec/tests/test_validation.py
@@ -29,7 +29,7 @@ import pytest
 
 from rtemsspec.validation import augment_with_test_case_links, generate, \
     TransitionMap
-from rtemsspec.items import EmptyItemCache, Item, ItemCache
+from rtemsspec.items import EmptyItemCache, Item, ItemCache, item_is_enabled
 from rtemsspec.tests.util import create_item_cache_config_and_copy_spec
 
 
@@ -41,7 +41,7 @@ def test_validation(tmpdir):
 
     item_cache_config = create_item_cache_config_and_copy_spec(
         tmpdir, "spec-validation", with_spec_types=True)
-    item_cache = ItemCache(item_cache_config)
+    item_cache = ItemCache(item_cache_config, is_item_enabled=item_is_enabled)
     augment_with_test_case_links(item_cache)
 
     transition_map = TransitionMap(item_cache["/directive"])
diff --git a/spec2modules.py b/spec2modules.py
index 4ae7a92d..16df8cf8 100755
--- a/spec2modules.py
+++ b/spec2modules.py
@@ -68,7 +68,8 @@ def main() -> None:
     if args.diff:
         rtemsspec.content.Content.write = _diff  # type: ignore
     config = rtemsspec.util.load_config("config.yml")
-    item_cache = rtemsspec.items.ItemCache(config["spec"])
+    item_cache = rtemsspec.items.ItemCache(
+        config["spec"], is_item_enabled=rtemsspec.items.item_is_enabled)
     rtemsspec.validation.generate(config["validation"], item_cache,
                                   args.targets)
 
diff --git a/specview.py b/specview.py
index 3f820e2d..7bfe4f9c 100755
--- a/specview.py
+++ b/specview.py
@@ -141,13 +141,10 @@ def _view_interface_placment(item: Item, level: int,
 
 def _view(item: Item, level: int, role: Optional[str],
           validated_filter: str) -> None:
-    if not item.enabled:
-        return
     if not _visit_item(item, level, role, validated_filter):
         return
     for child in item.children("validation"):
-        if child.enabled:
-            _visit_item(child, level + 1, "validation", validated_filter)
+        _visit_item(child, level + 1, "validation", validated_filter)
     _view_interface_placment(item, level + 1, validated_filter)
     for link in item.links_to_children(_CHILD_ROLES):
         _view(link.item, level + 1, link.role, validated_filter)
@@ -200,9 +197,8 @@ def _validate(item: Item) -> bool:
     count = 0
     for link in itertools.chain(item.links_to_children(_VALIDATION_ROLES),
                                 item.links_to_parents(_PARENT_ROLES)):
-        if link.item.enabled:
-            validated = _validate(link.item) and validated
-            count += 1
+        validated = _validate(link.item) and validated
+        count += 1
     pre_qualified = is_pre_qualified(item)
     item["_pre_qualified"] = pre_qualified
     if count == 0:
@@ -212,14 +208,11 @@ def _validate(item: Item) -> bool:
 
 
 def _validation_count(item: Item) -> int:
-    return len(
-        list(child for child in item.children("validation") if child.enabled))
+    return len(list(child for child in item.children("validation")))
 
 
 def _no_validation(item: Item, path: List[str]) -> List[str]:
     path_2 = path + [item.uid]
-    if not item.enabled:
-        return path_2[:-1]
     leaf = _validation_count(item) == 0
     for child in item.children(_CHILD_ROLES):
         path_2 = _no_validation(child, path_2)



More information about the vc mailing list