pygui: add configuration and ip options to enable/disable ip4/ip6 assignment, improved gui configuration to handle new fields that do not already exist

This commit is contained in:
Blake Harnden 2021-07-15 16:42:19 -07:00
parent 150d4bd6ea
commit 631cbbc73e
3 changed files with 50 additions and 31 deletions

View file

@ -120,25 +120,18 @@ class IpConfigs(yaml.YAMLObject):
yaml_tag: str = "!IpConfigs" yaml_tag: str = "!IpConfigs"
yaml_loader: Type[yaml.SafeLoader] = yaml.SafeLoader yaml_loader: Type[yaml.SafeLoader] = yaml.SafeLoader
def __init__( def __init__(self, **kwargs) -> None:
self, self.__setstate__(kwargs)
ip4: str = None,
ip6: str = None, def __setstate__(self, kwargs):
ip4s: List[str] = None, self.ip4s: List[str] = kwargs.get(
ip6s: List[str] = None, "ip4s", ["10.0.0.0", "192.168.0.0", "172.16.0.0"]
) -> None: )
if ip4s is None: self.ip4: str = kwargs.get("ip4", self.ip4s[0])
ip4s = ["10.0.0.0", "192.168.0.0", "172.16.0.0"] self.ip6s: List[str] = kwargs.get("ip6s", ["2001::", "2002::", "a::"])
self.ip4s: List[str] = ip4s self.ip6: str = kwargs.get("ip6", self.ip6s[0])
if ip6s is None: self.enable_ip4: bool = kwargs.get("enable_ip4", True)
ip6s = ["2001::", "2002::", "a::"] self.enable_ip6: bool = kwargs.get("enable_ip6", True)
self.ip6s: List[str] = ip6s
if ip4 is None:
ip4 = self.ip4s[0]
self.ip4: str = ip4
if ip6 is None:
ip6 = self.ip6s[0]
self.ip6: str = ip6
class GuiConfig(yaml.YAMLObject): class GuiConfig(yaml.YAMLObject):
@ -223,7 +216,7 @@ def check_directory() -> None:
def read() -> GuiConfig: def read() -> GuiConfig:
with CONFIG_PATH.open("r") as f: with CONFIG_PATH.open("r") as f:
return yaml.load(f, Loader=yaml.SafeLoader) return yaml.safe_load(f)
def save(config: GuiConfig) -> None: def save(config: GuiConfig) -> None:

View file

