comparison libervia/cli/base.py @ 4142:783bbdbf8567

cli (base): new `a_quit` method to run async cleaning methods: it may be necessary to run some async cleaning method (e.g. if bridge is needed), this new method handles async cleaning method, and will be called when `SystemExit` is used, or if `a_quit` is called explicitely. rel 426
author Goffi <goffi@goffi.org>
date Wed, 01 Nov 2023 14:08:00 +0100
parents 33fd658d9d00
children 730f542e4ad0
comparison
equal deleted inserted replaced
4141:ba8ddfdd334f 4142:783bbdbf8567
47 from libervia.backend.core import exceptions 47 from libervia.backend.core import exceptions
48 import libervia.cli 48 import libervia.cli
49 from libervia.cli.loops import QuitException, get_libervia_cli_loop 49 from libervia.cli.loops import QuitException, get_libervia_cli_loop
50 from libervia.cli.constants import Const as C 50 from libervia.cli.constants import Const as C
51 from libervia.frontends.bridge.bridge_frontend import BridgeException 51 from libervia.frontends.bridge.bridge_frontend import BridgeException
52 from libervia.frontends.tools import misc 52 from libervia.frontends.tools import aio, misc
53 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI 53 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
54 from collections import OrderedDict 54 from collections import OrderedDict
55 from rich import console 55 from rich import console
56 56
57 ## bridge handling 57 ## bridge handling
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 try: 733 try:
734 self.loop.run(self, args, namespace) 734 self.loop.run(self, args, namespace)
735 except SystemExit: 735 except SystemExit:
736 self.quit() 736 # The loop is stopped, but we execute it one more time to call `a_quit` which
737 # will call any cleaning method including async ones.
738 asyncio.get_event_loop().run_until_complete(self.a_quit())
737 739
738 @classmethod 740 @classmethod
739 def run(cls): 741 def run(cls):
740 cls()._run() 742 cls()._run()
741 743
797 callbacks_list.clear() 799 callbacks_list.clear()
798 800
799 self.loop.quit(exit_code) 801 self.loop.quit(exit_code)
800 if raise_exc: 802 if raise_exc:
801 raise QuitException 803 raise QuitException
804
805 async def a_quit(self, exit_code=0, raise_exc=True):
806 """Execute async quit callback before actually quitting
807
808 This method should be prefered to ``quit``, as it executes async quit callbacks
809 which may be necessary for proper cleaning of session.
810 """
811 try:
812 callbacks_list = self._on_quit_callbacks
813 except AttributeError:
814 pass
815 else:
816 for callback, args, kwargs in callbacks_list:
817 await aio.maybe_async(callback(*args, **kwargs))
818 callbacks_list.clear()
819 self.quit(exit_code, raise_exc)
802 820
803 async def check_jids(self, jids): 821 async def check_jids(self, jids):
804 """Check jids validity, transform roster name to corresponding jids 822 """Check jids validity, transform roster name to corresponding jids
805 823
806 @param profile: profile name 824 @param profile: profile name