Skip to content

Commit aa8b485

Browse files
committed
Added utils.evaluate_vars function
1 parent 2c1e72e commit aa8b485

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

netjsonconfig/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import re
12
from collections import OrderedDict
23
from copy import deepcopy
34

5+
import six
6+
47

58
def merge_config(template, config):
69
"""
@@ -34,6 +37,32 @@ def sorted_dict(dictionary):
3437
return OrderedDict(sorted(dictionary.items()))
3538

3639

40+
var_pattern = re.compile(r'\{\{\s(\w*)\s\}\}')
41+
42+
43+
def evaluate_vars(data, context={}):
44+
"""
45+
Evaluates variables in ``data``
46+
47+
:param data: data structure containing variables, may be
48+
``str``, ``dict`` or ``list``
49+
:param context: ``dict`` containing variables
50+
:returns: modified data structure
51+
"""
52+
if isinstance(data, (dict, list)):
53+
if isinstance(data, dict):
54+
loop_items = data.items()
55+
elif isinstance(data, list):
56+
loop_items = enumerate(data)
57+
for key, value in loop_items:
58+
data[key] = evaluate_vars(value, context)
59+
elif isinstance(data, six.string_types):
60+
for var in var_pattern.findall(data):
61+
if var in context:
62+
data = data.replace('{{ %s }}' % var, context[var])
63+
return data
64+
65+
3766
class _TabsMixin(object): # pragma: nocover
3867
"""
3968
mixin that adds _tabs method to test classes

tests/test_utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from netjsonconfig.utils import merge_config
3+
from netjsonconfig.utils import evaluate_vars, merge_config
44

55

66
class TestUtils(unittest.TestCase):
@@ -109,3 +109,27 @@ def test_merge_list_of_dicts_unchanged(self):
109109
{"c": "original"}
110110
]
111111
})
112+
113+
def test_evaluate_vars(self):
114+
self.assertEqual(evaluate_vars('{{ tz }}', {'tz': 'UTC'}), 'UTC')
115+
self.assertEqual(evaluate_vars('tz: {{ tz }}', {'tz': 'UTC'}), 'tz: UTC')
116+
117+
def test_evaluate_vars_missing(self):
118+
self.assertEqual(evaluate_vars('{{ tz }}'), '{{ tz }}')
119+
self.assertEqual(evaluate_vars('tz: {{ tz }}'), 'tz: {{ tz }}')
120+
121+
def test_evaluate_vars_dict(self):
122+
val = evaluate_vars({'timezone': '{{ tz }}'}, {'tz': 'UTC'})
123+
self.assertEqual(val, {'timezone': 'UTC'})
124+
125+
def test_evaluate_vars_nested_dict(self):
126+
val = evaluate_vars({'general': {'timezone': '{{ tz }}'}}, {'tz': 'UTC'})
127+
self.assertEqual(val, {'general': {'timezone': 'UTC'}})
128+
129+
def test_evaluate_vars_list(self):
130+
val = evaluate_vars(['{{ a }}', '{{ b }}'], {'a': '1', 'b': '2'})
131+
self.assertEqual(val, ['1', '2'])
132+
133+
def test_evaluate_vars_list_in_dict(self):
134+
val = evaluate_vars({'l': ['{{ a }}', '{{ b }}']}, {'a': '1', 'b': '2'})
135+
self.assertEqual(val, {'l': ['1', '2']})

0 commit comments

Comments
 (0)