From abecba2332d6b6756cf83ba2d87fcbf3640f0c18 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sun, 27 Oct 2019 16:29:11 +0100 Subject: Replace the try and wait for segfault approach with explicit inSession check. --- python/vyos/config.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'python/vyos/config.py') diff --git a/python/vyos/config.py b/python/vyos/config.py index 3a340b2da..ee1adb289 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -104,12 +104,10 @@ class Config(object): running_config_text = f.read() # Session config ("active") only exists in conf mode. - # Trying to obtain it from op mode will cause a fatal cli-shell-api error. - # If that happens, we assume that a script is running from op mode and use the running config - # for the "session config" variable as well. - try: + # In op mode, we'll just use the same running config for both active and session configs. + if self.in_session(): session_config_text = self._run([self._cli_shell_api, '--show-working-only', '--show-show-defaults', 'showConfig']) - except VyOSError: + else: session_config_text = running_config_text self._session_config = vyos.configtree.ConfigTree(session_config_text) -- cgit v1.2.3 From a5d800400aaf9d1df61feb79c3626d37d0a3015d Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sun, 27 Oct 2019 16:38:55 +0100 Subject: Allow list arguments in the vyos.config show_config() function. --- python/vyos/config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'python/vyos/config.py') diff --git a/python/vyos/config.py b/python/vyos/config.py index ee1adb289..5c2fc7285 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -222,7 +222,7 @@ class Config(object): except VyOSError: return False - def show_config(self, path='', default=None): + def show_config(self, path=[], default=None): """ Args: path (str): Configuration tree path, or empty @@ -231,6 +231,8 @@ class Config(object): Returns: str: working configuration """ + if isinstance(path, list): + path = " ".join(path) try: out = self._run(self._make_command('showConfig', path)) return out -- cgit v1.2.3 From aac75107bc9988ecb599265da029827ff09bf713 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Mon, 28 Oct 2019 15:01:52 +0100 Subject: Add a function for retrieving config dicts. --- python/vyos/config.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'python/vyos/config.py') diff --git a/python/vyos/config.py b/python/vyos/config.py index 5c2fc7285..13b2c107e 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -65,6 +65,7 @@ In operational mode, all functions return values from the running config. import os import re +import json import subprocess import vyos.configtree @@ -225,7 +226,7 @@ class Config(object): def show_config(self, path=[], default=None): """ Args: - path (str): Configuration tree path, or empty + path (str list): Configuration tree path, or empty default (str): Default value to return Returns: @@ -239,6 +240,16 @@ class Config(object): except VyOSError: return(default) + def get_config_dict(self, path=[], effective=False): + """ + Args: path (str list): Configuration tree path, can be empty + Returns: a dict representation of the config + """ + res = self.show_config(self._make_path(path)) + config_tree = vyos.configtree.ConfigTree(res) + config_dict = json.loads(config_tree.to_json()) + return config_dict + def is_multi(self, path): """ Args: -- cgit v1.2.3