Skip to content

Commit e7cd1bb

Browse files
committed
refactor serialization/unserialization
- don't save unused address list - calculate unused address list on unserialization
1 parent bb8733d commit e7cd1bb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

ip_pool/ip_pool.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,22 @@ def release_address(self, hostname):
7474
def _to_dict(self):
7575
_dict = {
7676
"ip_version": self._ip_version,
77-
"network_prefix_len": self._prefix_len,
78-
"unused_addresses": [str(k) for k in self._ipaddr_pool],
77+
"network": str(self._network),
7978
"hostnames": {k: str(v) for k, v in self._hostnames.items()},
8079
}
8180

8281
return _dict
8382

8483
def _initialize_from_dict(self, ip_pool_dict: Dict):
8584
self._ip_version = ip_pool_dict["ip_version"]
86-
self._prefix_len = ip_pool_dict["network_prefix_len"]
87-
unused_addrs = ip_pool_dict["unused_addresses"]
85+
network = IPv4Network(ip_pool_dict["network"])
8886
hostnames = ip_pool_dict["hostnames"]
8987

88+
# unused_addrs = ip_pool_dict["unused_addresses"]
89+
9090
pool = []
9191
# hostnames = {}
9292

93-
for addr_str in unused_addrs:
94-
addr = IPv4Address(addr_str)
95-
pool.append(addr)
96-
9793
for hostname, addr_str in hostnames.items():
9894
addr = IPv4Address(addr_str)
9995
if addr in hostnames.values():
@@ -102,7 +98,13 @@ def _initialize_from_dict(self, ip_pool_dict: Dict):
10298
f"Address [{addr_str}] used more than once: {dupes} "
10399
)
104100
hostnames[hostname] = addr
101+
pool = list(network.hosts())
102+
used_addrs = list(hostnames.values())
103+
for used in used_addrs:
104+
if used in pool:
105+
pool.remove(used)
105106

107+
self._network = network
106108
self._hostnames = hostnames
107109
self._ipaddr_pool = pool
108110

0 commit comments

Comments
 (0)