Skip to content

Commit

Permalink
[fix] Fixed UCI format backward compatibility with OpenWISP 1
Browse files Browse the repository at this point in the history
This fix is needed to allow workarounds in the code of
OpenWISP-Firmware to keep working also with OpenWISP 2.
  • Loading branch information
nemesifier committed Feb 26, 2021
1 parent cbb03fd commit a0cda13
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions netjsonconfig/backends/openwisp/openwisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from jinja2 import Environment, PackageLoader

from ..openwrt.openwrt import OpenWrt
from .renderer import OpenWrtRenderer
from .schema import schema


Expand All @@ -12,6 +13,7 @@ class OpenWisp(OpenWrt):
"""

schema = schema
renderer = OpenWrtRenderer

def validate(self):
self._sanitize_radios()
Expand Down
24 changes: 24 additions & 0 deletions netjsonconfig/backends/openwisp/renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from ..openwrt.renderer import OpenWrtRenderer as BaseRenderer


class OpenWrtRenderer(BaseRenderer):
"""
OpenWRT Renderer for OpenWISP 1.x backend
It uses a slightly different template
than the default OpenWRT renderer in order
to provide backward compatibility with the
format that was generated by OpenWISP Manager.
E.g.:
# OpenWISP Manager:
config 'system' 'system'
option 'hostname' 'openwisp-test'
# standard OpenWRT conf generated by netjsonconfig:
config system 'system'
option hostname 'openwisp-test'
"""

pass
20 changes: 20 additions & 0 deletions netjsonconfig/backends/openwisp/templates/openwrt.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% for package, config_blocks in data.items() %}
package {{ package }}

{% for config in config_blocks %}
config '{{ config['.type'] }}' '{{ config['.name'] }}'
{% for key, value in config.items() %}
{% if value not in ['', None] and not key.startswith('.') %}
{% if value is not string and value is iterable %}
{% for list_value in value %}
list '{{ key }}' '{{ list_value }}'
{% endfor %}
{% else %}
option '{{ key }}' '{{ value }}'
{% endif %}
{% endif %}
{% endfor %}

{% endfor %}

{% endfor %}
6 changes: 3 additions & 3 deletions tests/openwisp/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def test_uci(self):
expected = self._tabs(
"""package system
config system 'system'
option hostname 'openwisp-test'
config 'system' 'system'
option 'hostname' 'openwisp-test'
"""
)
self.assertEqual(contents, expected)
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_double_generation(self):
def test_wireless_radio_disabled_0(self):
o = OpenWisp({'radios': self.config['radios']})
output = o.render()
self.assertIn("option disabled '0'", output)
self.assertIn("option 'disabled' '0'", output)

def test_tc_script(self):
config = deepcopy(self.config)
Expand Down

0 comments on commit a0cda13

Please sign in to comment.