@ -23,6 +23,8 @@ class IpConfigDialog(Dialog):
self.ip4_listbox: Optional[ListboxScroll] = None self.ip4_listbox: Optional[ListboxScroll] = None
self.ip6_entry: Optional[ttk.Entry] = None self.ip6_entry: Optional[ttk.Entry] = None
self.ip6_listbox: Optional[ListboxScroll] = None self.ip6_listbox: Optional[ListboxScroll] = None
self.enable_ip4 = tk.BooleanVar(value=self.app.guiconfig.ips.enable_ip4)
self.enable_ip6 = tk.BooleanVar(value=self.app.guiconfig.ips.enable_ip6)
self.draw() self.draw()
def draw(self) -> None: def draw(self) -> None:
@ -36,10 +38,19 @@ class IpConfigDialog(Dialog):
frame.rowconfigure(0, weight=1) frame.rowconfigure(0, weight=1)
frame.grid(sticky=tk.NSEW, pady=PADY) frame.grid(sticky=tk.NSEW, pady=PADY)
ip4_checkbox = ttk.Checkbutton(
frame, text="Enable IP4?", variable=self.enable_ip4
)
ip4_checkbox.grid(row=0, column=0, sticky=tk.EW)
ip6_checkbox = ttk.Checkbutton(
frame, text="Enable IP6?", variable=self.enable_ip6
)
ip6_checkbox.grid(row=0, column=1, sticky=tk.EW)
ip4_frame = ttk.LabelFrame(frame, text="IPv4", padding=FRAME_PAD) ip4_frame = ttk.LabelFrame(frame, text="IPv4", padding=FRAME_PAD)
ip4_frame.columnconfigure(0, weight=1) ip4_frame.columnconfigure(0, weight=1)
ip4_frame.rowconfigure(0, weight=1) ip4_frame.rowconfigure(1, weight=1)
ip4_frame.grid(row=0, column=0, stick="nsew") ip4_frame.grid(row=1, column=0, stick=tk.NSEW)
self.ip4_listbox = ListboxScroll(ip4_frame) self.ip4_listbox = ListboxScroll(ip4_frame)
self.ip4_listbox.listbox.bind("<<ListboxSelect>>", self.select_ip4) self.ip4_listbox.listbox.bind("<<ListboxSelect>>", self.select_ip4)
self.ip4_listbox.grid(sticky=tk.NSEW, pady=PADY) self.ip4_listbox.grid(sticky=tk.NSEW, pady=PADY)
@ -63,7 +74,7 @@ class IpConfigDialog(Dialog):
ip6_frame = ttk.LabelFrame(frame, text="IPv6", padding=FRAME_PAD) ip6_frame = ttk.LabelFrame(frame, text="IPv6", padding=FRAME_PAD)
ip6_frame.columnconfigure(0, weight=1) ip6_frame.columnconfigure(0, weight=1)
ip6_frame.rowconfigure(0, weight=1) ip6_frame.rowconfigure(0, weight=1)
ip6_frame.grid(row=0, column=1, stick="nsew") ip6_frame.grid(row=1, column=1, stick=tk.NSEW)
self.ip6_listbox = ListboxScroll(ip6_frame) self.ip6_listbox = ListboxScroll(ip6_frame)
self.ip6_listbox.listbox.bind("<<ListboxSelect>>", self.select_ip6) self.ip6_listbox.listbox.bind("<<ListboxSelect>>", self.select_ip6)
self.ip6_listbox.grid(sticky=tk.NSEW, pady=PADY) self.ip6_listbox.grid(sticky=tk.NSEW, pady=PADY)
@ -86,7 +97,7 @@ class IpConfigDialog(Dialog):
# draw buttons # draw buttons
frame = ttk.Frame(self.top) frame = ttk.Frame(self.top)
frame.grid(stick="ew") frame.grid(stick=tk.EW)
for i in range(2): for i in range(2):
frame.columnconfigure(i, weight=1) frame.columnconfigure(i, weight=1)
button = ttk.Button(frame, text="Save", command=self.click_save) button = ttk.Button(frame, text="Save", command=self.click_save)
@ -142,10 +153,18 @@ class IpConfigDialog(Dialog):
ip6 = self.ip6_listbox.listbox.get(index) ip6 = self.ip6_listbox.listbox.get(index)
ip6s.append(ip6) ip6s.append(ip6)
ip_config = self.app.guiconfig.ips ip_config = self.app.guiconfig.ips
ip_config.ip4 = self.ip4 ip_changed = False
ip_config.ip6 = self.ip6 if ip_config.ip4 != self.ip4:
ip_config.ip4 = self.ip4
ip_changed = True
if ip_config.ip6 != self.ip6:
ip_config.ip6 = self.ip6
ip_changed = True
ip_config.ip4s = ip4s ip_config.ip4s = ip4s
ip_config.ip6s = ip6s ip_config.ip6s = ip6s
self.app.core.ifaces_manager.update_ips(self.ip4, self.ip6) ip_config.enable_ip4 = self.enable_ip4.get()
ip_config.enable_ip6 = self.enable_ip6.get()
if ip_changed:
self.app.core.ifaces_manager.update_ips(self.ip4, self.ip6)
self.app.save_config() self.app.save_config()
self.destroy() self.destroy()

View file

@ -160,11 +160,18 @@ class InterfaceManager:
index += 1 index += 1
return index return index
def get_ips(self, node: Node) -> [str, str]: def get_ips(self, node: Node) -> [Optional[str], Optional[str]]:
enable_ip4 = self.app.guiconfig.ips.enable_ip4
enable_ip6 = self.app.guiconfig.ips.enable_ip6
ip4, ip6 = None, None
if not enable_ip4 and not enable_ip6:
return ip4, ip6
index = self.next_index(node) index = self.next_index(node)
ip4 = self.current_subnets.ip4[index] if enable_ip4:
ip6 = self.current_subnets.ip6[index] ip4 = str(self.current_subnets.ip4[index])
return str(ip4), str(ip6) if enable_ip6:
ip6 = str(self.current_subnets.ip6[index])
return ip4, ip6
def get_subnets(self, iface: Interface) -> Subnets: def get_subnets(self, iface: Interface) -> Subnets:
ip4_subnet = self.ip4_subnets ip4_subnet = self.ip4_subnets