part 1 load data from dictionary works

This commit is contained in:
2025-05-18 12:47:52 +02:00
parent 0c4d000d96
commit d6c20b81f9
6 changed files with 231 additions and 176 deletions

View File

@ -17,7 +17,7 @@ from tkinter import TclError, filedialog, ttk
from common_tools import (
ConfigManager,
ThemeManager,
Create,
CryptoUtil,
GiteaUpdate,
Tunnel,
Tooltip,
@ -28,7 +28,7 @@ from wp_app_config import AppConfig, Msg
AppConfig.USER_FILE.write_text(getpass.getuser())
AppConfig.ensure_directories()
AppConfig.create_default_settings()
Create.decrypt()
CryptoUtil.decrypt()
class Wirepy(tk.Tk):
@ -242,15 +242,20 @@ class FrameWidgets(ttk.Frame):
self.l_box.configure(yscrollcommand=self.scrollbar.set)
# Tunnel List
self.tl = LxTools.get_file_name(AppConfig.TEMP_DIR)
for tunnels in self.tl:
self.tl = Tunnel.parse_files_to_dictionary(directory=AppConfig.TEMP_DIR)
LxTools.clean_files(AppConfig.TEMP_DIR, file=None)
AppConfig.ensure_directories()
# self.tl = LxTools.get_file_name(AppConfig.TEMP_DIR)
for tunnels, values in self.tl.items():
self.l_box.insert("end", tunnels)
self.l_box.update()
# Button Vpn
if self.a != "":
self.stop()
data = self.handle_tunnel_data(self.a)
self.handle_tunnel_data(self.a, self.tl)
self.show_data()
else:
self.start()
@ -586,22 +591,6 @@ class FrameWidgets(ttk.Frame):
else:
Tooltip(self.btn_stst, Msg.TTIP["start_tl"], self.tooltip_state)
def handle_tunnel_data(self, tunnel_name: str) -> tuple[str, str, str, str | None]:
"""_summary_
Args:
tunnel_name (str): name of a tunnel
Returns:
tuple[str, str]: tuple with tunnel data
"""
wg_read = f"/tmp/tlecdcwg/{tunnel_name}.conf"
with open(wg_read, "r", encoding="utf-8") as file:
data = Tunnel.con_to_dict(file)
self.init_and_report(data)
self.show_data()
return data
def color_label(self) -> None:
"""
View activ Tunnel in the color green or yellow
@ -652,25 +641,19 @@ class FrameWidgets(ttk.Frame):
title=_("Select Wireguard config File"),
filetypes=[(_("WG config files"), "*.conf")],
)
# Überprüfe, ob der Benutzer den Dialog abgebrochen hat
# Check if a file was selected
if not filepath:
print("File import: abort by user...")
return
with open(filepath, "r", encoding="utf-8") as file:
read = file.read()
path_split = filepath.split("/")
path_split1 = path_split[-1]
filepath = Path(filepath)
read = filepath.read_text(encoding="utf-8")
if (
"PrivateKey = " in read
and "PublicKey = " in read
and "Endpoint =" in read
and "Endpoint = " in read
):
with open(filepath, "r", encoding="utf-8") as file:
key = Tunnel.con_to_dict(file)
key = Tunnel.con_to_dict(read)
pre_key = key[3]
if len(pre_key) != 0:
@ -714,7 +697,7 @@ class FrameWidgets(ttk.Frame):
text=True,
)
Create.encrypt()
CryptoUtil.encrypt()
else:
shutil.copy(filepath, f"{AppConfig.TEMP_DIR}/")
@ -737,7 +720,7 @@ class FrameWidgets(ttk.Frame):
text=True,
)
Create.encrypt()
CryptoUtil.encrypt()
self.str_var.set("")
self.a = Tunnel.active()
self.l_box.insert(0, self.a)
@ -764,7 +747,7 @@ class FrameWidgets(ttk.Frame):
self.str_var.set(self.a)
self.color_label()
self.stop()
data = self.handle_tunnel_data(self.a)
self.handle_tunnel_data(self.a, self.tl)
process: CompletedProcess[str] = subprocess.run(
[
"nmcli",
@ -1053,7 +1036,7 @@ class FrameWidgets(ttk.Frame):
theme_set5.writelines(lines5)
self.autoconnect_var.set(value=new_a_connect)
self.update_connection_display()
Create.encrypt()
CryptoUtil.encrypt()
except IndexError:
@ -1070,18 +1053,17 @@ class FrameWidgets(ttk.Frame):
except EOFError as e:
print(e)
def init_and_report(self, data=None) -> None:
"""
Displays the value address, DNS and peer in the labels
or empty it again
"""
def handle_tunnel_data(self, active=None, data=None) -> None:
tunnel = active
values = data[tunnel]
# Address Label
self.add = tk.StringVar()
self.add.set(f"{_("Address: ")}{data[0]}")
self.add.set(f" Address: {values['Address']}")
self.DNS = tk.StringVar()
self.DNS.set(f" DNS: {data[1]}")
self.DNS.set(f" DNS: {values['DNS']}")
self.enp = tk.StringVar()
self.enp.set(f"{_("Endpoint: ")}{data[2]}")
self.enp.set(f"Endpoint: {values['Endpoint']}")
def show_data(self) -> None:
"""
@ -1118,10 +1100,8 @@ class FrameWidgets(ttk.Frame):
else:
data = self.handle_tunnel_data(self.a)
if data:
self.handle_connection_state("stop")
self.handle_tunnel_data(self.a, self.tl)
self.handle_connection_state("stop")
except IndexError:
@ -1167,8 +1147,7 @@ class FrameWidgets(ttk.Frame):
["nmcli", "connection", "up", target_tunnel]
)
self.update_connection_display()
data = self.handle_tunnel_data(self.a)
self.init_and_report(data)
self.handle_tunnel_data(self.a, self.tl)
self.show_data()
self.color_label()
self.stop()