comparison libervia/cli/base.py @ 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 51744ad00a42
children 783bbdbf8567
comparison
equal deleted inserted replaced
4132:8e5333f1b430 4133:33fd658d9d00
114 if bridge_module is None: 114 if bridge_module is None:
115 log.error("Can't import {} bridge".format(bridge_name)) 115 log.error("Can't import {} bridge".format(bridge_name))
116 sys.exit(1) 116 sys.exit(1)
117 117
118 self.bridge = bridge_module.AIOBridge() 118 self.bridge = bridge_module.AIOBridge()
119 self._onQuitCallbacks = [] 119 self._on_quit_callbacks = []
120 120
121 def get_config(self, name, section=C.CONFIG_SECTION, default=None): 121 def get_config(self, name, section=C.CONFIG_SECTION, default=None):
122 """Retrieve a setting value from sat.conf""" 122 """Retrieve a setting value from sat.conf"""
123 return config.config_get(self.sat_conf, section, name, default=default) 123 return config.config_get(self.sat_conf, section, name, default=default)
124 124
299 def add_on_quit_callback(self, callback, *args, **kwargs): 299 def add_on_quit_callback(self, callback, *args, **kwargs):
300 """Add a callback which will be called on quit command 300 """Add a callback which will be called on quit command
301 301
302 @param callback(callback): method to call 302 @param callback(callback): method to call
303 """ 303 """
304 self._onQuitCallbacks.append((callback, args, kwargs)) 304 self._on_quit_callbacks.append((callback, args, kwargs))
305 305
306 def get_output_choices(self, output_type): 306 def get_output_choices(self, output_type):
307 """Return valid output filters for output_type 307 """Return valid output filters for output_type
308 308
309 @param output_type: True for default, 309 @param output_type: True for default,
728 except QuitException: 728 except QuitException:
729 return 729 return
730 730
731 def _run(self, args=None, namespace=None): 731 def _run(self, args=None, namespace=None):
732 self.loop = LiberviaCLILoop() 732 self.loop = LiberviaCLILoop()
733 self.loop.run(self, args, namespace) 733 try:
734 self.loop.run(self, args, namespace)
735 except SystemExit:
736 self.quit()
734 737
735 @classmethod 738 @classmethod
736 def run(cls): 739 def run(cls):
737 cls()._run() 740 cls()._run()
738 741
783 @param raise_exp(boolean): if True raise a QuitException to stop code execution 786 @param raise_exp(boolean): if True raise a QuitException to stop code execution
784 The default value should be used most of time. 787 The default value should be used most of time.
785 """ 788 """
786 # first the onQuitCallbacks 789 # first the onQuitCallbacks
787 try: 790 try:
788 callbacks_list = self._onQuitCallbacks 791 callbacks_list = self._on_quit_callbacks
789 except AttributeError: 792 except AttributeError:
790 pass 793 pass
791 else: 794 else:
792 for callback, args, kwargs in callbacks_list: 795 for callback, args, kwargs in callbacks_list:
793 callback(*args, **kwargs) 796 callback(*args, **kwargs)
797 callbacks_list.clear()
794 798
795 self.loop.quit(exit_code) 799 self.loop.quit(exit_code)
796 if raise_exc: 800 if raise_exc:
797 raise QuitException 801 raise QuitException
798 802