summaryrefslogtreecommitdiff
path: root/python/vyos/config.py
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2017-09-08 04:06:29 +0200
committerDaniil Baturin <daniil@baturin.org>2017-09-08 04:06:29 +0200
commit4f47f37280d774fb7ad935616c8391d0689a1f36 (patch)
treeafa2412d86da8bf8f41c5881fcee137ffcd7e825 /python/vyos/config.py
parent244da328e258921dd8d5e0bd773e622e252d8580 (diff)
downloadvyos-1x-4f47f37280d774fb7ad935616c8391d0689a1f36.tar.gz
vyos-1x-4f47f37280d774fb7ad935616c8391d0689a1f36.zip
Make the vyos.config.Config's return_value(s) and list_node functions behave like their Perl counterparts.
Most scripts used to do something like my $foo = $config->returnValue("system foo"); if !defined($foo) { $foo = $defaultFooValue; } In most cases values do not exist because they are optional and simply not set. In a substantial minority of cases they don't exist but are mandatory, in this case it's probably a good practice to check if it exists first. In rare but frustrating cases returnValue returns undef because the path is wrong. It was tempting but as it turns out impractical to force the user to handle every undefined value as an error, but the cost of wrapping every return_value call in a try/except block is too high. Instead we should facilitate the most common case. For this, those functions now support an optional named argument default=None so an optional value with a sensible default can be handled like $foo = config.return_value("system foo", default="bar")
Diffstat (limited to 'python/vyos/config.py')
-rw-r--r--python/vyos/config.py12
1 files changed, 6 insertions, 6 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py
index 85dd49e68..edee6b33e 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -95,7 +95,7 @@ class Config(object):
except VyOSError:
return False
- def return_value(self, path):
+ def return_value(self, path, default=None):
full_path = self._level + path
if self.is_multi(path):
raise VyOSError("Cannot use return_value on multi node: {0}".format(full_path))
@@ -106,9 +106,9 @@ class Config(object):
out = self._run(self._make_command('returnValue', full_path))
return out
except VyOSError:
- raise VyOSError("Path doesn't exist: {0}".format(full_path))
+ return(default)
- def return_values(self, path):
+ def return_values(self, path, default=[]):
full_path = self._level + path
if not self.is_multi(path):
raise VyOSError("Cannot use return_values on non-multi node: {0}".format(full_path))
@@ -119,9 +119,9 @@ class Config(object):
out = self._run(self._make_command('returnValues', full_path))
return out
except VyOSError:
- raise VyOSError("Path doesn't exist: {0}".format(full_path))
+ return(default)
- def list_nodes(self, path):
+ def list_nodes(self, path, default=[]):
full_path = self._level + path
if self.is_tag(path):
try:
@@ -129,6 +129,6 @@ class Config(object):
values = out.split()
return list(map(lambda x: re.sub(r'^\'(.*)\'$', r'\1',x), values))
except VyOSError:
- raise VyOSError("Path doesn't exist: {0}".format(full_path))
+ return(default)
else:
raise VyOSError("Cannot use list_nodes on a non-tag node: {0}".format(full_path))