Verified Commit 4a110faa authored by Karel Koci's avatar Karel Koci 🤘

euci: add option to ensure that tupple is returned

Idea of this is that user can set to configuration file instead of list
option but we want to make sure that even if he does that that we get
tuple of values. This is exactly for that case. Setting list to True
ensures that get never returns anything else then list. Where on the
other hand setting list to False causes it to never return tuple even if
value is lists (in which case first found list is returned).
parent c1ddf063
Pipeline #48765 passed with stage
in 18 seconds
......@@ -70,13 +70,19 @@ class EUci(Uci):
dtype: data type to be returned. Currently supported are: str, bool and
int. If you don't specify this then it defaults to str. If value
cannot be converted to specified type then it raises ValueError.
list: bool setting if option is expected to be list. This ensures that
this method always returns tuple or on the other hand never
returns one.
default: default value to be returned instead of raising
UciExceptionNotFound.
Note that dtype and list are considered only if at least "section" is
provided.
When requested value is not found then this raises UciExceptionNotFound.
ValueError is raised in case of value that can't be converted to dtype.
"""
kwdiff = set(kwargs).difference({'default'})
kwdiff = set(kwargs).difference({'default', 'list'})
if kwdiff:
raise TypeError("'{}' is an invalid keyword argument for this function".format(kwdiff))
......@@ -91,8 +97,16 @@ class EUci(Uci):
return values
if type(values) == tuple:
return tuple((self._get(str(value), dtype) for value in values))
return self._get(str(values), dtype)
result = tuple((self._get(str(value), dtype) for value in values))
else:
result = self._get(str(values), dtype)
if 'list' in kwargs:
if (type(result) == tuple) == bool(kwargs['list']):
return result
if kwargs['list']:
return (result,)
return result[0]
return result
def _set_value(self, value, dtype):
if dtype == bool:
......
......@@ -157,3 +157,18 @@ config testing 'testing'
with euci.EUci(confdir=tmpdir.strpath) as u:
assert not u.get('test', 'testing', 'one', dtype=bool)
assert u.get('test', 'testing', 'two', dtype=bool)
def test_get_list(tmpdir):
'Test get with list'
tmpdir.join('test').write("""
config testing 'testing'
option option '0'
list list '0'
list list '1'
""")
u = euci.EUci(confdir=tmpdir.strpath)
assert u.get('test', 'testing', 'option', dtype=bool, list=True) == (False,)
assert not u.get('test', 'testing', 'option', dtype=bool, list=False)
assert u.get('test', 'testing', 'list', dtype=bool, list=True) == (False, True)
assert not u.get('test', 'testing', 'list', dtype=bool, list=False)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment