Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][backward-conversion] Recognize templates #100 #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ target/

# editors
*.komodoproject
.vscode

# other
*.DS_Store*
.__*
__DEV
Pipfile*
48 changes: 48 additions & 0 deletions netjsonconfig/backends/base/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]