Skip to content

Commit 5182887

Browse files
committed
[openwrt] Fixed bug in switch backward conversion #149
Improves and closes #149
1 parent 772cec1 commit 5182887

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

netjsonconfig/backends/base/converter.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ def to_intermediate_loop(self, block, result, index=None): # pragma: nocover
8888

8989
def to_netjson(self, remove_block=True):
9090
"""
91-
Converts the intermediate data structure (``self.intermediate_datra``)
91+
Converts the intermediate data structure (``self.intermediate_data``)
9292
to a NetJSON configuration dictionary (``self.config``)
9393
"""
9494
result = OrderedDict()
95-
# Clean intermediate data
96-
intermediate_data = self.clean_intermediate_data(list(self.intermediate_data[self.intermediate_key]))
95+
# clean intermediate data
96+
intermediate_data = self.to_netjson_clean(self.intermediate_data[self.intermediate_key])
97+
# intermediate_data = list(self.intermediate_data[self.intermediate_key])
9798
# iterate over copied intermediate data structure
9899
for index, block in enumerate(intermediate_data):
99100
if self.should_skip_block(block):
@@ -109,18 +110,13 @@ def to_netjson(self, remove_block=True):
109110
# return result, expects dict
110111
return result
111112

112-
def clean_intermediate_data(self, intermediate_data):
113-
"""
114-
Utility method called to clean data for backend in ``to_netjson``
115-
"""
116-
clean_intermediate_data, appendto_clean_intermediate_data = [], []
117-
for block in intermediate_data:
118-
if '.type' in block and block['.type'] == 'switch_vlan':
119-
appendto_clean_intermediate_data.append(block)
120-
else:
121-
clean_intermediate_data.append(block)
122-
clean_intermediate_data.extend(appendto_clean_intermediate_data)
123-
return clean_intermediate_data
113+
def to_netjson_clean(self, intermediate_data):
114+
"""
115+
Utility method called to pre-process the intermediate data structure
116+
during backward conversion (``to_netjson``)
117+
"""
118+
# returns a copy in order to avoid modifying the original structure
119+
return list(intermediate_data)
124120

125121
def to_netjson_loop(self, block, result, index=None): # pragma: nocover
126122
"""

netjsonconfig/backends/openwrt/converters/switch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ def to_intermediate_loop(self, block, result, index=None):
2121
result['network'] += switch_vlan
2222
return result
2323

24+
def to_netjson_clean(self, intermediate_data):
25+
reordered_data, last_items = [], []
26+
for block in super().to_netjson_clean(intermediate_data):
27+
if '.type' in block and block['.type'] == 'switch_vlan':
28+
last_items.append(block)
29+
else:
30+
reordered_data.append(block)
31+
reordered_data.extend(last_items)
32+
return reordered_data
33+
2434
def __intermediate_switch(self, switch):
2535
switch.update({
2636
'.type': 'switch',

0 commit comments

Comments
 (0)