Skip to content

Commit

Permalink
[backward-conversion] Recognize templates #100
Browse files Browse the repository at this point in the history
Closes #100
  • Loading branch information
atb00ker committed Jul 16, 2020
1 parent dfd7c78 commit 57856c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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]

0 comments on commit 57856c6

Please sign in to comment.