improve error handling

This commit is contained in:
matthias@arch 2023-12-16 00:54:49 +01:00
parent e88cf6b98d
commit e0ce0d1b1d

View File

@ -1,6 +1,7 @@
import subprocess import subprocess
from os import path from os import path
from .globals import error
class UeberzugLayer(): class UeberzugLayer():
"""Wrapper for Ueberzug++""" """Wrapper for Ueberzug++"""
@ -11,9 +12,9 @@ class UeberzugLayer():
self._pid = None 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) 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: 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): 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: with open(pid_file, "r") as file:
try: try:
self._pid = int(file.read()) 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"): 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)]) 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: 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"): def remove_image(self, identifier="Image"):
ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "remove", "-i", identifier]) ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "remove", "-i", identifier])
if not ret.returncode == 0: 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): def __del__(self):
from os import remove from os import remove
@ -39,7 +42,8 @@ class UeberzugLayer():
remove(self._pid_file) remove(self._pid_file)
except: except:
pass pass
import subprocess # might be unloaded if self._socket is not None:
ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "exit"]) import subprocess # might be unloaded
if not ret.returncode == 0: ret = subprocess.run(["ueberzug", "cmd", "-s", self._socket, "-a", "exit"])
raise Exception(f"ueberzug cmd exited with {ret.returncode}") if not ret.returncode == 0:
error(f"UeberzugLayer.__del__: ueberzug layer exited with {ret.returncode}")