From 3c8e1a20b55d5c143924357ab47eac4bbf67f33c Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Tue, 16 Feb 2016 17:43:10 +0000 Subject: [PATCH] Changed syntax of configuration variables #39 The proposed syntax ${var} can cause conflicts with shell scripts which can be included in the files section of the configuration dictionary. To avoid any ambiguity, the syntax has been reverted to the default jinja2 syntax. --- docs/source/general/basics.rst | 4 ++-- docs/source/general/commandline_utility.rst | 2 +- netjsonconfig/backends/base.py | 2 -- tests/openwrt/test_context.py | 16 ++++++++-------- tests/test_bin.py | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/docs/source/general/basics.rst b/docs/source/general/basics.rst index 2101b646e..f3db2031e 100644 --- a/docs/source/general/basics.rst +++ b/docs/source/general/basics.rst @@ -296,8 +296,8 @@ Here's an example from the real world: "url": "http://controller.examplewifiservice.com", "interval": "60", "verify_ssl": "1", - "uuid": "${UUID}", - "key": "${KEY}" + "uuid": "{{ UUID }}", + "key": "{{ KEY }}" } ] } diff --git a/docs/source/general/commandline_utility.rst b/docs/source/general/commandline_utility.rst index 2fc3f331d..5d94911de 100644 --- a/docs/source/general/commandline_utility.rst +++ b/docs/source/general/commandline_utility.rst @@ -76,7 +76,7 @@ know what this argument does please read ":ref:`configuration_variables`"), ther you can reference environment variables inside *configurations* and *templates*:: export HOSTNAME=freedom - netjsonconfig -c '{"general": { "hostname": "${HOSTNAME}" }}' -b openwrt -m render + netjsonconfig -c '{"general": { "hostname": "{{ HOSTNAME }}" }}' -b openwrt -m render You can also avoid using ``export`` and write everything in a one line command:: diff --git a/netjsonconfig/backends/base.py b/netjsonconfig/backends/base.py index cce47fa04..a6a66747a 100644 --- a/netjsonconfig/backends/base.py +++ b/netjsonconfig/backends/base.py @@ -13,8 +13,6 @@ def __init__(self, *args, **kwargs): self.filters = {} self.block_start_string = '{=##' self.block_end_string = '##=}' - self.variable_start_string = '${' - self.variable_end_string = '}' def call_binop(self, context, operator, left, right): raise SecurityError('binary operator {0} not allowed'.format(operator)) diff --git a/tests/openwrt/test_context.py b/tests/openwrt/test_context.py index 4e02a7b24..6fb49ba6d 100644 --- a/tests/openwrt/test_context.py +++ b/tests/openwrt/test_context.py @@ -13,8 +13,8 @@ class TestContext(unittest.TestCase, _TabsMixin): def test_config(self): config = { "general": { - "hostname": "${name}", - "description": "${desc}" + "hostname": "{{ name }}", + "description": "{{ desc }}" } } context = { @@ -27,8 +27,8 @@ def test_config(self): self.assertIn("option description 'test.context.desc'", output) def test_template(self): - config = {"general": {"hostname": "${name}"}} - template = {"general": {"description": "${desc}"}} + config = {"general": {"hostname": "{{ name }}"}} + template = {"general": {"description": "{{ desc }}"}} context = { "name": "test-context-name", "desc": "test.context.desc" @@ -39,21 +39,21 @@ def test_template(self): self.assertIn("option description 'test.context.desc'", output) def test_sandbox(self): - danger = """${ self.__repr__.__globals__.get('sys').version }""" + danger = """{{ self.__repr__.__globals__.get('sys').version }}""" config = {"general": {"description": danger}} o = OpenWrt(config, context={"description": "sandbox"}) with self.assertRaises(SecurityError): print(o.render()) def test_security_binop(self): - danger = """desc: ${10**10}""" + danger = """desc: {{ 10**10 }}""" config = {"general": {"description": danger}} o = OpenWrt(config, context={"description": "sandbox"}) with self.assertRaises(SecurityError): print(o.render()) def test_security_unop(self): - danger = """desc: ${ -10 }""" + danger = """desc: {{ -10 }}""" config = {"general": {"description": danger}} o = OpenWrt(config, context={"description": "sandbox"}) with self.assertRaises(SecurityError): @@ -67,7 +67,7 @@ def test_security_block(self): print(o.render()) def test_security_methods(self): - danger = """${ "{.__getitem__.__globals__[sys].version}".format(self) }""" + danger = """{{ "{.__getitem__.__globals__[sys].version}".format(self) }}""" config = {"general": {"description": danger}} o = OpenWrt(config, context={"description": "sandbox"}) with self.assertRaises(SecurityError): diff --git a/tests/test_bin.py b/tests/test_bin.py index 0e95c7d34..68f733d6a 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -123,8 +123,8 @@ def test_generate_redirection(self): tar.close() def test_context(self): - config = json.dumps({'general': {'description': '${DESC}'}}) + config = json.dumps({'general': {'description': '{{ DESC }}'}}) command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render".format(config) output = subprocess.check_output(command, shell=True).decode() - self.assertNotIn('${DESC}', output) + self.assertNotIn('{{ DESC }}', output) self.assertIn('testdesc', output)