From f7168bc29dfdc90b0a7bcfa9bfe66c48aa833170 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 14 Jan 2021 17:19:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20royalnet=5Fconsole=20to=20b?= =?UTF-8?q?e=20run=20as=20a=20module,=20importing=20the=20packs=20specifie?= =?UTF-8?q?d=20with=20-p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet_console/__main__.py | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 royalnet_console/__main__.py diff --git a/royalnet_console/__main__.py b/royalnet_console/__main__.py new file mode 100644 index 00000000..1f72ae30 --- /dev/null +++ b/royalnet_console/__main__.py @@ -0,0 +1,63 @@ +# Special imports +from __future__ import annotations +import royalnet.royaltyping as t + +# External imports +import logging +import importlib +import click +import asyncio +import royalnet.engineer as engi + +# Internal imports +from .pda import ConsolePDA + +# Special global objects +log = logging.getLogger(__name__) + + +# Code +@click.command() +@click.option("-p", "--pack", "packs", multiple=True) +def main(packs: t.List[str]): + log.debug("Creating PDA...") + pda = ConsolePDA() + + for pack in packs: + log.debug(f"Importing module: {pack!r}") + try: + pack = importlib.import_module(pack) + except ImportError as e: + log.error(f"Skipping {pack!r}: {e!r}") + continue + + for attribute in dir(pack): + log.debug(f"Getting attribute: {attribute!r}") + value = pack.__getattribute__(attribute) + log.debug(f"Attribute is: {value!r}") + + if isinstance(value, engi.PartialCommand): + log.debug(f"Attribute is a PartialCommand, registering it as: {value.f.__name__!r}") + pda.register_partial(part=value, names=[value.f.__name__]) + + elif isinstance(value, engi.Conversation): + log.debug(f"Attribute is a Conversation, registering it...") + pda.register_conversation(conv=value) + + log.debug("Getting event loop...") + loop = asyncio.get_event_loop() + log.debug(f"Event loop is: {loop!r}") + + log.debug("Running the PDA until interrupted...") + try: + loop.run_until_complete(pda.run()) + except KeyboardInterrupt: + log.debug("Got an interrupt, shutting down...") + exit(0) + + log.fatal("PDA stopped unexpectedly, shutting down...") + exit(1) + + +if __name__ == "__main__": + main()