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
This commit is contained in:
Goffi 2023-11-01 14:08:00 +01:00
commit b7d3afeabd

View file

@ -49,7 +49,7 @@ import libervia.cli
from libervia.cli.loops import QuitException, get_libervia_cli_loop
from libervia.cli.constants import Const as C
from libervia.frontends.bridge.bridge_frontend import BridgeException
from libervia.frontends.tools import misc
from libervia.frontends.tools import aio, misc
import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
from collections import OrderedDict
from rich import console
@ -733,7 +733,9 @@ class LiberviaCli:
try:
self.loop.run(self, args, namespace)
except SystemExit:
self.quit()
# The loop is stopped, but we execute it one more time to call `a_quit` which
# will call any cleaning method including async ones.
asyncio.get_event_loop().run_until_complete(self.a_quit())
@classmethod
def run(cls):
@ -800,6 +802,22 @@ class LiberviaCli:
if raise_exc:
raise QuitException
async def a_quit(self, exit_code=0, raise_exc=True):
"""Execute async quit callback before actually quitting
This method should be prefered to ``quit``, as it executes async quit callbacks
which may be necessary for proper cleaning of session.
"""
try:
callbacks_list = self._on_quit_callbacks
except AttributeError:
pass
else:
for callback, args, kwargs in callbacks_list:
await aio.maybe_async(callback(*args, **kwargs))
callbacks_list.clear()
self.quit(exit_code, raise_exc)
async def check_jids(self, jids):
"""Check jids validity, transform roster name to corresponding jids