From dfb90d962daf8c0915395f42efba15e08acb1c5d Mon Sep 17 00:00:00 2001 From: Ajay Tripathi Date: Mon, 20 Apr 2020 01:58:44 +0530 Subject: [PATCH] [backward-conversion] Recognize templates #100 Closes #100 --- .gitignore | 4 ++++ netjsonconfig/backends/base/backend.py | 30 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/.gitignore b/.gitignore index 766c20f81..29ed7feea 100644 --- a/.gitignore +++ b/.gitignore @@ -54,8 +54,12 @@ target/ # editors *.komodoproject +.vscode # other *.DS_Store* .__* __DEV + +# TODO: Remove +test.py diff --git a/netjsonconfig/backends/base/backend.py b/netjsonconfig/backends/base/backend.py index 71336d117..e523d7da1 100644 --- a/netjsonconfig/backends/base/backend.py +++ b/netjsonconfig/backends/base/backend.py @@ -161,6 +161,36 @@ def json(self, validate=True, *args, **kwargs): config.update({'type': 'DeviceConfiguration'}) return json.dumps(config, *args, **kwargs) + def merge_native(self, templates=None): + """ + :param templates: ``list`` containing **NetJSON** configuration dictionaries + + returns a dict of configuration which contains configurations + that do not exist in given templates. + + :returns: dict + """ + config_to_send = {} + controller_config = self._merge_config({}, templates) + for section in controller_config: + device_section = self.config.get(section, None) + + if device_section and isinstance(device_section, list): + for element in device_section: + element_dict = dict(element) + if element_dict not in controller_config[section]: + config_to_send[section] = element_dict + + if device_section and isinstance(device_section, dict): + for config in controller_config[section]: + device_config = device_section.get(config, None) + if device_config and device_config != controller_config[section][config]: + if not config_to_send.get(section, None): + config_to_send[section] = {} + config_to_send[section][config] = device_config + + return config_to_send + def generate(self): """ Returns a ``BytesIO`` instance representing an in-memory tar.gz archive