From 9ecf39499a70f7d1e8977aed66fd041356bd9b58 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sat, 26 Oct 2024 19:46:13 +0200 Subject: Introduce small helper function for sys.exit This saves us from specifying the program name every single time we want the program to exit with an error message. --- later | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/later b/later index 61d8abe..5b82a6e 100755 --- a/later +++ b/later @@ -21,6 +21,10 @@ class YTDLPLogger: print("later: yt-dlp:", msg, file=sys.stderr) +def exit(message=""): + sys.exit(f"later: {message}" if message else 0) + + def get_xdg(type, fallback): env = f"XDG_{type.upper()}_HOME" if env in os.environ: @@ -75,7 +79,7 @@ class TitleMap: # Clobber the title cache if it was corrupted self.commit_to_disk = True except Exception as err: - sys.exit(f"later: cannot read title cache: {err}") + exit(f"cannot read title cache: {err}") def mark_seen(self, key): self.seen.add(key) @@ -96,7 +100,7 @@ class TitleMap: # necessary: importing yt_dlp is noticeably slow :( import yt_dlp except ModuleNotFoundError: - sys.exit("later: yt-dlp was requested, but yt_dlp python module not found") + exit("yt-dlp was requested, but yt_dlp python module not found") if not self.ytdl: self.ytdl = yt_dlp.YoutubeDL({"logger": YTDLPLogger()}) @@ -131,7 +135,7 @@ class TitleMap: with open(self.path, "w") as handle: json.dump(seen_entries, handle) except OSError as err: - sys.exit(f"later: cannot write title cache: {err}") + exit(f"cannot write title cache: {err}") def entries(title_map): -- cgit v1.2.3-2-gb3c3