Skip to content

Commit e1e9b41

Browse files
committed
[fix] Update renderer to use regex for True/False coercion
1 parent 99225ad commit e1e9b41

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

netjsonconfig/backends/openwrt/renderer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ def cleanup(self, output):
1616
.replace("\noption", "\n\toption")
1717
.replace("\nlist", "\n\tlist")
1818
)
19-
# convert True to 1 and False to 0
20-
output = output.replace("True", "1").replace("False", "0")
19+
import re
20+
# convert 'True' to '1' and 'False' to '0', but only on whole words bounded by quotes e.g. 'True' -> '1'
21+
output = re.sub(r"'True'", "'1'", output)
22+
output = re.sub(r"'False'", "'0'", output)
2123
# max 2 consecutive \n delimiters
2224
output = output.replace("\n\n\n", "\n\n")
2325
# if output is present

tests/openwrt/test_wireless.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ def test_render_wifi_interface(self):
6363
expected = self._tabs(self._wifi_uci)
6464
self.assertEqual(o.render(), expected)
6565

66+
def test_render_wifi_issue_250_true_ssid(self):
67+
netjson = deepcopy(self._wifi_netjson)
68+
netjson["interfaces"][0]["wireless"]["ssid"] = "TrueGait Living Guest"
69+
o = OpenWrt(netjson)
70+
expected_uci = self._wifi_uci.replace("'MyWifiAP'", "'TrueGait Living Guest'")
71+
self.assertEqual(o.render(), self._tabs(expected_uci))
72+
73+
def test_render_wifi_issue_250_false_ssid(self):
74+
netjson = deepcopy(self._wifi_netjson)
75+
netjson["interfaces"][0]["wireless"]["ssid"] = "FalseGait Living Guest"
76+
o = OpenWrt(netjson)
77+
expected_uci = self._wifi_uci.replace("'MyWifiAP'", "'FalseGait Living Guest'")
78+
self.assertEqual(o.render(), self._tabs(expected_uci))
79+
80+
6681
def test_parse_wifi_interface(self):
6782
o = OpenWrt(native=self._wifi_uci)
6883
self.assertDictEqual(o.config, self._wifi_netjson)

0 commit comments

Comments
 (0)