Get set generic
This creates all powerful get
and set
methods. The idea behind this is to protect input from configuration. Uci does not provide us with types so if we want to be sure that we are working with correct type (which can be pretty funny like iteration over tuple vs. string) we have to check that. These new all mighty and ultimate methods are intended to do so.
This integrates all previously defined functions to single one.
Merge request reports
Activity
added Feature label
@shenek cam you please also go trough this new API and make sure that it does not break anything for you? Or potentially if you see something obviously wrong..
added 1 commit
- d9ec725f - euci: improve exception when invalid keyword argument is passed
126 according to that. Supported types are: str, bool and int. 127 If provided value is not of any of these types then it is converted to 128 string. 129 130 It is suggested to always explicitly type last positional argument to 131 ensure correct type. That is in case of boolean for example: 132 set("foo", "fee", "faa", bool(value)) 63 133 """ 64 value = self.get(*args, **kwargs) 65 if value.lower() not in self.__BOOLEAN_VALUES: 66 raise ValueError 67 return self.__BOOLEAN_VALUES[value.lower()] 134 dtype = type(args[-1]) 135 if dtype == tuple or dtype == list: 136 # We consider first value as authoritative for type 137 dtype = type(args[-1][0]) if args[-1] else str That
args[-1]
is expanded to tuple/list that is accessed inargs[-1][0]
so it is effectively doingbool(args[-1])
which means: is tuple/list empty?
If it is empty then we fallback on string type. It is effectively not used later but I did it for consistency sake.And yes we expect that
args[-1]
is always present.
57 99 58 def get_boolean(self, *args, **kwargs): 59 """Returns given UCI config as a boolean. 60 Value '0', 'no', 'off', 'false' or 'disabled' is returned as False. 61 Value '1' , 'yes', 'on', 'true' or 'enabled' is returned as True. 62 ValueError is raised on any other value. 100 if type(values) == tuple: 101 result = tuple((self._get(str(value), dtype) for value in values)) 102 else: 103 result = self._get(str(values), dtype) 104 if 'list' in kwargs: 105 if (type(result) == tuple) == bool(kwargs['list']): 106 return result 107 if kwargs['list']: 108 return (result,) 109 return result[0] It is complicated condition I agree but it is I think better than duplicating code.
Edited by Karel Koci
assigned to @kkoci
assigned to @mmatejek
assigned to @kkoci