From 061222558b79551ea85921654d55cceba5a5e7b7 Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Sat, 6 Jun 2026 02:27:59 +0200 Subject: [PATCH] handle quitters --- config-template.toml | 3 +++ src/worblehat/cli/main.py | 27 ++++++++------------------- src/worblehat/main.py | 3 ++- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/config-template.toml b/config-template.toml index ad0c38d..af7abd9 100644 --- a/config-template.toml +++ b/config-template.toml @@ -38,3 +38,6 @@ dryrun = false warn_days_before_borrowing_deadline = [ 5, 1 ] days_before_queue_position_expires = 14 warn_days_before_expiring_queue_position_deadline = [ 3, 1 ] + +[general] +quit_allowed = true diff --git a/src/worblehat/cli/main.py b/src/worblehat/cli/main.py index 24fe1d9..1ac0eda 100644 --- a/src/worblehat/cli/main.py +++ b/src/worblehat/cli/main.py @@ -16,6 +16,7 @@ from worblehat.models import * from worblehat.services import ( create_bookcase_item_from_isbn, is_valid_isbn, + Config, ) from .subclis import ( @@ -47,26 +48,13 @@ class WorblehatCli(NumberedCmd): self.sql_session_dirty = False self.prompt_header = None - @classmethod - def run_with_safe_exit_wrapper(cls, sql_session: Session) -> None: - tool = cls(sql_session) + def run_with_safe_exit_wrapper(self) -> None: while True: try: - tool.cmdloop() - except KeyboardInterrupt: - if not tool.sql_session_dirty: - exit(0) - try: - print() - if prompt_yes_no( - "Are you sure you want to exit without saving?", - default=False, - ): - raise KeyboardInterrupt - except KeyboardInterrupt: - if tool.sql_session is not None: - tool.sql_session.rollback() - exit(0) + self.cmdloop() + except KeyboardInterrupt: + print("\n\n-----------------\n") + self.do_exit("Exit") def do_show_bookcase(self, arg: str) -> None: bookcase_selector = InteractiveItemSelector( @@ -246,7 +234,8 @@ class WorblehatCli(NumberedCmd): self.sql_session.commit() else: self.sql_session.rollback() - exit(0) + if Config["general.quit_allowed"]: + exit(0) funcs = { 0: { diff --git a/src/worblehat/main.py b/src/worblehat/main.py index adff47a..844e6c0 100644 --- a/src/worblehat/main.py +++ b/src/worblehat/main.py @@ -65,7 +65,8 @@ def main() -> None: if args.command == "cli": sql_session = _connect_to_database(echo=Config["logging.debug_sql"]) - WorblehatCli.run_with_safe_exit_wrapper(sql_session) + worblehat = WorblehatCli(sql_session) + worblehat.run_with_safe_exit_wrapper() exit(0) if args.command == "create-db":