From 631cbbc73e3418be44da377c918f2fcbe1af5f8f Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Thu, 15 Jul 2021 16:42:19 -0700 Subject: [PATCH] pygui: add configuration and ip options to enable/disable ip4/ip6 assignment, improved gui configuration to handle new fields that do not already exist --- daemon/core/gui/appconfig.py | 33 ++++++++++++----------------- daemon/core/gui/dialogs/ipdialog.py | 33 +++++++++++++++++++++++------ daemon/core/gui/interface.py | 15 +++++++++---- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/daemon/core/gui/appconfig.py b/daemon/core/gui/appconfig.py index 8ce7b2b0..04f2fdcb 100644 --- a/daemon/core/gui/appconfig.py +++ b/daemon/core/gui/appconfig.py @@ -120,25 +120,18 @@ class IpConfigs(yaml.YAMLObject): yaml_tag: str = "!IpConfigs" yaml_loader: Type[yaml.SafeLoader] = yaml.SafeLoader - def __init__( - self, - ip4: str = None, - ip6: str = None, - ip4s: List[str] = None, - ip6s: List[str] = None, - ) -> None: - if ip4s is None: - ip4s = ["10.0.0.0", "192.168.0.0", "172.16.0.0"] - self.ip4s: List[str] = ip4s - if ip6s is None: - ip6s = ["2001::", "2002::", "a::"] - 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 + def __init__(self, **kwargs) -> None: + self.__setstate__(kwargs) + + def __setstate__(self, kwargs): + self.ip4s: List[str] = kwargs.get( + "ip4s", ["10.0.0.0", "192.168.0.0", "172.16.0.0"] + ) + self.ip4: str = kwargs.get("ip4", self.ip4s[0]) + self.ip6s: List[str] = kwargs.get("ip6s", ["2001::", "2002::", "a::"]) + self.ip6: str = kwargs.get("ip6", self.ip6s[0]) + self.enable_ip4: bool = kwargs.get("enable_ip4", True) + self.enable_ip6: bool = kwargs.get("enable_ip6", True) class GuiConfig(yaml.YAMLObject): @@ -223,7 +216,7 @@ def check_directory() -> None: def read() -> GuiConfig: with CONFIG_PATH.open("r") as f: - return yaml.load(f, Loader=yaml.SafeLoader) + return yaml.safe_load(f) def save(config: GuiConfig) -> None: diff --git a/daemon/core/gui/dialogs/ipdialog.py b/daemon/core/gui/dialogs/ipdialog.py index a09ca097..68b5ab36 100644 --- a/daemon/core/gui/dialogs/ipdialog.py +++ b/daemon/core/gui/dialogs/ipdialog.py @@ -23,6 +23,8 @@ class IpConfigDialog(Dialog): self.ip4_listbox: Optional[ListboxScroll] = None self.ip6_entry: Optional[ttk.Entry] = 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() def draw(self) -> None: @@ -36,10 +38,19 @@ class IpConfigDialog(Dialog): frame.rowconfigure(0, weight=1) 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.columnconfigure(0, weight=1) - ip4_frame.rowconfigure(0, weight=1) - ip4_frame.grid(row=0, column=0, stick="nsew") + ip4_frame.rowconfigure(1, weight=1) + ip4_frame.grid(row=1, column=0, stick=tk.NSEW) self.ip4_listbox = ListboxScroll(ip4_frame) self.ip4_listbox.listbox.bind("<>", self.select_ip4) 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.columnconfigure(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.listbox.bind("<>", self.select_ip6) self.ip6_listbox.grid(sticky=tk.NSEW, pady=PADY) @@ -86,7 +97,7 @@ class IpConfigDialog(Dialog): # draw buttons frame = ttk.Frame(self.top) - frame.grid(stick="ew") + frame.grid(stick=tk.EW) for i in range(2): frame.columnconfigure(i, weight=1) button = ttk.Button(frame, text="Save", command=self.click_save) @@ -142,10 +153,18 @@ class IpConfigDialog(Dialog): ip6 = self.ip6_listbox.listbox.get(index) ip6s.append(ip6) ip_config = self.app.guiconfig.ips - ip_config.ip4 = self.ip4 - ip_config.ip6 = self.ip6 + ip_changed = False + 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.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.destroy() diff --git a/daemon/core/gui/interface.py b/daemon/core/gui/interface.py index 8076e202..d4d09443 100644 --- a/daemon/core/gui/interface.py +++ b/daemon/core/gui/interface.py @@ -160,11 +160,18 @@ class InterfaceManager: index += 1 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) - ip4 = self.current_subnets.ip4[index] - ip6 = self.current_subnets.ip6[index] - return str(ip4), str(ip6) + if enable_ip4: + ip4 = str(self.current_subnets.ip4[index]) + if enable_ip6: + ip6 = str(self.current_subnets.ip6[index]) + return ip4, ip6 def get_subnets(self, iface: Interface) -> Subnets: ip4_subnet = self.ip4_subnets