From 02887b93365cf5750f7435d4e99d77684236b0a9 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 17 May 2024 10:58:01 +0200 Subject: [PATCH] feat: add main_sync wrapper for asyncio compatibility Refactored the main execution pathway to introduce a `main_sync` function that wraps the existing asynchronous `main` function, facilitating compatibility with environments that necessitate or prefer synchronous execution. This change enhances the bot's flexibility in various deployment scenarios without altering the core asynchronous functionality. In addition, expanded the exception handling in `get_version` to catch all exceptions instead of limiting to `DistributionNotFound`. This broadens the robustness of version retrieval, ensuring the application can gracefully handle unexpected issues during version lookup. Whitespace adjustments improve code readability by clearly separating function definitions. These adjustments contribute to the maintainability and operability of the application, allowing for broader usage contexts and easier integration into diverse environments. --- src/gptbot/__main__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/gptbot/__main__.py b/src/gptbot/__main__.py index d5b8774..77c6096 100644 --- a/src/gptbot/__main__.py +++ b/src/gptbot/__main__.py @@ -7,16 +7,19 @@ import signal import asyncio import importlib.metadata + def sigterm_handler(_signo, _stack_frame): exit() + def get_version(): try: package_version = importlib.metadata.version("matrix_gptbot") - except pkg_resources.DistributionNotFound: + except Exception: return None return package_version + async def main(): # Parse command line arguments parser = ArgumentParser() @@ -59,5 +62,9 @@ async def main(): print("Received SIGTERM - exiting...") +def main_sync(): + asyncio.run(main()) + + if __name__ == "__main__": - asyncio.get_event_loop().run_until_complete(main()) \ No newline at end of file + main_sync()