Skip to content

Commit a0cda13

Browse files
committed
[fix] Fixed UCI format backward compatibility with OpenWISP 1
This fix is needed to allow workarounds in the code of OpenWISP-Firmware to keep working also with OpenWISP 2.
1 parent cbb03fd commit a0cda13

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

netjsonconfig/backends/openwisp/openwisp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from jinja2 import Environment, PackageLoader
44

55
from ..openwrt.openwrt import OpenWrt
6+
from .renderer import OpenWrtRenderer
67
from .schema import schema
78

89

@@ -12,6 +13,7 @@ class OpenWisp(OpenWrt):
1213
"""
1314

1415
schema = schema
16+
renderer = OpenWrtRenderer
1517

1618
def validate(self):
1719
self._sanitize_radios()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from ..openwrt.renderer import OpenWrtRenderer as BaseRenderer
2+
3+
4+
class OpenWrtRenderer(BaseRenderer):
5+
"""
6+
OpenWRT Renderer for OpenWISP 1.x backend
7+
8+
It uses a slightly different template
9+
than the default OpenWRT renderer in order
10+
to provide backward compatibility with the
11+
format that was generated by OpenWISP Manager.
12+
13+
E.g.:
14+
15+
# OpenWISP Manager:
16+
config 'system' 'system'
17+
option 'hostname' 'openwisp-test'
18+
19+
# standard OpenWRT conf generated by netjsonconfig:
20+
config system 'system'
21+
option hostname 'openwisp-test'
22+
"""
23+
24+
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% for package, config_blocks in data.items() %}
2+
package {{ package }}
3+
4+
{% for config in config_blocks %}
5+
config '{{ config['.type'] }}' '{{ config['.name'] }}'
6+
{% for key, value in config.items() %}
7+
{% if value not in ['', None] and not key.startswith('.') %}
8+
{% if value is not string and value is iterable %}
9+
{% for list_value in value %}
10+
list '{{ key }}' '{{ list_value }}'
11+
{% endfor %}
12+
{% else %}
13+
option '{{ key }}' '{{ value }}'
14+
{% endif %}
15+
{% endif %}
16+
{% endfor %}
17+
18+
{% endfor %}
19+
20+
{% endfor %}

tests/openwisp/test_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def test_uci(self):
111111
expected = self._tabs(
112112
"""package system
113113
114-
config system 'system'
115-
option hostname 'openwisp-test'
114+
config 'system' 'system'
115+
option 'hostname' 'openwisp-test'
116116
"""
117117
)
118118
self.assertEqual(contents, expected)
@@ -182,7 +182,7 @@ def test_double_generation(self):
182182
def test_wireless_radio_disabled_0(self):
183183
o = OpenWisp({'radios': self.config['radios']})
184184
output = o.render()
185-
self.assertIn("option disabled '0'", output)
185+
self.assertIn("option 'disabled' '0'", output)
186186

187187
def test_tc_script(self):
188188
config = deepcopy(self.config)

0 commit comments

Comments
 (0)