From 51576d2970b47b2b7f8a58655de3d4594c1ee31a Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 14 Feb 2018 21:14:29 -0800 Subject: [PATCH] Fix various inconsistencies/bugs in cli.py --- disco/cli.py | 68 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/disco/cli.py b/disco/cli.py index edddbed..c1e96fb 100644 --- a/disco/cli.py +++ b/disco/cli.py @@ -14,18 +14,35 @@ from gevent import monkey monkey.patch_all() parser = argparse.ArgumentParser() -parser.add_argument('--config', help='Configuration file', default='config.json') + +# Command line specific arguments +parser.add_argument('--run-bot', help='run a disco bot on this client', action='store_true', default=False) +parser.add_argument('--plugin', help='load plugins into the bot', nargs='*', default=[]) +parser.add_argument('--config', help='Configuration file', default=None) +parser.add_argument('--shard-auto', help='Automatically run all shards', action='store_true', default=False) + +# Configuration overrides parser.add_argument('--token', help='Bot Authentication Token', default=None) -parser.add_argument('--shard-count', help='Total number of shards', default=None) parser.add_argument('--shard-id', help='Current shard number/id', default=None) -parser.add_argument('--shard-auto', help='Automatically run all shards', action='store_true', default=False) +parser.add_argument('--shard-count', help='Total number of shards', default=None) +parser.add_argument('--max-reconnects', help='Maximum reconnect attempts', default=None) +parser.add_argument('--log-level', help='log level', default=None) parser.add_argument('--manhole', action='store_true', help='Enable the manhole', default=None) parser.add_argument('--manhole-bind', help='host:port for the manhole to bind too', default=None) parser.add_argument('--encoder', help='encoder for gateway data', default=None) -parser.add_argument('--run-bot', help='run a disco bot on this client', action='store_true', default=False) -parser.add_argument('--plugin', help='load plugins into the bot', nargs='*', default=[]) -parser.add_argument('--log-level', help='log level', default=None) -parser.add_argument('--http-bind', help='bind information for http server', default=None) + + +# Mapping of argument names to configuration overrides +CONFIG_OVERRIDE_MAPPING = { + 'token': 'token', + 'shard_id': 'shard_id', + 'shard_count': 'shard_count', + 'max_reconnects': 'max_reconnects', + 'log_level': 'log_level', + 'manhole': 'manhole_enable', + 'manhole_bind': 'manhole_bind', + 'encoder': 'encoder', +} def disco_main(run=False): @@ -38,36 +55,41 @@ def disco_main(run=False): :class:`Client` A new Client from the provided command line arguments """ - args = parser.parse_args() - from disco.client import Client, ClientConfig from disco.bot import Bot, BotConfig from disco.util.logging import setup_logging - if os.path.exists(args.config): + # Parse out all our command line arguments + args = parser.parse_args() + + # Create the base configuration object + if args.config: config = ClientConfig.from_file(args.config) else: - config = ClientConfig() - - config.manhole_enable = args.manhole - if args.manhole_bind: - config.manhole_bind = args.manhole_bind - if args.log_level: - config.log_level = args.log_level + if os.path.exists('config.json'): + config = ClientConfig.from_file('config.json') + elif os.path.exists('config.yaml'): + config = ClientConfig.from_file('config.yaml') + else: + config = ClientConfig() - for k, v in six.iteritems(vars(args)): - if hasattr(config, k) and v is not None: - setattr(config, k, v) + for arg_key, config_key in six.iteritems(CONFIG_OVERRIDE_MAPPING): + if getattr(args, arg_key) is not None: + setattr(config, config_key, getattr(args, arg_key)) + # Setup the auto-sharder if args.shard_auto: from disco.gateway.sharder import AutoSharder AutoSharder(config).run() return + # Setup logging based on the configured level setup_logging(level=getattr(logging, config.log_level.upper())) + # Build out client object client = Client(config) + # If applicable, build the bot and load plugins bot = None if args.run_bot or hasattr(config, 'bot'): bot_config = BotConfig(config.bot) if hasattr(config, 'bot') else BotConfig() @@ -76,12 +98,6 @@ def disco_main(run=False): else: bot_config.plugins += args.plugin - if args.http_bind: - bot_config.http_enabled = True - host, port = args.http_bind.split(':', 1) - bot_config.http_host = host - bot_config.http_port = int(port) - bot = Bot(client, bot_config) if run: