Class LxTools expands wg_set to set_file renamed

This commit is contained in:
Désiré Werner Menrath 2025-04-24 23:04:34 +02:00
parent 87943b2489
commit d2a57b329b
3 changed files with 136 additions and 129 deletions

View File

@ -23,7 +23,7 @@ gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext
wg_set = Path(Path.home() / ".config/wire_py/settings")
#wg_set = Path(Path.home() / ".config/wire_py/settings")
class Create:
@ -135,30 +135,30 @@ class LxTools(tk.Tk):
super().__init__(*args, **kwargs)
def theme_change_light(self):
def theme_change_light(self, file=None):
"""
Set light theme
"""
if self.tk.call("ttk::style", "theme", "use") == "water-dark":
self.tk.call("set_theme", "light")
with open(wg_set, "r", encoding="utf-8") as theme_set2:
with open(file, "r", encoding="utf-8") as theme_set2:
lines3 = theme_set2.readlines()
lines3[3] = "light\n"
with open(wg_set, "w", encoding="utf-8") as theme_set2:
with open(file, "w", encoding="utf-8") as theme_set2:
theme_set2.writelines(lines3)
self.color_label()
def theme_change_dark(self):
def theme_change_dark(self, file=None):
"""
Set dark theme
"""
if not self.tk.call("ttk::style", "theme", "use") == "water-dark":
self.tk.call("set_theme", "dark")
with open(wg_set, "r", encoding="utf-8") as theme_set2:
with open(file, "r", encoding="utf-8") as theme_set2:
lines4 = theme_set2.readlines()
lines4[3] = "dark\n"
with open(wg_set, "w", encoding="utf-8") as theme_set2:
with open(file, "w", encoding="utf-8") as theme_set2:
theme_set2.writelines(lines4)
self.color_label()
@ -176,6 +176,106 @@ class LxTools(tk.Tk):
with open(file, "w", encoding="utf-8") as f:
f.write(logname)
@staticmethod
def clean_files(dirname=None, path=None):
if dirname != None:
shutil.rmtree(dirname)
if path != None:
Path.unlink(f"{path}")
@staticmethod
def if_tip(path):
"""
method that writes in file whether tooltip is displayed or not
"""
with open(path, "r", encoding="utf-8") as set_f2:
lines2 = set_f2.readlines()
if "False\n" in lines2:
tip = False
else:
tip = True
return tip
@staticmethod
def msg_window(img_w, img_i, w_title, w_txt, txt2=None, com=None):
"""
Method for different message windows for the user. with 4 arguments to be passed.
To create messages with your own images, icons, and titles.
As an alternative to Python Messagebox.
Paths to images must be specified: r'/usr/share/icons/lx-icons/64/info.png'
img_w = Image for Tk Window
img_i = Image for Icon
w_title = Windows Title
w_txt = Text for Tk Window
txt2 = Text for Button two
com = function for Button command
"""
msg = tk.Toplevel()
msg.resizable(width=False, height=False)
msg.title(w_title)
msg.configure(pady=15, padx=15)
msg.img = tk.PhotoImage(file=img_w)
msg.i_window = tk.Label(msg, image=msg.img)
label = tk.Label(msg, text=w_txt)
label.grid(column=1, row=0)
if txt2 is not None and com is not None:
label.config(font=("Ubuntu", 11), padx=15, justify="left")
msg.i_window.grid(column=0, row=0, sticky="nw")
button2 = ttk.Button(msg, text=f"{txt2}", command=com, padding=4)
button2.grid(column=0, row=1, sticky="e", columnspan=2)
button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
button.grid(column=0, row=1, sticky="w", columnspan=2)
else:
label.config(font=("Ubuntu", 11), padx=15)
msg.i_window.grid(column=0, row=0)
button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
button.grid(column=0, columnspan=2, row=1)
img_i = tk.PhotoImage(file=img_i)
msg.iconphoto(True, img_i)
msg.columnconfigure(0, weight=1)
msg.rowconfigure(0, weight=1)
msg.winfo_toplevel()
@staticmethod
def sigi(dirname=None, path=None):
"""
function for clean up after break
"""
def signal_handler(signum, frame):
"""
Determine clear text names for signal numbers
"""
signals_to_names_dict = dict(
(getattr(signal, n), n)
for n in dir(signal)
if n.startswith("SIG") and "_" not in n
)
signame = signals_to_names_dict.get(signum, f"Unnamed signal: {signum}")
# End program for certain signals, report to others only reception
if signum in (signal.SIGINT, signal.SIGTERM):
exit_code = 1
print(
f"\nSignal {signame} {(signum)} received. => Aborting with exit code {exit_code}."
)
LxTools.clean_files(dirname)
print("Breakdown by user...")
sys.exit(exit_code)
else:
print(f"Signal {signum} received and ignored.")
LxTools.clean_files(dirname)
print("Process unexpectedly ended...")
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)
class GiteaUpdate:
"""
@ -185,7 +285,7 @@ class GiteaUpdate:
"""
@staticmethod
def api_down(update_api_url, version):
def api_down(update_api_url, version, file=None):
"""
Calling api_down requests the URL and the version of the running script.
Example: version = 'v. 1.1.1.1' GiteaUpdate.api_down(http://example.de, version)
@ -194,7 +294,7 @@ class GiteaUpdate:
response = requests.get(update_api_url, timeout=10)
response_dict = response.json()
response_dict = response_dict[0]
with open(wg_set, "r", encoding="utf-8") as set_f:
with open(file, "r", encoding="utf-8") as set_f:
set_f = set_f.read()
if "on\n" in set_f:
if version[3:] != response_dict["tag_name"]:
@ -224,7 +324,7 @@ class GiteaUpdate:
ii = down_ok_image
wt = _("Download Successful")
msg_t = _("Your zip file is in home directory")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -233,7 +333,8 @@ class GiteaUpdate:
ii = down_not_ok_image
wt = _("Download error")
msg_t = _("Download failed! Please try again")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
except subprocess.CalledProcessError:
# img_w, img_i, w_title, w_txt hand over
@ -241,52 +342,7 @@ class GiteaUpdate:
ii = down_not_ok_image
wt = _("Download error")
msg_t = _("Download failed! No internet connection!")
msg_window(iw, ii, wt, msg_t)
def msg_window(img_w, img_i, w_title, w_txt, txt2=None, com=None):
"""
Function for different message windows for the user. with 4 arguments to be passed.
To create messages with your own images, icons, and titles.
As an alternative to Python Messagebox.
Paths to images must be specified: r'/usr/share/icons/lx-icons/64/info.png'
img_w = Image for Tk Window
img_i = Image for Icon
w_title = Windows Title
w_txt = Text for Tk Window
txt2 = Text for Button two
com = function for Button command
"""
msg = tk.Toplevel()
msg.resizable(width=False, height=False)
msg.title(w_title)
msg.configure(pady=15, padx=15)
msg.img = tk.PhotoImage(file=img_w)
msg.i_window = tk.Label(msg, image=msg.img)
label = tk.Label(msg, text=w_txt)
label.grid(column=1, row=0)
if txt2 is not None and com is not None:
label.config(font=("Ubuntu", 11), padx=15, justify="left")
msg.i_window.grid(column=0, row=0, sticky="nw")
button2 = ttk.Button(msg, text=f"{txt2}", command=com, padding=4)
button2.grid(column=0, row=1, sticky="e", columnspan=2)
button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
button.grid(column=0, row=1, sticky="w", columnspan=2)
else:
label.config(font=("Ubuntu", 11), padx=15)
msg.i_window.grid(column=0, row=0)
button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
button.grid(column=0, columnspan=2, row=1)
img_i = tk.PhotoImage(file=img_i)
msg.iconphoto(True, img_i)
msg.columnconfigure(0, weight=1)
msg.rowconfigure(0, weight=1)
msg.winfo_toplevel()
LxTools.msg_window(iw, ii, wt, msg_t)
class Tunnel:
@ -393,7 +449,7 @@ class Tunnel:
ii = r"/usr/share/icons/lx-icons/48/wg_vpn.png"
wt = _("Export Successful")
msg_t = _("Your zip file is in home directory")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -402,7 +458,7 @@ class Tunnel:
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Export error")
msg_t = _("Export failed! Please try again")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -411,60 +467,12 @@ class Tunnel:
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Select tunnel")
msg_t = _("Please first import tunnel")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
except TypeError:
pass
def sigi(dirname):
"""
function for clean up after break
"""
def signal_handler(signum, frame):
"""
Determine clear text names for signal numbers
"""
signals_to_names_dict = dict(
(getattr(signal, n), n)
for n in dir(signal)
if n.startswith("SIG") and "_" not in n
)
signame = signals_to_names_dict.get(signum, f"Unnamed signal: {signum}")
# End program for certain signals, report to others only reception
if signum in (signal.SIGINT, signal.SIGTERM):
exit_code = 1
print(
f"\nSignal {signame} {(signum)} received. => Aborting with exit code {exit_code}."
)
shutil.rmtree(dirname)
Path.unlink("/tmp/.loguser")
print("Breakdown by user...")
sys.exit(exit_code)
else:
print(f"Signal {signum} received and ignored.")
shutil.rmtree(dirname)
Path.unlink("/tmp/.loguser")
print("Process unexpectedly ended...")
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)
def if_tip(path):
"""
method that writes in file whether tooltip is displayed or not
"""
with open(path, "r", encoding="utf-8") as set_f2:
lines2 = set_f2.readlines()
if "False\n" in lines2:
tip = False
else:
tip = True
return tip
class Tooltip:

View File

@ -14,8 +14,7 @@ from pathlib import Path
from subprocess import check_call
from tkinter import TclError, filedialog, ttk
from cls_mth_fc import (Create, GiteaUpdate, Tooltip, Tunnel, if_tip,
LxTools, msg_window, sigi)
from cls_mth_fc import (Create, GiteaUpdate, Tooltip, Tunnel, LxTools)
LxTools.uos()
Create.dir_and_files()
@ -23,15 +22,16 @@ Create.make_dir()
Create.decrypt()
tcl_path = Path("/usr/share/TK-Themes")
wg_set = Path(Path.home() / ".config/wire_py/settings")
tips = if_tip(wg_set)
set_file = Path(Path.home() / ".config/wire_py/settings")
tips = LxTools.if_tip(set_file)
dirname = Path("/tmp/tlecdcwg/")
userfile = Path("/tmp/.loguser")
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
VERSION = "v. 2.04.1725"
res = GiteaUpdate.api_down(
"https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION
"https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION, set_file
)
# Translate
@ -42,7 +42,7 @@ gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext
sigi(dirname)
LxTools.sigi(dirname, userfile)
class Wirepy(tk.Tk):
@ -168,7 +168,7 @@ class FrameWidgets(ttk.Frame):
txt2 = _("Go to Wire-Py git")
com = link_btn
msg_window(iw, ii, wt, msg_t, txt2, com)
LxTools.msg_window(iw, ii, wt, msg_t, txt2, com)
# Frame for Menu
self.menu_frame = ttk.Frame(self)
@ -197,8 +197,8 @@ class FrameWidgets(ttk.Frame):
self.settings.add_checkbutton(
label=_("Disable Tooltips"), command=tooltip, variable=set_tip
)
self.settings.add_command(label=_("Light"), command=lambda: LxTools.theme_change_light(self))
self.settings.add_command(label=_("Dark"), command=lambda: LxTools.theme_change_dark(self))
self.settings.add_command(label=_("Light"), command=lambda: LxTools.theme_change_light(self, set_file))
self.settings.add_command(label=_("Dark"), command=lambda: LxTools.theme_change_dark(self, set_file))
# About BTN Menu / Label
self.about_btn = ttk.Button(
@ -427,7 +427,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Select tunnel")
msg_t = _("Please select a tunnel from the list")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -436,7 +436,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Select tunnel")
msg_t = _("Please first import tunnel")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
# Button Trash
self.btn_tr = ttk.Button(
@ -500,7 +500,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Renaming not possible")
msg_t = _("The new name may contain only 12 characters")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
elif len(self.lb_rename.get()) == 0:
@ -509,7 +509,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Renaming not possible")
msg_t = _("At least one character must be entered")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
elif any(ch in special_characters for ch in self.lb_rename.get()):
@ -520,7 +520,7 @@ class FrameWidgets(ttk.Frame):
msg_t = _(
"No valid sign. These must not be used.\nBlank, Slash, Backslash and { }\n"
)
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -572,7 +572,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Renaming not possible")
msg_t = _("Please select a tunnel from the list")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
# Button Rename
self.btn_rename = ttk.Button(
@ -678,7 +678,7 @@ class FrameWidgets(ttk.Frame):
msg_t = _(
"Tunnel already available!\nPlease use another file for import"
)
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -827,7 +827,7 @@ class FrameWidgets(ttk.Frame):
msg_t = _(
"Oh... no valid Wireguard File!\nPlease select a valid Wireguard File"
)
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
except EOFError as e:
print(e)
@ -1056,7 +1056,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Select tunnel")
msg_t = _("Please select a tunnel from the list")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -1065,7 +1065,7 @@ class FrameWidgets(ttk.Frame):
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Select tunnel")
msg_t = _("Please first import tunnel")
msg_window(iw, ii, wt, msg_t)
LxTools.msg_window(iw, ii, wt, msg_t)
if __name__ == "__main__":
@ -1081,6 +1081,5 @@ if __name__ == "__main__":
window.tk.call("set", "::tk::dialog::file::showHiddenVar", "0")
window.mainloop()
shutil.rmtree(dirname)
Path.unlink("/tmp/.loguser")
LxTools.clean_files(dirname, userfile)
sys.exit(0)