diff options
Diffstat (limited to 'python/vyos/config.py')
-rw-r--r-- | python/vyos/config.py | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index 75055a603..54cb518c3 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -97,12 +97,12 @@ class Config(object): self.__session_env = None # Running config can be obtained either from op or conf mode, it always succeeds - # (if config system is initialized at all). + # once the config system is initialized during boot; + # before initialization, set to empty string if os.path.isfile('/tmp/vyos-config-status'): running_config_text = self._run([self._cli_shell_api, '--show-active-only', '--show-show-defaults', '--show-ignore-edit', 'showConfig']) else: - with open('/opt/vyatta/etc/config/config.boot') as f: - running_config_text = f.read() + running_config_text = '' # Session config ("active") only exists in conf mode. # In op mode, we'll just use the same running config for both active and session configs. @@ -112,7 +112,10 @@ class Config(object): session_config_text = running_config_text self._session_config = vyos.configtree.ConfigTree(session_config_text) - self._running_config = vyos.configtree.ConfigTree(running_config_text) + if running_config_text: + self._running_config = vyos.configtree.ConfigTree(running_config_text) + else: + self._running_config = None def _make_command(self, op, path): args = path.split() @@ -155,7 +158,7 @@ class Config(object): ``exists("system name-server"`` without ``set_level``. Args: - path (str): relative config path + path (str|list): relative config path """ # Make sure there's always a space between default path (level) # and path supplied as method argument @@ -166,7 +169,7 @@ class Config(object): else: self._level = [] elif isinstance(path, list): - self._level = path + self._level = path.copy() else: raise TypeError("Level path must be either a whitespace-separated string or a list") @@ -177,7 +180,7 @@ class Config(object): Returns: str: current edit level """ - return(self._level) + return(self._level.copy()) def exists(self, path): """ @@ -278,8 +281,12 @@ class Config(object): Returns: a dict representation of the config """ res = self.show_config(self._make_path(path), effective=effective) - config_tree = vyos.configtree.ConfigTree(res) - config_dict = json.loads(config_tree.to_json()) + if res: + config_tree = vyos.configtree.ConfigTree(res) + config_dict = json.loads(config_tree.to_json()) + else: + config_dict = {} + return config_dict def is_multi(self, path): @@ -386,7 +393,7 @@ class Config(object): values = [] if not values: - return(default) + return(default.copy()) else: return(values) @@ -407,7 +414,7 @@ class Config(object): nodes = [] if not nodes: - return(default) + return(default.copy()) else: return(nodes) @@ -425,7 +432,10 @@ class Config(object): This function is safe to use in operational mode. In configuration mode, it ignores uncommited changes. """ - return(self._running_config.exists(self._make_path(path))) + if self._running_config: + return(self._running_config.exists(self._make_path(path))) + + return False def return_effective_value(self, path, default=None): """ @@ -438,9 +448,12 @@ class Config(object): Returns: str: Node value """ - try: - value = self._running_config.return_value(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._running_config: + try: + value = self._running_config.return_value(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + value = None + else: value = None if not value: @@ -448,7 +461,6 @@ class Config(object): else: return(value) - def return_effective_values(self, path, default=[]): """ Retrieve all values of a multi-value node in a running (effective) config @@ -459,13 +471,16 @@ class Config(object): Returns: str list: A list of values """ - try: - values = self._running_config.return_values(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._running_config: + try: + values = self._running_config.return_values(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + values = [] + else: values = [] if not values: - return(default) + return(default.copy()) else: return(values) @@ -482,12 +497,15 @@ class Config(object): Raises: VyOSError: if the node is not a tag node """ - try: - nodes = self._running_config.list_nodes(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._running_config: + try: + nodes = self._running_config.list_nodes(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + nodes = [] + else: nodes = [] if not nodes: - return(default) + return(default.copy()) else: return(nodes) |