Skip to content

Commit

Permalink
[general] Avoid default mutable arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jun 6, 2017
1 parent 117bab1 commit 5ddc201
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
13 changes: 7 additions & 6 deletions netjsonconfig/backends/base/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class BaseBackend(object):
schema = None
FILE_SECTION_DELIMITER = '# ---------- files ---------- #'
intermediate_data = None
list_identifiers = []

def __init__(self, config, templates=[], context={}):
def __init__(self, config, templates=None, context=None):
"""
:param config: ``dict`` containing valid **NetJSON DeviceConfiguration**
:param templates: ``list`` containing **NetJSON** dictionaries that will be
Expand Down Expand Up @@ -54,17 +55,17 @@ def _merge_config(self, config, templates):
"""
Merges config with templates
"""
if not templates:
return config
# type check
if not isinstance(templates, list):
raise TypeError('templates argument must be an instance of list')
# merge any present template with main configuration
# merge templates with main configuration
base_config = {}

for template in templates:
template = self._load(template)
base_config = merge_config(base_config, template)
base_config = merge_config(base_config, self._load(template), self.list_identifiers)
if base_config:
return merge_config(base_config, config)
return merge_config(base_config, config, self.list_identifiers)
return config

def _evaluate_vars(self, config, context):
Expand Down
3 changes: 2 additions & 1 deletion netjsonconfig/backends/openwisp/openwisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def _sanitize_radios(self):
for radio in self.config.get('radios', []):
radio.setdefault('disabled', False)

def _render_template(self, template, context={}):
def _render_template(self, template, context=None):
openwisp_env = Environment(loader=PackageLoader(self.__module__, 'templates'),
trim_blocks=True)
template = openwisp_env.get_template(template)
context = context or {}
return template.render(**context)

def _add_unique_file(self, item):
Expand Down
1 change: 1 addition & 0 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class OpenWrt(BaseBackend):
converters.Default,
]
renderer = OpenWrtRenderer
list_identifiers = ['name', 'config_value', 'id']

def _generate_contents(self, tar):
"""
Expand Down
13 changes: 8 additions & 5 deletions netjsonconfig/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import six


def merge_config(template, config):
def merge_config(template, config, list_identifiers=None):
"""
Merges ``config`` on top of ``template``.
Expand All @@ -19,6 +19,7 @@ def merge_config(template, config):
:param template: template ``dict``
:param config: config ``dict``
:param list_identifiers: ``list`` or ``None``
:returns: merged ``dict``
"""
result = template.copy()
Expand All @@ -27,13 +28,13 @@ def merge_config(template, config):
node = result.get(key, {})
result[key] = merge_config(node, value)
elif isinstance(value, list) and isinstance(result.get(key), list):
result[key] = merge_list(result[key], value)
result[key] = merge_list(result[key], value, list_identifiers)
else:
result[key] = value
return result


def merge_list(list1, list2, identifiers=['name', 'config_value', 'id']):
def merge_list(list1, list2, identifiers=None):
"""
Merges ``list2`` on top of ``list1``.
Expand All @@ -43,10 +44,12 @@ def merge_list(list1, list2, identifiers=['name', 'config_value', 'id']):
The remaining elements will be summed in order to create a list
which contains elements of both lists.
:param list1: list from template
:param list2: list from config
:param list1: ``list`` from template
:param list2: ``list from config
:param identifiers: ``list`` or ``None``
:returns: merged ``list``
"""
identifiers = identifiers or []
dict_map = {'list1': OrderedDict(), 'list2': OrderedDict()}
counter = 1
for list_ in [list1, list2]:
Expand Down
2 changes: 1 addition & 1 deletion tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_templates_type_error(self):
}
}
with self.assertRaises(TypeError):
OpenWrt(config, templates={})
OpenWrt(config, templates={'a': 'a'})

def test_templates_config_error(self):
config = {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_evaluate_vars_one_char(self):
def test_merge_list_override(self):
template = [{"name": "test1", "tx": 1}]
config = [{"name": "test1", "tx": 2}]
result = merge_list(template, config)
result = merge_list(template, config, ['name'])
self.assertEqual(result, config)

def test_merge_list_union_and_override(self):
Expand All @@ -182,7 +182,7 @@ def test_merge_list_union_and_override(self):
{"id": "test1", "a": "0", "b": "b"},
{"id": "test2", "c": "c"}
]
result = merge_list(template, config)
result = merge_list(template, config, ['id'])
self.assertEqual(result, [
{"id": "test1", "a": "0", "b": "b"},
{"id": "test2", "c": "c"}
Expand All @@ -191,7 +191,7 @@ def test_merge_list_union_and_override(self):
def test_merge_list_config_value(self):
template = [{"config_value": "test1", "tx": 1}]
config = [{"config_value": "test1", "tx": 2}]
result = merge_list(template, config)
result = merge_list(template, config, ['config_value'])
self.assertEqual(result, config)

def test_get_copy_default(self):
Expand Down

0 comments on commit 5ddc201

Please sign in to comment.