changeset 4133:33fd658d9d00

cli (base): catch `SystemExit` to do a proper `quit` when `Ctrl-C` is pressed.
author Goffi <goffi@goffi.org>
date Wed, 18 Oct 2023 15:33:38 +0200
parents 8e5333f1b430
children 8d361adf0ee1
files libervia/cli/base.py
diffstat 1 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/cli/base.py	Wed Oct 18 15:33:34 2023 +0200
+++ b/libervia/cli/base.py	Wed Oct 18 15:33:38 2023 +0200
@@ -116,7 +116,7 @@
             sys.exit(1)
 
         self.bridge = bridge_module.AIOBridge()
-        self._onQuitCallbacks = []
+        self._on_quit_callbacks = []
 
     def get_config(self, name, section=C.CONFIG_SECTION, default=None):
         """Retrieve a setting value from sat.conf"""
@@ -301,7 +301,7 @@
 
         @param callback(callback): method to call
         """
-        self._onQuitCallbacks.append((callback, args, kwargs))
+        self._on_quit_callbacks.append((callback, args, kwargs))
 
     def get_output_choices(self, output_type):
         """Return valid output filters for output_type
@@ -730,7 +730,10 @@
 
     def _run(self, args=None, namespace=None):
         self.loop = LiberviaCLILoop()
-        self.loop.run(self, args, namespace)
+        try:
+            self.loop.run(self, args, namespace)
+        except SystemExit:
+            self.quit()
 
     @classmethod
     def run(cls):
@@ -785,12 +788,13 @@
         """
         # first the onQuitCallbacks
         try:
-            callbacks_list = self._onQuitCallbacks
+            callbacks_list = self._on_quit_callbacks
         except AttributeError:
             pass
         else:
             for callback, args, kwargs in callbacks_list:
                 callback(*args, **kwargs)
+            callbacks_list.clear()
 
         self.loop.quit(exit_code)
         if raise_exc: