euci: add default values to get methods

If key doesn't exist in uci, default value will be returned
parent cc8b4e53
Pipeline #48154 failed with stages
in 18 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
from uci import Uci, UciExceptionNotFound
class EUci(Uci):
......@@ -46,16 +46,29 @@ class EUci(Uci):
def __init__(self, *args, **kwargs):
super(EUci, self).__init__(*args, **kwargs)
def get_boolean(self, *args):
def get(self, *args, default=None):
try:
return super().get(*args)
except UciExceptionNotFound:
if default is not None:
return default
raise
def get_boolean(self, *args, default=None):
"""Returns given UCI config as a boolean.
Value '0', 'no', 'off', 'false' or 'disabled' is returned as False.
Value '1' , 'yes', 'on', 'true' or 'enabled' is returned as True.
ValueError is raised on any other value.
"""
value = self.get(*args)
if value.lower() in self.__BOOLEAN_VALUES:
return self.__BOOLEAN_VALUES[value.lower()]
raise ValueError
try:
value = self.get(*args)
except UciExceptionNotFound:
if default is not None:
return default
raise
if value.lower() not in self.__BOOLEAN_VALUES:
raise ValueError
return self.__BOOLEAN_VALUES[value.lower()]
def set_boolean(self, *args):
"""Sets boolean value to given UCI config.
......@@ -64,11 +77,16 @@ class EUci(Uci):
nargs[-1] = self.__BOOLEAN_TRUE if nargs[-1] else self.__BOOLEAN_FALSE
self.set(*nargs)
def get_integer(self, *args):
def get_integer(self, *args, default=None):
"""Returns given UCI config as an integer.
Raises ValueError if config value can't be converted to int.
"""
return int(self.get(*args))
try:
return int(self.get(*args))
except UciExceptionNotFound:
if default is not None:
return default
raise
def set_integer(self, *args):
"""Sets integer to given UCI config.
......
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