From 60b55da973c591bd4e8fb072f7ff8d606816c8c9 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Tue, 21 Oct 2025 22:20:06 -0500 Subject: [PATCH] Moved to use contextlib suppress pattern than empty exception blocks --- src/__builtins__.py | 2 +- src/app.py | 12 +++++------- src/core/controllers/base_controller_data.py | 17 ++++++++--------- src/core/widgets/controls/open_files_button.py | 5 ++--- src/core/widgets/vte_widget.py | 10 +++++----- src/libs/singleton.py | 12 +++++------- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/__builtins__.py b/src/__builtins__.py index b1a8c6e..3642a81 100644 --- a/src/__builtins__.py +++ b/src/__builtins__.py @@ -77,4 +77,4 @@ def custom_except_hook(exc_type, exc_value, exc_traceback): logger.error("Uncaught exception", exc_info = (exc_type, exc_value, exc_traceback)) -sys.excepthook = custom_except_hook \ No newline at end of file +sys.excepthook = custom_except_hook diff --git a/src/app.py b/src/app.py index 32f1e0b..14eca5a 100644 --- a/src/app.py +++ b/src/app.py @@ -1,4 +1,5 @@ # Python imports +from contextlib import suppress import signal import os @@ -52,18 +53,15 @@ class Application: except Exception: ipc_server.send_test_ipc_message() - try: + with suppress(Exception): ipc_server.create_ipc_listener() - except Exception as e: - ... def setup_debug_hook(self): - try: + # Typically: ValueError: signal only works in main thread + with suppress(ValueError): # kill -SIGUSR2 from Linux/Unix or SIGBREAK signal from Windows signal.signal( vars(signal).get("SIGBREAK") or vars(signal).get("SIGUSR2"), debug_signal_handler ) - except ValueError: - # Typically: ValueError: signal only works in main thread - ... \ No newline at end of file + diff --git a/src/core/controllers/base_controller_data.py b/src/core/controllers/base_controller_data.py index 47088e4..966be21 100644 --- a/src/core/controllers/base_controller_data.py +++ b/src/core/controllers/base_controller_data.py @@ -46,9 +46,10 @@ class BaseControllerData: logger.info(f"Not a File: {arg}") - if len(files) > 0: - settings_manager.set_is_starting_with_file(True) - settings_manager.set_starting_files(files) + if len(files) == 0: return + + settings_manager.set_is_starting_with_file(True) + settings_manager.set_starting_files(files) def get_base_container(self): return self.base_container @@ -84,24 +85,22 @@ class BaseControllerData: widget.remove(child) def get_clipboard_data(self, encoding = "utf-8") -> str: - if which("xclip"): - command = ['xclip','-selection','clipboard'] - else: + if not which("xclip"): logger.info('xclip not found...') return + command = ['xclip','-selection','clipboard'] proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout = subprocess.PIPE) retcode = proc.wait() data = proc.stdout.read() return data.decode(encoding).strip() def set_clipboard_data(self, data: type, encoding = "utf-8") -> None: - if which("xclip"): - command = ['xclip','-selection','clipboard'] - else: + if not which("xclip"): logger.info('xclip not found...') return + command = ['xclip','-selection','clipboard'] proc = subprocess.Popen(command, stdin = subprocess.PIPE) proc.stdin.write(data.encode(encoding)) proc.stdin.close() diff --git a/src/core/widgets/controls/open_files_button.py b/src/core/widgets/controls/open_files_button.py index 42acf30..d29eaea 100644 --- a/src/core/widgets/controls/open_files_button.py +++ b/src/core/widgets/controls/open_files_button.py @@ -1,4 +1,5 @@ # Python imports +from contextlib import suppress import os # Lib imports @@ -58,11 +59,9 @@ class OpenFilesButton(Gtk.Button): chooser.set_select_multiple(True) - try: + with suppress(Exception): folder = widget.get_current_file().get_parent() if not start_dir else start_dir chooser.set_current_folder( folder.get_path() ) - except Exception as e: - ... response = chooser.run() if not response == Gtk.ResponseType.OK: diff --git a/src/core/widgets/vte_widget.py b/src/core/widgets/vte_widget.py index 608e84a..7a5f18d 100644 --- a/src/core/widgets/vte_widget.py +++ b/src/core/widgets/vte_widget.py @@ -99,7 +99,7 @@ class VteWidget(Vte.Terminal): try: command_ran = lines[-1].split("-->:")[1].strip() except VteWidgetException as e: - logger.debud(e) + logger.debug(e) return if not command_ran[0:3].encode() in self.cd_cmd_prefix: @@ -114,12 +114,12 @@ class VteWidget(Vte.Terminal): event = Event("pty_path_updated", "", target_path) event_system.emit("handle_bridge_event", (event,)) - def update_term_path(self, fpath): + def update_term_path(self, fpath: str): self.dont_process = True cmds = [f"cd '{fpath}'\n", "clear\n"] - for i in cmds: - self.run_command(i) + for cmd in cmds: + self.run_command(cmd) - def run_command(self, cmd): + def run_command(self, cmd: str): self.feed_child_binary(bytes(cmd, 'utf8')) \ No newline at end of file diff --git a/src/libs/singleton.py b/src/libs/singleton.py index 23b7191..b484b28 100644 --- a/src/libs/singleton.py +++ b/src/libs/singleton.py @@ -12,13 +12,11 @@ class SingletonError(Exception): class Singleton: - ccount = 0 + _instance = None def __new__(cls, *args, **kwargs): - obj = super(Singleton, cls).__new__(cls) - cls.ccount += 1 + if cls._instance: + raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...") - if cls.ccount == 2: - raise SingletonError(f"Exceeded {cls.__name__} instantiation limit...") - - return obj + cls._instance = super(Singleton, cls).__new__(cls) + return cls._instance