Skip to content

Commit

Permalink
improvement(core): add IO_Object.get_dict_from_tuple() helper
Browse files Browse the repository at this point in the history
Too much duplicated code. Factor out.

This obviously doesn't safe any lines of code. It still factors out the
concept of converting a config-tuple to a dictionary.
  • Loading branch information
thom311 authored and erig0 committed Jan 9, 2024
1 parent 131e4fe commit 37d453c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/firewall/core/fw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,7 @@ def get_service_config_dict(self, obj):
return obj.export_config_dict()

def set_service_config(self, obj, conf):
conf_dict = {}
for i, value in enumerate(conf):
conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]] = value

conf_dict = obj.get_dict_from_tuple(conf)
return self.set_service_config_dict(obj, conf_dict)

def set_service_config_dict(self, obj, conf):
Expand All @@ -653,10 +650,9 @@ def new_service(self, name, conf):
if name in self._services or name in self._builtin_services:
raise FirewallError(errors.NAME_CONFLICT, "new_service(): '%s'" % name)

conf_dict = {}
for i, value in enumerate(conf):
conf_dict[Service.IMPORT_EXPORT_STRUCTURE[i][0]] = value

conf_dict = IO_Object.get_dict_from_tuple_static(
Service.IMPORT_EXPORT_STRUCTURE, conf
)
return self.new_service_dict(name, conf_dict)

def new_service_dict(self, name, conf):
Expand Down Expand Up @@ -827,10 +823,7 @@ def get_zone_config_dict(self, obj):
return obj.export_config_dict()

def set_zone_config(self, obj, conf):
conf_dict = {}
for i, value in enumerate(conf):
conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]] = value

conf_dict = obj.get_dict_from_tuple(conf)
return self.set_zone_config_dict(obj, conf_dict)

def set_zone_config_dict(self, obj, conf):
Expand All @@ -851,10 +844,9 @@ def new_zone(self, name, conf):
if name in self._zones or name in self._builtin_zones:
raise FirewallError(errors.NAME_CONFLICT, "new_zone(): '%s'" % name)

conf_dict = {}
for i, value in enumerate(conf):
conf_dict[Zone.IMPORT_EXPORT_STRUCTURE[i][0]] = value

conf_dict = IO_Object.get_dict_from_tuple_static(
Zone.IMPORT_EXPORT_STRUCTURE, conf
)
return self.new_zone_dict(name, conf_dict)

def new_zone_dict(self, name, conf):
Expand Down
10 changes: 10 additions & 0 deletions src/firewall/core/io/io_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def export_config(self):
ret.append(copy.deepcopy(getattr(self, x[0])))
return tuple(ret)

@staticmethod
def get_dict_from_tuple_static(IMPORT_EXPORT_STRUCTURE, conf):
conf_dict = {}
for i, value in enumerate(conf):
conf_dict[IMPORT_EXPORT_STRUCTURE[i][0]] = value
return conf_dict

def get_dict_from_tuple(obj, conf):
return IO_Object.get_dict_from_tuple_static(obj.IMPORT_EXPORT_STRUCTURE, conf)

def export_config_dict(self):
conf = {}
type_formats = dict([(x[0], x[1]) for x in self.IMPORT_EXPORT_STRUCTURE])
Expand Down

0 comments on commit 37d453c

Please sign in to comment.