From 0c4d000d9644ad71c0ed7b6ca500e701fcdd8003 Mon Sep 17 00:00:00 2001 From: punix Date: Mon, 12 May 2025 16:48:48 +0200 Subject: [PATCH] add ckeck_key_is_exist() for import --- common_tools.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ wp_app_config.py | 7 +++++++ 2 files changed, 58 insertions(+) diff --git a/common_tools.py b/common_tools.py index 4b429fb..ee823cb 100755 --- a/common_tools.py +++ b/common_tools.py @@ -81,6 +81,57 @@ class LxTools(tk.Tk): def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) + @staticmethod + def ckeck_key_is_exist( + directorys: list[str], search_string: str = None + ) -> bool | None: + """ + Check if the key is exist in the file + Args: + directorys (list[str]): list of directories to search in + search_string (str): string to search for + Returns: + bool: True if the key is found, False otherwise + """ + + if search_string: + result = False + + for directory in directorys: + + in_paths = Path(directory) + + if not in_paths.exists() or not in_paths.is_dir(): + continue + + # Get a list of all files in the directorys + files = [file for file in in_paths.iterdir() if file.is_file()] + if not files: + continue + + # Search for the string in the files + for file in files: + try: + with open(file, "r", errors="ignore") as f: + for line in f: + if search_string in line: + # Set the result to True if the string is found + result = True + break + + # If the string is found, stop searching for the string in other files + if result: + break + + except Exception: + # Ignore errors and continue to the next file + continue + + else: + result = None + print(result) + return result + @staticmethod def center_window_cross_platform(window, width, height): """ diff --git a/wp_app_config.py b/wp_app_config.py index 8f34591..b9d07e5 100644 --- a/wp_app_config.py +++ b/wp_app_config.py @@ -57,6 +57,13 @@ class AppConfig: "pkey_path": "/usr/local/etc/ssl/pwgk.pem", } + # Lists of searches + DIRECTORYS: list[str] = [ + "/etc/netplan/", + "/etc/NetworkManager/system-connections/", + "/var/lib/NetworkManager/user-connections/", + ] + # Images and icons paths IMAGE_PATHS: Dict[str, str] = { "icon_vpn": "/usr/share/icons/lx-icons/48/wg_vpn.png",