fix new tooltip signal now in modul cls_mth_fc.py

This commit is contained in:
Désiré Werner Menrath 2025-04-22 14:22:39 +02:00
parent 47bdfbfb17
commit 5753a35d6c
3 changed files with 90 additions and 74 deletions

View File

@ -4,14 +4,15 @@ import gettext
import locale import locale
import os import os
import shutil import shutil
import signal
import subprocess import subprocess
import sys
import tkinter as tk import tkinter as tk
import zipfile import zipfile
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from subprocess import check_call from subprocess import check_call
from tkinter import ttk from tkinter import ttk
import requests import requests
APP = "wirepy" APP = "wirepy"
@ -378,6 +379,43 @@ class Tunnel:
pass 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): def if_tip(path):
""" """
method that writes in file whether tooltip is displayed or not method that writes in file whether tooltip is displayed or not
@ -402,21 +440,24 @@ class Tooltip:
info: label and button is parrent. info: label and button is parrent.
""" """
def __init__(self, widget, text, TIPS=None): def __init__(self, widget, text, tips=None):
self.widget = widget self.widget = widget
self.text = text self.text = text
self.tooltip_window = None self.tooltip_window = None
if TIPS: if tips:
self.widget.bind("<Enter>", self.show_tooltip) self.widget.bind("<Enter>", self.show_tooltip)
self.widget.bind("<Leave>", self.hide_tooltip) self.widget.bind("<Leave>", self.hide_tooltip)
def show_tooltip(self, event=None): def show_tooltip(self, event=None):
"""
shows the tooltip
"""
if self.tooltip_window or not self.text: if self.tooltip_window or not self.text:
return return
x, y, _, _ = self.widget.bbox("insert") x, y, _, _ = self.widget.bbox("insert")
x += self.widget.winfo_rootx() + 25 x += self.widget.winfo_rootx() + 40
y += self.widget.winfo_rooty() + 20 y += self.widget.winfo_rooty() + 40
self.tooltip_window = tw = tk.Toplevel(self.widget) self.tooltip_window = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(True) tw.wm_overrideredirect(True)
tw.wm_geometry(f"+{x}+{y}") tw.wm_geometry(f"+{x}+{y}")
@ -427,6 +468,9 @@ class Tooltip:
label.grid() label.grid()
def hide_tooltip(self, event=None): def hide_tooltip(self, event=None):
"""
hide the tooltip
"""
if self.tooltip_window: if self.tooltip_window:
self.tooltip_window.destroy() self.tooltip_window.destroy()
self.tooltip_window = None self.tooltip_window = None

110
wirepy.py
View File

