diff --git a/src/dexorder/bin/backfill_ohlc.py b/src/dexorder/bin/backfill_ohlc.py index 65ee4dd..d58e93f 100644 --- a/src/dexorder/bin/backfill_ohlc.py +++ b/src/dexorder/bin/backfill_ohlc.py @@ -74,4 +74,4 @@ async def main(): if __name__ == '__main__': - execute(main()) + execute(main) diff --git a/src/dexorder/bin/block_for_time.py b/src/dexorder/bin/block_for_time.py index 9720484..4789db6 100644 --- a/src/dexorder/bin/block_for_time.py +++ b/src/dexorder/bin/block_for_time.py @@ -37,4 +37,4 @@ if __name__ == '__main__': time = parse_date(sys.argv[1], ignoretz=True).replace(tzinfo=timezone.utc) seconds_per_block = float(sys.argv[2]) sys.argv = [sys.argv[0], *sys.argv[3:]] - execute(main()) + execute(main) diff --git a/src/dexorder/bin/executable.py b/src/dexorder/bin/executable.py index a60469a..b63a27c 100644 --- a/src/dexorder/bin/executable.py +++ b/src/dexorder/bin/executable.py @@ -7,10 +7,11 @@ import tomllib from asyncio import CancelledError from signal import Signals from traceback import print_exception -from typing import Coroutine +from typing import Coroutine, Callable, Union, Any from dexorder import configuration, config from dexorder.alert import init_alerts +from dexorder.configuration.schema import Config from dexorder.metric.metric_startup import start_metrics_server if __name__ == '__main__': @@ -25,7 +26,25 @@ async def _shutdown_coro(_sig, _loop): if task is not this_task: task.cancel() -def execute(main:Coroutine, shutdown=None, *, parse_logging=True, parse_args=True): + +def split_args(): + omegaconf_args = [] + regular_args = [] + for arg in sys.argv[1:]: + if '=' in arg: + key, value = arg.split('=', 1) + if hasattr(Config, key): + omegaconf_args.append(arg) + else: + regular_args.append(arg) + return omegaconf_args, regular_args + + +def execute(main:Callable[...,Coroutine[Any,Any,Any]], shutdown=None, *, parse_logging=True, parse_args: Union[Callable[[list[str]],Any],bool]=True): + """ + if parse_args is a function, then the command-line arguments are given to OmegaConf first, and any args parsed by + OmegaConf are stripped from the args list. The remaining args are then passed to parse_args(args) + """ # config configured = False if parse_logging: @@ -42,10 +61,18 @@ def execute(main:Coroutine, shutdown=None, *, parse_logging=True, parse_args=Tru logging.basicConfig(level=logging.INFO, stream=sys.stdout) log.setLevel(logging.DEBUG) log.info('Logging configured to default') + xconf = None if parse_args: + if callable(parse_args): + omegaconf_args, regular_args = split_args() + else: + omegaconf_args = None # NOTE: there is special command-line argument handling in config/load.py to get a config filename. # The -c/--config flag MUST BE FIRST if present. - configuration.parse_args() + configuration.parse_args(omegaconf_args) + if callable(parse_args): + # noinspection PyUnboundLocalVariable + xconf = parse_args(regular_args) init_alerts() @@ -59,7 +86,14 @@ def execute(main:Coroutine, shutdown=None, *, parse_logging=True, parse_args=Tru loop.add_signal_handler(s, lambda sig=s: asyncio.create_task(_shutdown_coro(sig, loop), name=f'{s.name} handler')) # main - task = loop.create_task(main, name='main') + num_args = len(inspect.signature(main).parameters) + if num_args == 0: + coro = main() + elif num_args == 1: + coro = main(xconf) + else: + raise Exception(f'main() must accept 0 or 1 arguments, not {num_args}') + task = loop.create_task(coro, name='main') try: loop.run_until_complete(task) except CancelledError: diff --git a/src/dexorder/bin/finaldata.py b/src/dexorder/bin/finaldata.py index fb416d1..bc6cebd 100644 --- a/src/dexorder/bin/finaldata.py +++ b/src/dexorder/bin/finaldata.py @@ -62,4 +62,4 @@ async def main(): if __name__ == '__main__': - execute(main()) + execute(main) diff --git a/src/dexorder/bin/main.py b/src/dexorder/bin/main.py index 66b67d2..9e5efcc 100644 --- a/src/dexorder/bin/main.py +++ b/src/dexorder/bin/main.py @@ -138,4 +138,4 @@ async def main(): if __name__ == '__main__': - execute(main()) + execute(main) diff --git a/src/dexorder/bin/mirror.py b/src/dexorder/bin/mirror.py index 98ee58b..2c44c0b 100644 --- a/src/dexorder/bin/mirror.py +++ b/src/dexorder/bin/mirror.py @@ -216,4 +216,4 @@ async def main(): if __name__ == '__main__': - execute(main()) + execute(main) diff --git a/src/dexorder/bin/reconcile.py b/src/dexorder/bin/reconcile.py index ec98ea9..e65ef7d 100644 --- a/src/dexorder/bin/reconcile.py +++ b/src/dexorder/bin/reconcile.py @@ -28,5 +28,5 @@ async def main(): if __name__ == '__main__': - execute(main()) + execute(main) \ No newline at end of file