From 57856c63edf14526e2454165f575e0f9437d70bd 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 | 2 ++ netjsonconfig/backends/base/backend.py | 48 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/.gitignore b/.gitignore index 766c20f81..8df6344c3 100644 --- a/.gitignore +++ b/.gitignore @@ -54,8 +54,10 @@ target/ # editors *.komodoproject +.vscode # other *.DS_Store* .__* __DEV +Pipfile* diff --git a/netjsonconfig/backends/base/backend.py b/netjsonconfig/backends/base/backend.py index b9a435ad2..b92ab268d 100644 --- a/netjsonconfig/backends/base/backend.py +++ b/netjsonconfig/backends/base/backend.py @@ -324,3 +324,51 @@ def __restore_intermediate_data(self): del self.intermediate_data self.intermediate_data = self._intermediate_copy del self._intermediate_copy + + def distinct_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 + """ + + templates = self._merge_config({}, templates) + config_to_send = deepcopy(self.config) + + for section in self.config: + template_section = templates.get(section, None) + + if isinstance(template_section, dict): + for element in template_section: + self._distinct_native_dict( + template_section, element, section, config_to_send + ) + # if empty, delete section dict + if not config_to_send[section]: + del config_to_send[section] + + elif isinstance(template_section, list): + for element in template_section: + if element in self.config[section]: + config_to_send[section].remove(element) + # if empty, delete section list + if not config_to_send[section]: + del config_to_send[section] + + return config_to_send + + def _distinct_native_dict(self, template_section, element, section, config_to_send): + if element in self.config[section].keys(): + t_element = template_section[element] + if isinstance(t_element, list): + for item in t_element: + if item in self.config[section][element]: + config_to_send[section][element].remove(item) + # if empty, delete section list + if not config_to_send[section][element]: + del config_to_send[section][element] + elif t_element == self.config[section][element]: + del config_to_send[section][element]