diff --git a/netjsonconfig/backends/base/converter.py b/netjsonconfig/backends/base/converter.py index 6dd651a19..ed3bc84c5 100644 --- a/netjsonconfig/backends/base/converter.py +++ b/netjsonconfig/backends/base/converter.py @@ -88,12 +88,13 @@ def to_intermediate_loop(self, block, result, index=None): # pragma: nocover def to_netjson(self, remove_block=True): """ - Converts the intermediate data structure (``self.intermediate_datra``) + Converts the intermediate data structure (``self.intermediate_data``) to a NetJSON configuration dictionary (``self.config``) """ result = OrderedDict() - # Clean intermediate data - intermediate_data = self.clean_intermediate_data(list(self.intermediate_data[self.intermediate_key])) + # clean intermediate data + intermediate_data = self.to_netjson_clean(self.intermediate_data[self.intermediate_key]) + # intermediate_data = list(self.intermediate_data[self.intermediate_key]) # iterate over copied intermediate data structure for index, block in enumerate(intermediate_data): if self.should_skip_block(block): @@ -109,18 +110,13 @@ def to_netjson(self, remove_block=True): # return result, expects dict return result - def clean_intermediate_data(self, intermediate_data): - """ - Utility method called to clean data for backend in ``to_netjson`` - """ - clean_intermediate_data, appendto_clean_intermediate_data = [], [] - for block in intermediate_data: - if '.type' in block and block['.type'] == 'switch_vlan': - appendto_clean_intermediate_data.append(block) - else: - clean_intermediate_data.append(block) - clean_intermediate_data.extend(appendto_clean_intermediate_data) - return clean_intermediate_data + def to_netjson_clean(self, intermediate_data): + """ + Utility method called to pre-process the intermediate data structure + during backward conversion (``to_netjson``) + """ + # returns a copy in order to avoid modifying the original structure + return list(intermediate_data) def to_netjson_loop(self, block, result, index=None): # pragma: nocover """ diff --git a/netjsonconfig/backends/openwrt/converters/switch.py b/netjsonconfig/backends/openwrt/converters/switch.py index 723fec453..c1e650966 100644 --- a/netjsonconfig/backends/openwrt/converters/switch.py +++ b/netjsonconfig/backends/openwrt/converters/switch.py @@ -21,6 +21,16 @@ def to_intermediate_loop(self, block, result, index=None): result['network'] += switch_vlan return result + def to_netjson_clean(self, intermediate_data): + reordered_data, last_items = [], [] + for block in super().to_netjson_clean(intermediate_data): + if '.type' in block and block['.type'] == 'switch_vlan': + last_items.append(block) + else: + reordered_data.append(block) + reordered_data.extend(last_items) + return reordered_data + def __intermediate_switch(self, switch): switch.update({ '.type': 'switch',