Skip to content

Commit f52a7b1

Browse files
Bastian-KrauseEmantor
authored andcommitted
config: allow types other than string in get_target_option()/set_target_option()
There is no reason to limit options to strings. YAML supports scalars, lists and dictionaries. As there is no way to write the options set during runtime back to YAML, so there is no reason to limit the types for the set_option() method either. Lift this limitation, so YAML-supported data types can be retrieved and all data types can be set. Note that this changes previous behavior, because now options are not converted to strings anymore. Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent caf895a commit f52a7b1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

labgrid/config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ def get_target_option(self, target, name, default=None):
176176
Args:
177177
target (str): name of the target
178178
name (str): name of the option
179-
default (str): A default parameter in case the option can not be
179+
default (any): A default parameter in case the option can not be
180180
found
181181
182182
Returns:
183-
str: value of the option or default parameter
183+
any: value of the option or default parameter
184184
185185
Raises:
186186
KeyError: if the requested key can not be found in the
@@ -191,7 +191,7 @@ def get_target_option(self, target, name, default=None):
191191
raise KeyError(f"No target '{target}' found in configuration")
192192

193193
try:
194-
return str(self.data['targets'][target]['options'][name])
194+
return self.data['targets'][target]['options'][name]
195195
except (KeyError, TypeError):
196196
# Empty target declarations become None in the target dict, hence
197197
# TypeError when we try to subscript it.
@@ -206,15 +206,14 @@ def set_target_option(self, target, name, value):
206206
Args:
207207
target (str): name of the target
208208
name (str): name of the option
209-
value (str): the new value
209+
value (any): the new value
210210
211211
Raises:
212212
KeyError: if the requested target can not be found in the
213213
configuration
214214
"""
215215
assert isinstance(target, str)
216216
assert isinstance(name, str)
217-
assert isinstance(value, str)
218217

219218
if target not in self.data['targets']:
220219
raise KeyError(f"No target '{target}' found in configuration")

tests/test_config.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import OrderedDict
2+
13
import pytest
24

35
from labgrid.config import Config
@@ -10,19 +12,32 @@ def test_get_target_option(tmpdir):
1012
targets:
1113
main:
1214
options:
13-
foo: bar
14-
spam: eggs
15+
str: test
16+
list: [1, 2, 3]
17+
dict:
18+
a: 1
19+
b: 2
20+
bool: False
21+
int: 0x20
22+
float: 3.14
23+
none: null
1524
"""
1625
)
1726
c = Config(str(p))
18-
assert c.get_target_option("main", "spam") == "eggs"
27+
assert c.get_target_option("main", "str") == "test"
28+
assert c.get_target_option("main", "list") == [1, 2, 3]
29+
assert c.get_target_option("main", "dict") == OrderedDict([('a', 1), ('b', 2)])
30+
assert c.get_target_option("main", "bool") is False
31+
assert c.get_target_option("main", "int") == 0x20
32+
assert c.get_target_option("main", "float") == 3.14
33+
assert c.get_target_option("main", "none") is None
1934

2035
with pytest.raises(KeyError) as err:
2136
c.get_target_option("main", "blah")
2237
assert "No option" in str(err)
2338

2439
with pytest.raises(KeyError) as err:
25-
c.get_target_option("nonexist", "spam")
40+
c.get_target_option("nonexist", "str")
2641
assert "No target" in str(err)
2742

2843
def test_set_target_option(tmpdir):
@@ -42,6 +57,10 @@ def test_set_target_option(tmpdir):
4257
c.set_target_option("main", "spam", "eggs")
4358
assert c.get_target_option("main", "spam") == "eggs"
4459

60+
obj = object()
61+
c.set_target_option("main", "obj", obj)
62+
assert c.get_target_option("main", "obj") is obj
63+
4564
def test_template(tmpdir):
4665
p = tmpdir.join("config.yaml")
4766
p.write(

0 commit comments

Comments
 (0)