diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-08-12 07:58:45 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-09-18 13:27:36 +0200 |
commit | 17858d80db83fc3205b23fe2fc40d190360a2e1b (patch) | |
tree | 7030d46521ec05bee2d94cbbb204fe90149e1ba8 | |
parent | content: Fix code coverage issues (diff) | |
download | rtems-central-17858d80db83fc3205b23fe2fc40d190360a2e1b.tar.bz2 |
items: Add normalize_key_path()
-rw-r--r-- | rtemsspec/items.py | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/rtemsspec/items.py b/rtemsspec/items.py index 2f4d80ac..09ba2a19 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. """ |