methods optimized-13:24

This commit is contained in:
Désiré Werner Menrath 2025-04-29 13:25:03 +02:00
parent 19d413ea97
commit 5fb4e68867

View File

@ -554,7 +554,6 @@ class FrameWidgets(ttk.Frame):
read = file.read() read = file.read()
path_split = filepath.split("/") path_split = filepath.split("/")
path_split1 = path_split[-1] path_split1 = path_split[-1]
self.a = Tunnel.active()
if "PrivateKey = " in read and "PublicKey = " in read and "Endpoint =" in read: if "PrivateKey = " in read and "PublicKey = " in read and "Endpoint =" in read:
with open(filepath, "r", encoding="utf-8") as file: with open(filepath, "r", encoding="utf-8") as file:
@ -578,7 +577,7 @@ class FrameWidgets(ttk.Frame):
os.rename(p1, f"/tmp/tlecdcwg/{path_split}") os.rename(p1, f"/tmp/tlecdcwg/{path_split}")
new_conf = f"/tmp/tlecdcwg/{path_split}" new_conf = f"/tmp/tlecdcwg/{path_split}"
if self.a != "": if self.a != "":
check_call(["nmcli", "connection", "down", Tunnel.active()]) check_call(["nmcli", "connection", "down", self.a])
self.reset_fields() self.reset_fields()
subprocess.check_output(["nmcli", "connection", "import", "type", subprocess.check_output(["nmcli", "connection", "import", "type",
@ -589,7 +588,7 @@ class FrameWidgets(ttk.Frame):
else: else:
shutil.copy(filepath, "/tmp/tlecdcwg/") shutil.copy(filepath, "/tmp/tlecdcwg/")
if self.a != "": if self.a != "":
check_call(["nmcli", "connection", "down", Tunnel.active()]) check_call(["nmcli", "connection", "down", self.a])
self.reset_fields() self.reset_fields()
subprocess.check_output(["nmcli", "connection", "import", "type", subprocess.check_output(["nmcli", "connection", "import", "type",
@ -670,11 +669,9 @@ class FrameWidgets(ttk.Frame):
select_tl = self.l_box.get(select_tunnel[0]) select_tl = self.l_box.get(select_tunnel[0])
if self.selected_option.get() == 0: if self.selected_option.get() == 0:
with open(set_file, "r", encoding="utf-8") as set_f3: lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines3 = set_f3.readlines() lines[7] = 'off\n'
lines3[7] = "off\n" Path(set_file).write_text(''.join(lines), encoding="utf-8")
with open(set_file, "w", encoding="utf-8") as set_f3:
set_f3.writelines(lines3)
tl = Tunnel.list() tl = Tunnel.list()
@ -682,11 +679,9 @@ class FrameWidgets(ttk.Frame):
self.wg_autostart.configure(state="disabled") self.wg_autostart.configure(state="disabled")
if self.selected_option.get() >= 1: if self.selected_option.get() >= 1:
with open(set_file, "r", encoding="utf-8") as set_f3: lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines3 = set_f3.readlines() lines[7] = select_tl
lines3[7] = select_tl Path(set_file).write_text(''.join(lines), encoding="utf-8")
with open(set_file, "w", encoding="utf-8") as set_f3:
set_f3.writelines(lines3)
except IndexError: except IndexError:
self.selected_option.set(1) self.selected_option.set(1)
@ -699,14 +694,13 @@ class FrameWidgets(ttk.Frame):
Set (on), the selected tunnel is displayed in the label. Set (on), the selected tunnel is displayed in the label.
At (off) the label is first emptied then filled with No Autoconnect At (off) the label is first emptied then filled with No Autoconnect
""" """
with open(set_file, "r", encoding="utf-8") as set_f4: lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines4 = set_f4.readlines()
if lines4[7] != "off\n": if lines[7] != "off\n":
print(f"{lines4[7]} starts automatically when the system starts.") print(f"{lines[7]} starts automatically when the system starts.")
self.selected_option.set(1) self.selected_option.set(1)
self.autoconnect_var.set("") self.autoconnect_var.set("")
self.auto_con = lines4[7] self.auto_con = lines[7]
else: else:
self.selected_option.set(0) self.selected_option.set(0)