summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2022-04-11 15:38:19 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2022-04-11 15:47:29 +0200
commit6f9e26c6a0aa9d267cf94e82c898eb850d96f20b (patch)
treedc228faba94f88ab9e694b0ed05a542bd3b6242c
parentspec: Clarify scheduler of created task (diff)
downloadrtems-central-6f9e26c6a0aa9d267cf94e82c898eb850d96f20b.tar.bz2
items: Add ItemGetValueContext.arg()
-rw-r--r--rtemsspec/items.py9
-rw-r--r--rtemsspec/tests/test_items_item.py11
2 files changed, 19 insertions, 1 deletions
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"