diff --git a/scripts/scripts_support.py b/scripts/scripts_support.py new file mode 100644 index 0000000..152eccd --- /dev/null +++ b/scripts/scripts_support.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +import asyncio +from functools import wraps + +import jmclient # install asyncioreactor +from twisted.internet import reactor + +from jmbase import jmprint + + +def wrap_main(func): + + @wraps(func) + async def func_wrapper(*args, **kwargs): + + try: + return await func(*args, **kwargs) + except SystemExit as e: + return e.args[0] if e.args else None + finally: + try: + for task in asyncio.all_tasks(): + task.cancel() + if reactor.running: + reactor.stop() + except Exception as e: + jmprint(f'Errors during reactor cleaenup/stop: {e}', 'debug') + + return func_wrapper diff --git a/scripts/wallet-tool.py b/scripts/wallet-tool.py index bd26d49..969f0d5 100755 --- a/scripts/wallet-tool.py +++ b/scripts/wallet-tool.py @@ -5,25 +5,19 @@ import sys import jmclient # install asyncioreactor from twisted.internet import reactor +from scripts_support import wrap_main from jmbase import jmprint from jmclient import wallet_tool_main +@wrap_main async def _main(): - try: - res = await wallet_tool_main("wallets") - if res: - jmprint(res, "success") - else: - jmprint("Finished", "success") - except SystemExit as e: - return e.args[0] if e.args else None - finally: - for task in asyncio.all_tasks(): - task.cancel() - if reactor.running: - reactor.stop() + res = await wallet_tool_main("wallets") + if res: + jmprint(res, "success") + else: + jmprint("Finished", "success") if __name__ == "__main__":