add_wp_app_config.py for central configuration

This commit is contained in:
2025-04-30 09:49:57 +02:00
parent 2e94a324a6
commit 08bef8fe6e
9 changed files with 92 additions and 91 deletions

View File

@ -15,31 +15,26 @@ from subprocess import check_call
from tkinter import TclError, filedialog, ttk
from cls_mth_fc import (Create, GiteaUpdate, Tooltip, Tunnel, LxTools)
from wp_app_config import AppConfig
LxTools.uos()
Create.dir_and_files()
Create.make_dir()
Create.decrypt()
tcl_path: Path = Path("/usr/share/TK-Themes")
set_file: Path = Path(Path.home() / ".config/wire_py/settings")
keys: Path = Path(Path.home() / ".config/wire_py/keys")
tips = LxTools.if_tip(set_file)
folder_path: Path = Path("/tmp/tlecdcwg/")
user_file = Path("/tmp/.log_user")
tips = LxTools.if_tip(AppConfig.SETTINGS_FILE)
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
VERSION: str = "v. 2.04.1725"
res = GiteaUpdate.api_down("https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION, set_file)
res = GiteaUpdate.api_down("https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION, AppConfig.SETTINGS_FILE)
# Translate
APP = "wirepy"
LOCALE_DIR = "/usr/share/locale/"
locale.bindtextdomain(APP, LOCALE_DIR)
gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
AppConfig.APP_NAME
locale.bindtextdomain(AppConfig.APP_NAME, AppConfig.LOCALE_DIR)
gettext.bindtextdomain(AppConfig.APP_NAME, AppConfig.LOCALE_DIR)
gettext.textdomain(AppConfig.APP_NAME)
_ = gettext.gettext
img_w: str = r"/usr/share/icons/lx-icons/64/info.png"
@ -52,7 +47,7 @@ ie:str = _("Import Error")
pfit: str = _("Please first import tunnel")
pstl: str = _("Please select a tunnel from the list")
LxTools.sigi(folder_path, user_file)
LxTools.sigi(AppConfig.TEMP_DIR, AppConfig.USER_FILE)
class Wirepy(tk.Tk):
"""
@ -62,24 +57,18 @@ class Wirepy(tk.Tk):
super().__init__(*args, **kwargs)
self.my_tool_tip = None
self.x_width = 600
self.y_height = 383
self.x_width = AppConfig.UI_CONFIG["window_size"][0]
self.y_height = AppConfig.UI_CONFIG["window_size"][1]
self.monitor_center_x = int(self.winfo_screenwidth() / 2 - (self.x_width / 2))
self.monitor_center_y = int(self.winfo_screenheight() / 2 - (self.y_height / 2))
self.resizable(width=False, height=False)
self.title("Wire-Py")
self.resizable(AppConfig.UI_CONFIG["resizable_window"][0], AppConfig.UI_CONFIG["resizable_window"][1])
self.title(AppConfig.UI_CONFIG["window_title"])
self.geometry(f"{self.x_width}x{self.y_height}+{self.monitor_center_x}+{self.monitor_center_y}")
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.style = ttk.Style(self)
self.tk.call("source", f"{tcl_path}/water.tcl")
lines = set_file.read_text()
if "light\n" in lines:
self.tk.call("set_theme", "light")
else:
self.tk.call("set_theme", "dark")
self.tk.call("source", f"{AppConfig.SYSTEM_PATHS["tcl_path"]}/water.tcl")
LxTools.theme_change(self)
# Load the image file from the disk
self.wg_icon = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_vpn.png")
@ -352,9 +341,9 @@ class FrameWidgets(ttk.Frame):
"""
if self.tk.call("ttk::style", "theme", "use") == "water-dark":
self.tk.call("set_theme", "light")
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True) # (keepends=True) = not changed
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True) # (keepends=True) = not changed
lines[3] = 'light\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
self.color_label()
def theme_change_dark(self) -> None:
@ -363,9 +352,9 @@ class FrameWidgets(ttk.Frame):
"""
if not self.tk.call("ttk::style", "theme", "use") == "water-dark":
self.tk.call("set_theme", "dark")
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[3] = 'dark\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
self.color_label()
@staticmethod
@ -376,14 +365,14 @@ class FrameWidgets(ttk.Frame):
update_res (int): argument that is passed contains 0 or 1
"""
if update_res == 1:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[1] = 'off\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
else:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[1] = 'on\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
@staticmethod
def tooltip(tip) -> None:
@ -393,14 +382,14 @@ class FrameWidgets(ttk.Frame):
tip (bool): argument that is passed contains True or False
"""
if tip:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[5] = 'False\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
else:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[5] = 'True\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
def enable_check_box(self, _) -> None:
"""
@ -425,12 +414,12 @@ class FrameWidgets(ttk.Frame):
pre_key = key[3]
check_call(["nmcli", "connection", "delete", select_tl])
self.l_box.delete(self.select_tunnel[0])
with open(set_file, "r", encoding="utf-8") as set_f6:
with open(AppConfig.SETTINGS_FILE, "r", encoding="utf-8") as set_f6:
lines6 = set_f6.readlines()
if (select_tl == lines6[7].strip()
and "off\n" not in lines6[7].strip()):
lines6[7] = "off\n"
with open(set_file, "w", encoding="utf-8") as set_f7:
with open(AppConfig.SETTINGS_FILE, "w", encoding="utf-8") as set_f7:
set_f7.writelines(lines6)
self.selected_option.set(0)
self.autoconnect_var.set(_("no Autoconnect"))
@ -516,11 +505,11 @@ class FrameWidgets(ttk.Frame):
if self.a != "" and self.a == select_tl:
self.a = Tunnel.active()
self.str_var.set(value=self.a)
with open(set_file, "r", encoding="utf-8") as set_f5:
with open(AppConfig.SETTINGS_FILE, "r", encoding="utf-8") as set_f5:
lines5 = set_f5.readlines()
if select_tl == lines5[7].strip() and "off\n" not in lines5[7].strip():
lines5[7] = new_a_connect
with open(set_file, "w", encoding="utf-8") as theme_set5:
with open(AppConfig.SETTINGS_FILE, "w", encoding="utf-8") as theme_set5:
theme_set5.writelines(lines5)
self.autoconnect_var.set(value=new_a_connect)
@ -658,9 +647,9 @@ class FrameWidgets(ttk.Frame):
select_tl = self.l_box.get(select_tunnel[0])
if self.selected_option.get() == 0:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[7] = 'off\n'
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
tl = Tunnel.list()
@ -668,9 +657,9 @@ class FrameWidgets(ttk.Frame):
self.wg_autostart.configure(state="disabled")
if self.selected_option.get() >= 1:
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
lines[7] = select_tl
Path(set_file).write_text(''.join(lines), encoding="utf-8")
Path(AppConfig.SETTINGS_FILE).write_text(''.join(lines), encoding="utf-8")
except IndexError:
self.selected_option.set(1)
@ -683,7 +672,7 @@ class FrameWidgets(ttk.Frame):
Set (on), the selected tunnel is displayed in the label.
At (off) the label is first emptied then filled with No Autoconnect
"""
lines = Path(set_file).read_text(encoding="utf-8").splitlines(keepends=True)
lines = Path(AppConfig.SETTINGS_FILE).read_text(encoding="utf-8").splitlines(keepends=True)
if lines[7] != "off\n":
print(f"{lines[7]} starts automatically when the system starts.")
@ -771,7 +760,7 @@ class FrameWidgets(ttk.Frame):
"""
View activ Tunnel in the color green or yellow
"""
lines = set_file.read_text()
lines = AppConfig.SETTINGS_FILE.read_text()
if "light\n" in lines:
self.lb_tunnel = ttk.Label(self, textvariable=self.str_var, foreground="green")
@ -865,5 +854,5 @@ if __name__ == "__main__":
window.tk.call("set", "::tk::dialog::file::showHiddenVar", "0")
window.mainloop()
LxTools.clean_files(folder_path, user_file)
LxTools.clean_files(AppConfig.TEMP_DIR, AppConfig.USER_FILE)
sys.exit(0)