[rtems-central commit] items: Add ItemCache.set_types()

Sebastian Huber sebh at rtems.org
Mon Jun 26 19:05:41 UTC 2023


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Thu Jun  1 13:56:10 2023 +0200

items: Add ItemCache.set_types()

---

 rtemsspec/items.py                      | 17 ++++++++++++++---
 rtemsspec/tests/test_items_itemcache.py |  3 +++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index a3890f6f..34a37a8e 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -24,6 +24,8 @@
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
 
+# pylint: disable=too-many-lines
+
 from contextlib import contextmanager
 import base64
 import hashlib
@@ -786,7 +788,7 @@ class ItemCache(dict):
         self._enabled = config.get("enabled", [])
         self._is_enabled = is_item_enabled
         for item in self.values():
-            self._set_type(item)
+            self.set_type(item)
             item["_enabled"] = is_item_enabled(self._enabled, item)
         if config.get("resolve-proxies", False):
             self.resolve_proxies()
@@ -839,7 +841,7 @@ class ItemCache(dict):
         item = self._add_item(uid, data)
         item.init_parents(self)
         item.init_children()
-        self._set_type(item)
+        self.set_type(item)
         item["_enabled"] = self._is_enabled(self._enabled, item)
         return item
 
@@ -929,7 +931,8 @@ class ItemCache(dict):
         for item in sorted(self.values()):
             item.init_children()
 
-    def _set_type(self, item: Item) -> None:
+    def set_type(self, item: Item) -> None:
+        """ Sets the type of the item. """
         spec_type = self._root_type
         value = item.data
         path: List[str] = []
@@ -942,6 +945,14 @@ class ItemCache(dict):
         self._types.add(the_type)
         self.items_by_type.setdefault(the_type, []).append(item)
 
+    def set_types(self, root_type_uid: str) -> None:
+        """
+        Sets the root type of the cache and then sets the type of all items.
+        """
+        self._root_type = _gather_spec_refinements(self[root_type_uid])
+        for item in self.values():
+            self.set_type(item)
+
 
 class EmptyItemCache(ItemCache):
     """ This class provides a empty cache of specification items. """
diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py
index 2c24d669..864ef70a 100644
--- a/rtemsspec/tests/test_items_itemcache.py
+++ b/rtemsspec/tests/test_items_itemcache.py
@@ -130,6 +130,9 @@ def test_item_mapper(tmpdir):
     config["resolve-proxies"] = True
     item_cache = ItemCache(config)
     item = item_cache["/p"]
+    assert item.type == "other"
+    item_cache.set_types("/spec/root")
+    assert item.type == "other"
     base_mapper = ItemMapper(item)
     assert base_mapper["d/c:v"] == "c"
     mapper = ItemMapper(item)



More information about the vc mailing list