Commit 4b9ab0a6 authored by Karel Koci's avatar Karel Koci 🤘

euci: implement get_t and set_t method

These are intended to be used as smart wrappers on top of get_* and
set_* methods. In case of get_t method you can specify type you are
expecting like a python type (str, bool and int). In case of set_t is
type autodetected from provided value.
parent a20c5a09
Pipeline #48728 passed with stage
in 17 seconds
......@@ -22,7 +22,7 @@
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from uci import Uci, UciExceptionNotFound
from uci import Uci, UciException, UciExceptionNotFound
class EUci(Uci):
......@@ -81,7 +81,7 @@ class EUci(Uci):
def set_boolean(self, *args):
"""Sets boolean value to given UCI config.
"""
self.set(*(args[:-1]), self.__BOOLEAN_TRUE if args[-1] else self.__BOOLEAN_FALSE)
self.set(*args[:-1], self.__BOOLEAN_TRUE if args[-1] else self.__BOOLEAN_FALSE)
def get_integer(self, *args, **kwargs):
"""Returns given UCI config as an integer.
......@@ -102,4 +102,53 @@ class EUci(Uci):
def set_integer(self, *args):
"""Sets integer to given UCI config.
"""
self.set(*(args[:-1]), str(args[-1]))
self.set(*args[:-1], str(args[-1]))
def get_t(self, dtype, *args, **kwargs):
"""Returns given UCI config typed as Python type in dtype.
This is wrapper on top of other EUci get_* methods. Note that this is
not magic. It only provides you with convinient way to use Python type
to call specific get_* method.
Returns value of requested type.
All exceptions it can raise depends on requested type but it also
raises one additional exception: EUciExceptionUnsupportedType
"""
tpmethods = {
str: self.get,
bool: self.get_boolean,
int: self.get_integer,
}
if dtype not in tpmethods:
raise EUciExceptionUnsupportedType(dtype)
return tpmethods[dtype](*args, **kwargs)
def set_t(self, *args, **kwargs):
"""Sets given value to given UCI config while detecting type.
Only supported types are those supported by set_* methods. If provided
value has type which is not supported or recognized then it is
converted to string. To ensure that you are setting correct type you
should always type it with call. That is for example:
set_t("foo", "fee", "faa", bool(value))
"""
tpmethods = {
bool: self.set_boolean,
int: self.set_integer,
}
dtype = type(args[-1])
if dtype in tpmethods:
tpmethods[dtype](*args, **kwargs)
else:
self.set(*args[:-1], str(args[-1]), **kwargs)
class EUciException(UciException):
"""Generic top level exception for EUci.
"""
class EUciExceptionUnsupportedType(EUciException):
"""Requested type is not supported by EUci.
"""
def __init__(self, dtype):
super().__init__("Requested type is not supported by EUci: {}".format(dtype))
......@@ -100,3 +100,81 @@ config testing 'testing'
with euci.EUci(confdir=tmpdir.strpath) as u:
assert not u.get_boolean('test', 'testing', 'one')
assert u.get_boolean('test', 'testing', 'two')
def test_get_t_string(tmpdir):
'Test get_t method for str type'
tmpdir.join('test').write("""
config str 'str'
option foo 'value'
""")
u = euci.EUci(confdir=tmpdir.strpath)
v = u.get_t(str, 'test', 'str', 'foo')
assert type(v) == str
assert v == 'value'
def test_get_t_boolean(tmpdir):
'Test get_t method for boolean type'
tmpdir.join('test').write("""
config bool 'bool'
option true 'true'
option false 'false'
""")
u = euci.EUci(confdir=tmpdir.strpath)
v1 = u.get_t(bool, 'test', 'bool', 'true')
assert type(v1) == bool
assert v1
v2 = u.get_t(bool, 'test', 'bool', 'false')
assert type(v2) == bool
assert not v2
def test_get_t_int(tmpdir):
'Test get_t method for int type'
tmpdir.join('test').write("""
config integer 'integer'
option plus '42'
option minus '-42'
""")
u = euci.EUci(confdir=tmpdir.strpath)
v1 = u.get_t(int, 'test', 'integer', 'plus')
assert v1 == 42
assert type(v1) == int
v2 = u.get_t(int, 'test', 'integer', 'minus')
assert v2 == -42
assert type(v2) == int
def test_set_t(tmpdir):
'Test set_integer'
tmpdir.join('test').write("""
config testing 'testing'
""")
u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
u.set_t('test', 'testing', 'foo', 'value')
assert u.get('test', 'testing', 'foo') == 'value'
def test_set_t_boolean(tmpdir):
'Test set_t method for bool type'
tmpdir.join('test').write("""
config testing 'testing'
""")
u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
u.set_t('test', 'testing', 'true', True)
u.set_t('test', 'testing', 'false', False)
assert u.get('test', 'testing', 'true') == '1'
assert u.get('test', 'testing', 'false') == '0'
def test_set_t_integer(tmpdir):
'Test set_integer'
tmpdir.join('test').write("""
config testing 'testing'
""")
u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
u.set_t('test', 'testing', 'plus', 42)
u.set_t('test', 'testing', 'minus', -42)
assert u.get('test', 'testing', 'plus') == '42'
assert u.get('test', 'testing', 'minus') == '-42'
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