From e0ce0d1b1dbbc98ce18cdce50c0a4668ad3307fb Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Sat, 16 Dec 2023 00:54:49 +0100 Subject: [PATCH] improve error handling --- imgsort/ueberzug.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/imgsort/ueberzug.py b/imgsort/ueberzug.py index 7e62418..fee9686 100644 --- a/imgsort/ueberzug.py +++ b/imgsort/ueberzug.py @@ -1,6 +1,7 @@ import subprocess from os import path +from .globals import error class UeberzugLayer(): """Wrapper for Ueberzug++""" @@ -11,9 +12,9 @@ class UeberzugLayer(): self._pid = None ret = subprocess.run(["ueberzug", "layer", "--pid-file", pid_file, "--no-stdin", "--no-opencv" if no_opencv else ""], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) if not ret.returncode == 0: - raise Exception(f"ueberzug layer exited with {ret.returncode}") + error(f"UeberzugLayer.init: ueberzug layer exited with {ret.returncode}") if not path.isfile(pid_file): - raise Exception(f"Ueberzug pid file not found: {pid_file}") + error(f"UeberzugLayer.init: can not find ueberzug pid file at '{pid_file}'") with open(pid_file, "r") as file: try: self._pid = int(file.read()) @@ -26,12 +27,14 @@ class UeberzugLayer(): def display_image(self, image, x=0, y=0, max_width=0, max_height=0, identifier="Image"): ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "add", "-i", identifier, "-f", image, "-x", str(x), "-y", str(y), "--max-width", str(max_width), "--max-height", str(max_height)]) if not ret.returncode == 0: - raise Exception(f"ueberzug cmd exited with {ret.returncode}") + self._socket = None + error(f"UeberzugLayer.display_image: ueberzug layer exited with {ret.returncode}") def remove_image(self, identifier="Image"): ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "remove", "-i", identifier]) if not ret.returncode == 0: - raise Exception(f"ueberzug cmd exited with {ret.returncode}") + self._socket = None + error(f"UeberzugLayer.remove_image: ueberzug layer exited with {ret.returncode}") def __del__(self): from os import remove @@ -39,7 +42,8 @@ class UeberzugLayer(): remove(self._pid_file) except: pass - import subprocess # might be unloaded - ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "exit"]) - if not ret.returncode == 0: - raise Exception(f"ueberzug cmd exited with {ret.returncode}") + if self._socket is not None: + import subprocess # might be unloaded + ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "exit"]) + if not ret.returncode == 0: + error(f"UeberzugLayer.__del__: ueberzug layer exited with {ret.returncode}")