From aeaffd620efa04155710b2567aadeab0c553f3a0 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 25 Jul 2018 23:27:53 -0400 Subject: [PATCH] simple cli implementation --- lbrynet/cli.py | 115 ++++++++++++++++++++++++++++++++ lbrynet/daemon/DaemonControl.py | 6 +- 2 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 lbrynet/cli.py diff --git a/lbrynet/cli.py b/lbrynet/cli.py new file mode 100644 index 000000000..33e3f6d7d --- /dev/null +++ b/lbrynet/cli.py @@ -0,0 +1,115 @@ +import sys +import asyncio +import aiohttp +from docopt import docopt +from textwrap import dedent + +from lbrynet.daemon.Daemon import Daemon +from lbrynet.daemon.DaemonControl import start + + +async def execute_command(command, args): + message = {'method': command, 'params': args} + async with aiohttp.ClientSession() as session: + async with session.get('http://localhost:5279/lbryapi', json=message) as resp: + print(await resp.json()) + + +def print_help(): + print(dedent(""" + NAME + lbry - LBRY command line client. + + USAGE + lbry [--conf ] [] + + EXAMPLES + lbry commands # list available commands + lbry status # get daemon status + lbry --conf ~/l1.conf status # like above but using ~/l1.conf as config file + lbry resolve_name what # resolve a name + lbry help resolve_name # get help for a command + """)) + + +def print_help_for_command(command): + print("@hackrush didn't implement this yet :-p") + + +def guess_type(x, key=None): + if not isinstance(x, str): + return x + if key in ('uri', 'channel_name', 'name', 'file_name', 'download_directory'): + return x + if x in ('true', 'True', 'TRUE'): + return True + if x in ('false', 'False', 'FALSE'): + return False + if '.' in x: + try: + return float(x) + except ValueError: + # not a float + pass + try: + return int(x) + except ValueError: + return x + + +def remove_brackets(key): + if key.startswith("<") and key.endswith(">"): + return str(key[1:-1]) + return key + + +def set_kwargs(parsed_args): + kwargs = {} + for key, arg in parsed_args.items(): + k = None + if arg is None: + continue + elif key.startswith("--") and remove_brackets(key[2:]) not in kwargs: + k = remove_brackets(key[2:]) + elif remove_brackets(key) not in kwargs: + k = remove_brackets(key) + kwargs[k] = guess_type(arg, k) + return kwargs + + +def main(argv): + if not argv: + print_help() + return 1 + + method, args = argv[0], argv[1:] + + if method in ['help', '--help', '-h']: + if len(args) == 1: + print_help_for_command(args[0]) + else: + print_help() + return 0 + + elif method in ['version', '--version', '-v']: + print("@hackrush didn't implement this yet :-p") + + elif method == 'start': + start(args) + + elif method not in Daemon.callable_methods: + print('"{}" is not a valid command.'.format(method)) + return 1 + + else: + fn = Daemon.callable_methods[method] + parsed = docopt(fn.__doc__, args) + kwargs = set_kwargs(parsed) + loop = asyncio.get_event_loop() + loop.run_until_complete(execute_command(method, kwargs)) + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/lbrynet/daemon/DaemonControl.py b/lbrynet/daemon/DaemonControl.py index 5e36e5343..f6b4ccd49 100644 --- a/lbrynet/daemon/DaemonControl.py +++ b/lbrynet/daemon/DaemonControl.py @@ -26,7 +26,7 @@ def test_internet_connection(): return utils.check_connection() -def start(): +def start(argv): """The primary entry point for launching the daemon.""" # postpone loading the config file to after the CLI arguments @@ -57,7 +57,7 @@ def start(): help='Show daemon version and quit' ) - args = parser.parse_args() + args = parser.parse_args(argv) update_settings_from_args(args) conf.settings.load_conf_file_settings() @@ -103,4 +103,4 @@ def update_settings_from_args(args): if __name__ == "__main__": - start() + start(sys.argv[1:])