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
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
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:
raise Exception(f"ueberzug cmd exited with {ret.returncode}")
error(f"UeberzugLayer.__del__: ueberzug layer exited with {ret.returncode}")