[rtems-central commit] items: Add normalize_key_path()

Sebastian Huber sebh at rtems.org
Fri Sep 18 11:31:55 UTC 2020


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed Aug 12 07:58:45 2020 +0200

items: Add normalize_key_path()

---

 rtemsspec/items.py | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 2f4d80a..09ba2a1 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -130,6 +130,13 @@ def _get_value(ctx: ItemGetValueContext) -> Any:
     return value
 
 
+def normalize_key_path(key_path: str, prefix: str = "") -> str:
+    """ Normalizes the key path with an optional prefix path. """
+    if not os.path.isabs(key_path):
+        key_path = os.path.join(prefix, key_path)
+    return os.path.normpath(key_path)
+
+
 class Item:
     """ Objects of this class represent a specification item. """
     def __init__(self, item_cache: "ItemCache", uid: str, data: Any):
@@ -155,17 +162,16 @@ class Item:
         """
         return self._data.get(key, default)
 
-    def get_by_key_path(self,
-                        key_path: str,
-                        prefix: str = "",
-                        get_value: ItemGetValue = _get_value) -> Any:
-        """ Gets the attribute value corresponding to the key path. """
-        if not os.path.isabs(key_path):
-            key_path = os.path.join(prefix, key_path)
-        key_path = os.path.normpath(key_path)
+    def get_by_normalized_key_path(
+            self,
+            normalized_key_path: str,
+            get_value: ItemGetValue = _get_value) -> Any:
+        """
+        Gets the attribute value corresponding to the normalized key path.
+        """
         path = "/"
         value = self._data
-        for key in key_path.strip("/").split("/"):
+        for key in normalized_key_path.strip("/").split("/"):
             parts = key.split("[")
             try:
                 index = int(parts[1].split("]")[0])
@@ -179,6 +185,14 @@ class Item:
             path = os.path.join(path, key)
         return value
 
+    def get_by_key_path(self,
+                        key_path: str,
+                        prefix: str = "",
+                        get_value: ItemGetValue = _get_value) -> Any:
+        """ Gets the attribute value corresponding to the key path. """
+        return self.get_by_normalized_key_path(
+            normalize_key_path(key_path, prefix), get_value)
+
     @property
     def uid(self) -> str:
         """ Returns the UID of the item. """



More information about the vc mailing list