@ -7,14 +7,22 @@ import locale
import webbrowser import webbrowser
import os import os
import sys import sys
import signal
import subprocess import subprocess
import shutil import shutil
import tkinter as tk import tkinter as tk
from pathlib import Path from pathlib import Path
from subprocess import check_call from subprocess import check_call
from tkinter import filedialog, ttk, TclError from tkinter import filedialog, ttk, TclError
from cls_mth_fc import Tooltip, Tunnel, Create, msg_window, if_tip, GiteaUpdate, uos from cls_mth_fc import (
Tooltip,
Tunnel,
Create,
msg_window,
sigi,
if_tip,
GiteaUpdate,
uos,
)
uos() uos()
Create.dir_and_files() Create.dir_and_files()
@ -23,7 +31,7 @@ Create.decrypt()
tcl_path = Path("/usr/share/TK-Themes") tcl_path = Path("/usr/share/TK-Themes")
wg_set = Path(Path.home() / ".config/wire_py/settings") wg_set = Path(Path.home() / ".config/wire_py/settings")
TIPS = if_tip(wg_set) tips = if_tip(wg_set)
dirname = Path("/tmp/tlecdcwg/") dirname = Path("/tmp/tlecdcwg/")
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year # 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
@ -41,38 +49,7 @@ gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP) gettext.textdomain(APP)
_ = gettext.gettext _ = gettext.gettext
sigi(dirname)
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)
class Wirepy(tk.Tk): class Wirepy(tk.Tk):
@ -238,12 +215,12 @@ class FrameWidgets(ttk.Frame):
self.version_lb.config(font=("Ubuntu", 11), foreground="#00c4ff") self.version_lb.config(font=("Ubuntu", 11), foreground="#00c4ff")
self.version_lb.grid(column=0, row=0, rowspan=4, padx=10) self.version_lb.grid(column=0, row=0, rowspan=4, padx=10)
Tooltip(self.version_lb, f"Version: {VERSION[2:]}", TIPS) Tooltip(self.version_lb, f"Version: {VERSION[2:]}", tips)
self.options_btn = ttk.Menubutton(self.menu_frame, text=_("Options")) self.options_btn = ttk.Menubutton(self.menu_frame, text=_("Options"))
self.options_btn.grid(column=1, columnspan=1, row=0) self.options_btn.grid(column=1, columnspan=1, row=0)
Tooltip(self.options_btn, _("Click for Settings"), TIPS) Tooltip(self.options_btn, _("Click for Settings"), tips)
set_update = tk.IntVar() set_update = tk.IntVar()
set_tip = tk.BooleanVar() set_tip = tk.BooleanVar()
@ -270,7 +247,7 @@ class FrameWidgets(ttk.Frame):
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10) self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
# View Checkbox for enable or disable Tooltip # View Checkbox for enable or disable Tooltip
if TIPS: if tips:
set_tip.set(value=False) set_tip.set(value=False)
else: else:
set_tip.set(value=True) set_tip.set(value=True)
@ -280,14 +257,14 @@ class FrameWidgets(ttk.Frame):
set_update.set(value=1) set_update.set(value=1)
self.updates_lb.configure(text=_("Update search off")) self.updates_lb.configure(text=_("Update search off"))
Tooltip(self.updates_lb, _("Updates you have disabled"), TIPS) Tooltip(self.updates_lb, _("Updates you have disabled"), tips)
elif res == "No Internet Connection!": elif res == "No Internet Connection!":
self.updates_lb.configure(text=_("No Server Connection!"), foreground="red") self.updates_lb.configure(text=_("No Server Connection!"), foreground="red")
elif res == "No Updates": elif res == "No Updates":
self.updates_lb.configure(text=_("No Updates")) self.updates_lb.configure(text=_("No Updates"))
Tooltip(self.updates_lb, _("Congratulations! Wire-Py is up to date"), TIPS) Tooltip(self.updates_lb, _("Congratulations! Wire-Py is up to date"), tips)
else: else:
set_update.set(value=0) set_update.set(value=0)
@ -297,7 +274,7 @@ class FrameWidgets(ttk.Frame):
self.update_btn = ttk.Menubutton(self.menu_frame, text=text) self.update_btn = ttk.Menubutton(self.menu_frame, text=text)
self.update_btn.grid(column=4, columnspan=3, row=0, padx=0) self.update_btn.grid(column=4, columnspan=3, row=0, padx=0)
Tooltip(self.update_btn, _("Click to download new version"), TIPS) Tooltip(self.update_btn, _("Click to download new version"), tips)
self.download = tk.Menu(self, relief="flat") self.download = tk.Menu(self, relief="flat")
@ -420,7 +397,7 @@ class FrameWidgets(ttk.Frame):
) )
self.btn_i.grid(column=0, row=1, padx=15, pady=8) self.btn_i.grid(column=0, row=1, padx=15, pady=8)
Tooltip(self.btn_i, _("Click to import a Wireguard Tunnel"), TIPS) Tooltip(self.btn_i, _("Click to import a Wireguard Tunnel"), tips)
def delete(): def delete():
""" """
@ -473,23 +450,20 @@ class FrameWidgets(ttk.Frame):
if self.l_box.size() == 0: if self.l_box.size() == 0:
self.wg_autostart.configure(state="disabled") self.wg_autostart.configure(state="disabled")
self.lb_rename.configure(state="disabled") self.lb_rename.configure(state="disabled")
Tooltip(
self.l_box, _("You must first import\na Wireguard tunnel"), TIPS
)
Tooltip( Tooltip(
self.wg_autostart, self.wg_autostart,
_( _(
"You must have at least one\ntunnel in the list,to use the autostart" "You must have at least one\ntunnel in the list,to use the autostart"
), ),
TIPS, tips,
) )
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), TIPS) Tooltip(self.btn_exp, _("No Tunnels in List for Export"), tips)
Tooltip(self.btn_stst, _("No tunnels to start in the list"), TIPS) Tooltip(self.btn_stst, _("No tunnels to start in the list"), tips)
Tooltip( Tooltip(
self.lb_rename, self.lb_rename,
_("To rename a tunnel, at least one must be in the list"), _("To rename a tunnel, at least one must be in the list"),
TIPS, tips,
) )
self.lb_rename.insert(0, _("Max. 12 characters!")) self.lb_rename.insert(0, _("Max. 12 characters!"))
@ -535,12 +509,12 @@ class FrameWidgets(ttk.Frame):
self.btn_tr.grid(column=0, row=2, padx=15, pady=8) self.btn_tr.grid(column=0, row=2, padx=15, pady=8)
if self.l_box.size() == 0: if self.l_box.size() == 0:
Tooltip(self.btn_tr, _("No tunnels to delete in the list"), TIPS) Tooltip(self.btn_tr, _("No tunnels to delete in the list"), tips)
else: else:
Tooltip( Tooltip(
self.btn_tr, self.btn_tr,
_("Click to delete a Wireguard Tunnel\nSelect from the list!"), _("Click to delete a Wireguard Tunnel\nSelect from the list!"),
TIPS, tips,
) )
# Button Export # Button Export
@ -550,12 +524,12 @@ class FrameWidgets(ttk.Frame):
self.btn_exp.grid(column=0, row=3, padx=15, pady=8) self.btn_exp.grid(column=0, row=3, padx=15, pady=8)
if self.l_box.size() == 0: if self.l_box.size() == 0:
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), TIPS) Tooltip(self.btn_exp, _("No Tunnels in List for Export"), tips)
else: else:
Tooltip( Tooltip(
self.btn_exp, self.btn_exp,
_(" Click to export all\nWireguard Tunnel to Zipfile"), _(" Click to export all\nWireguard Tunnel to Zipfile"),
TIPS, tips,
) )
# Label Entry # Label Entry
@ -568,13 +542,13 @@ class FrameWidgets(ttk.Frame):
Tooltip( Tooltip(
self.lb_rename, self.lb_rename,
_("To rename a tunnel, you need to\nselect a tunnel from the list"), _("To rename a tunnel, you need to\nselect a tunnel from the list"),
TIPS, tips,
) )
else: else:
Tooltip( Tooltip(
self.lb_rename, self.lb_rename,
_("To rename a tunnel, at least one must be in the list"), _("To rename a tunnel, at least one must be in the list"),
TIPS, tips,
) )
def tl_rename(): def tl_rename():
@ -698,7 +672,7 @@ class FrameWidgets(ttk.Frame):
if self.l_box.size() >= 1 and len(self.l_box.curselection()) >= 1: if self.l_box.size() >= 1 and len(self.l_box.curselection()) >= 1:
Tooltip( Tooltip(
self.wg_autostart, _("To use the autostart, enable this Checkbox"), TIPS self.wg_autostart, _("To use the autostart, enable this Checkbox"), tips
) )
if self.l_box.size() == 0: if self.l_box.size() == 0:
@ -707,7 +681,7 @@ class FrameWidgets(ttk.Frame):
_( _(
"You must have at least one\ntunnel in the list,to use the autostart" "You must have at least one\ntunnel in the list,to use the autostart"
), ),
TIPS, tips,
) )
else: else:
@ -715,7 +689,7 @@ class FrameWidgets(ttk.Frame):
Tooltip( Tooltip(
self.wg_autostart, self.wg_autostart,
_("To use the autostart, a tunnel must be selected from the list"), _("To use the autostart, a tunnel must be selected from the list"),
TIPS, tips,
) )
self.on_off() self.on_off()
@ -858,7 +832,7 @@ class FrameWidgets(ttk.Frame):
Tooltip( Tooltip(
self.wg_autostart, self.wg_autostart,
_("To use the autostart, enable this Checkbox"), _("To use the autostart, enable this Checkbox"),
TIPS, tips,
) )
# Tooltip(self.l_box, _("List of available tunnels")) # Tooltip(self.l_box, _("List of available tunnels"))
@ -869,7 +843,7 @@ class FrameWidgets(ttk.Frame):
"Click to delete a Wireguard Tunnel\n" "Click to delete a Wireguard Tunnel\n"
"Select from the list!" "Select from the list!"
), ),
TIPS, tips,
) )
Tooltip( Tooltip(
@ -878,7 +852,7 @@ class FrameWidgets(ttk.Frame):
" Click to export all\n" " Click to export all\n"
"Wireguard Tunnel to Zipfile" "Wireguard Tunnel to Zipfile"
), ),
TIPS, tips,
) )
Tooltip( Tooltip(
@ -887,7 +861,7 @@ class FrameWidgets(ttk.Frame):
"To rename a tunnel, you need to\n" "To rename a tunnel, you need to\n"
"select a tunnel from the list" "select a tunnel from the list"
), ),
TIPS, tips,
) )
self.lb_rename.insert(0, "Max. 12 characters!") self.lb_rename.insert(0, "Max. 12 characters!")
@ -932,11 +906,10 @@ class FrameWidgets(ttk.Frame):
except EOFError as e: except EOFError as e:
print(e) print(e)
except TypeError as e: except TypeError as e:
print(e) print("File import: abort by user...")
except FileNotFoundError as e: except FileNotFoundError as e:
print(e) print("File import: abort by user...")
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print("Tunnel exist!") print("Tunnel exist!")
def box_set(self): def box_set(self):
@ -1063,7 +1036,7 @@ class FrameWidgets(ttk.Frame):
) )
self.btn_stst.grid(column=0, row=0, padx=5, pady=8) self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
Tooltip(self.btn_stst, _("Click to stop selected Wireguard Tunnel"), TIPS) Tooltip(self.btn_stst, _("Click to stop selected Wireguard Tunnel"), tips)
def start(self): def start(self):
""" """
@ -1079,9 +1052,9 @@ class FrameWidgets(ttk.Frame):
tl = Tunnel.list() tl = Tunnel.list()
if len(tl) == 0: if len(tl) == 0:
Tooltip(self.btn_stst, _("No tunnels to start in the list"), TIPS) Tooltip(self.btn_stst, _("No tunnels to start in the list"), tips)
else: else:
Tooltip(self.btn_stst, _("Click to start selected Wireguard Tunnel"), TIPS) Tooltip(self.btn_stst, _("Click to start selected Wireguard Tunnel"), tips)
def color_label(self): def color_label(self):
""" """
@ -1178,7 +1151,6 @@ if __name__ == "__main__":
window.tk.call("tk_getOpenFile", "-foobarbaz") window.tk.call("tk_getOpenFile", "-foobarbaz")
except TclError: except TclError:
pass pass
window.tk.call("set", "::tk::dialog::file::showHiddenBtn", "0") window.tk.call("set", "::tk::dialog::file::showHiddenBtn", "0")
window.tk.call("set", "::tk::dialog::file::showHiddenVar", "0") window.tk.call("set", "::tk::dialog::file::showHiddenVar", "0")
window.mainloop() window.mainloop()