From 3ffbf7f20f3bdb8a162e2ecab608ac0a013a4b3c Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Fri, 12 Feb 2021 13:29:24 -0500 Subject: [PATCH] Handle exceptions and exit codes better --- lightcards/display.py | 7 +++++++ lightcards/parse.py | 8 +++++--- lightcards/runner.py | 10 +++++++--- man/lightcards.1 | 3 +++ man/lightcards.1.md | 3 +++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lightcards/display.py b/lightcards/display.py index 4739982..8340c27 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -19,6 +19,13 @@ def panel_create(x, y): return (win, panel) +class CursesError(BaseException): + def __init__(self, message="lightcards: Curses error!"): + self.message = message + print(self.message) + sys.exit(3) + + class Help: def __init__(self, outer, mlines=21, mcols=52): """Initialize help screen""" diff --git a/lightcards/parse.py b/lightcards/parse.py index 6fc2a75..3a1306b 100644 --- a/lightcards/parse.py +++ b/lightcards/parse.py @@ -13,7 +13,9 @@ def md2html(file): try: return markdown.markdown(open(file, "r").read(), extensions=["tables"]) except FileNotFoundError: - sys.exit(f'lightcards: "{file}": No such file or directory') + raise Exception( + f'lightcards: "{file}": No such file or directory' + ) from None def parse_html(html): @@ -29,11 +31,11 @@ def parse_html(html): for x in soup.find_all("tr"): outp.append(Card(tuple([clean_text(y) for y in x.find_all("td")]))) except AttributeError: - sys.exit("lightcards: No table found") + raise Exception("lightcards: No table found") from None ths = soup.find_all("th") if len(ths) != 2: - sys.exit("lightcards: Headings malformed") + raise Exception("lightcards: Headings malformed") # Return a tuple of nested lists return ([clean_text(x) for x in ths], outp[1:]) diff --git a/lightcards/runner.py b/lightcards/runner.py index 08004db..e0af7d9 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -2,14 +2,14 @@ # Armaan Bhojwani 2021 import argparse -from curses import wrapper +import curses import os import pkg_resources from random import shuffle import sys from . import parse, progress -from .display import Display +from .display import Display, CursesError from .deck import Status @@ -80,7 +80,10 @@ def show(args, stack, headers): # Send to display win = Display(stack, headers, idx, args.view) - wrapper(win.run) + try: + curses.wrapper(win.run) + except curses.error as e: + raise CursesError() from e def reparse(): @@ -96,6 +99,7 @@ def get_orig(): def main(args=sys.argv): + sys.tracebacklimit = 0 args = parse_args() global headers, stack (headers, stack) = parse.parse_html(parse.md2html(args.inp[0])) diff --git a/man/lightcards.1 b/man/lightcards.1 index 47115f4..37b3866 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -82,6 +82,9 @@ Parse error .TP \f[B]2\f[R] Invalid option +.TP +\f[B]3\f[R] +Curses error .SH BUGS .PP https://lists.sr.ht/\[ti]armaan/public-inbox diff --git a/man/lightcards.1.md b/man/lightcards.1.md index eb83e03..78865eb 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -83,6 +83,9 @@ Lightcards takes the first table from a valid Markdown or HTML file. Each row is **2** : Invalid option +**3** +: Curses error + # BUGS https://lists.sr.ht/~armaan/public-inbox -- 2.39.2