[rtems-central commit] items: Add ItemGetValueContext.arg()

Sebastian Huber sebh at rtems.org
Mon Apr 11 13:48:27 UTC 2022


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Mon Apr 11 15:38:19 2022 +0200

items: Add ItemGetValueContext.arg()

---

 rtemsspec/items.py                 |  9 +++++++++
 rtemsspec/tests/test_items_item.py | 11 ++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index 969a62f4..cd6846d1 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -44,6 +44,15 @@ class ItemGetValueContext(NamedTuple):
     index: Any  # should be int, but this triggers a mypy error
     args: Optional[str]
 
+    def arg(self, name: str, value: Optional[str] = None) -> str:
+        """ Get argument value by name. """
+        args = dict(
+            kv.split("=")  # type: ignore
+            for kv in self.args.split(","))  # type: ignore
+        if value:
+            return args.get(name, value)
+        return args[name]
+
 
 ItemMap = Dict[str, "Item"]
 ItemGetValue = Callable[[ItemGetValueContext], Any]
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index 8ea44ee1..2c308506 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -27,7 +27,8 @@
 import os
 import pytest
 
-from rtemsspec.items import EmptyItemCache, Item, ItemCache, Link
+from rtemsspec.items import EmptyItemCache, Item, ItemCache, \
+    ItemGetValueContext, Link
 
 
 def test_to_abs_uid():
@@ -263,3 +264,11 @@ def test_save_and_load(tmpdir):
     item2.load()
     assert item2["k"] == "v"
     assert item.file == item_file
+
+
+def test_item_get_value_arg():
+    item = Item(EmptyItemCache(), "i", {})
+    ctx = ItemGetValueContext(item, "", None, "", 0, "k=v,k2=v2")
+    assert ctx.arg("k") == "v"
+    assert ctx.arg("k2") == "v2"
+    assert ctx.arg("k3", "v3") == "v3"



More information about the vc mailing list