lbry-sdk/lbrynet/extras/cli.py

219 lines
6.8 KiB
Python
Raw Normal View History

2018-07-26 05:27:53 +02:00
import sys
import json
import asyncio
import argparse
2019-01-23 22:41:14 +01:00
import logging
2019-01-22 23:44:17 +01:00
import logging.handlers
2018-07-26 05:27:53 +02:00
from docopt import docopt
2019-01-23 22:41:14 +01:00
import aiohttp
from aiohttp.web import GracefulExit
2019-01-23 22:41:14 +01:00
2019-01-22 23:44:17 +01:00
from lbrynet import __name__ as lbrynet_name, __version__ as lbrynet_version
2019-01-23 22:41:14 +01:00
from lbrynet.extras.daemon.loggly_handler import get_loggly_handler
2019-01-21 21:55:50 +01:00
from lbrynet.conf import Config, CLIConfig
from lbrynet.extras.daemon.Daemon import Daemon
2018-07-26 05:27:53 +02:00
log = logging.getLogger(lbrynet_name)
2019-01-19 22:29:24 +01:00
log.addHandler(logging.NullHandler())
2019-01-21 21:55:50 +01:00
def display(data):
2019-01-22 05:28:26 +01:00
print(json.dumps(data, indent=2))
2018-07-26 05:27:53 +02:00
2019-01-22 15:43:03 +01:00
2019-01-21 21:55:50 +01:00
async def execute_command(conf, method, params):
2019-01-23 22:41:14 +01:00
async with aiohttp.ClientSession() as session:
try:
message = {'method': method, 'params': params}
async with session.get(conf.api_connection_url, json=message) as resp:
try:
data = await resp.json()
if 'result' in data:
display(data['result'])
elif 'error' in data:
if 'message' in data['error']:
display(data['error']['message'])
else:
display(data['error'])
except Exception as e:
log.exception('Could not process response from server:', exc_info=e)
except aiohttp.ClientConnectionError:
print("Could not connect to daemon. Are you sure it's running?")
2018-07-26 05:27:53 +02:00
def normalize_value(x, key=None):
2018-07-26 05:27:53 +02:00
if not isinstance(x, str):
return x
if key in ('uri', 'channel_name', 'name', 'file_name', 'download_directory'):
return x
if x.lower() == 'true':
2018-07-26 05:27:53 +02:00
return True
if x.lower() == 'false':
2018-07-26 05:27:53 +02:00
return False
if x.isdigit():
2018-07-26 05:27:53 +02:00
return int(x)
return x
2018-07-26 05:27:53 +02:00
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] = normalize_value(arg, k)
2018-07-26 05:27:53 +02:00
return kwargs
2019-01-23 19:00:58 +01:00
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, add_help=False, **kwargs)
self.add_argument(
'--help', dest='help', action='store_true', default=False,
help='show this help message and exit'
)
def add_command_parser(parent, command):
subcommand = parent.add_parser(
command['name'],
help=command['doc'].strip().splitlines()[0]
)
subcommand.set_defaults(
api_method_name=command['api_method_name'],
command=command['name'],
doc=command['doc'],
replaced_by=command.get('replaced_by', None)
)
2019-01-21 21:55:50 +01:00
def get_argument_parser():
2019-01-23 19:00:58 +01:00
main = ArgumentParser('lbrynet')
2019-01-21 21:55:50 +01:00
main.add_argument(
'--version', dest='cli_version', action="store_true",
help='Show lbrynet CLI version and exit.'
)
2019-01-23 19:00:58 +01:00
main.set_defaults(group=None, command=None)
2019-01-21 21:55:50 +01:00
CLIConfig.contribute_args(main)
2019-01-23 19:00:58 +01:00
sub = main.add_subparsers()
2019-01-21 21:55:50 +01:00
start = sub.add_parser('start', help='Start lbrynet server.')
start.add_argument(
'--quiet', dest='quiet', action="store_true",
help='Disable all console output.'
)
start.add_argument(
'--verbose', nargs="*",
help=('Enable debug output. Optionally specify loggers for which debug output '
'should selectively be applied.')
)
2019-01-23 19:00:58 +01:00
start.set_defaults(command='start', start_parser=start)
2019-01-21 21:55:50 +01:00
Config.contribute_args(start)
2019-01-23 19:00:58 +01:00
2019-01-21 21:55:50 +01:00
api = Daemon.get_api_definitions()
2019-01-23 19:00:58 +01:00
groups = {}
for group_name in sorted(api['groups']):
group_parser = sub.add_parser(group_name, help=api['groups'][group_name])
group_parser.set_defaults(group=group_name, group_parser=group_parser)
groups[group_name] = group_parser.add_subparsers()
for command_name in sorted(api['commands']):
command = api['commands'][command_name]
if command['group'] is None:
add_command_parser(sub, command)
else:
add_command_parser(groups[command['group']], command)
2019-01-21 21:55:50 +01:00
return main
def main(argv=None):
argv = argv or sys.argv[1:]
2019-01-21 21:55:50 +01:00
parser = get_argument_parser()
2019-01-22 21:00:13 +01:00
args, command_args = parser.parse_known_args(argv)
2019-01-23 22:41:14 +01:00
2019-01-22 05:28:26 +01:00
conf = Config.create_from_arguments(args)
2019-01-21 21:55:50 +01:00
if args.cli_version:
print(f"{lbrynet_name} {lbrynet_version}")
2019-01-23 22:41:14 +01:00
return 0
2019-01-21 21:55:50 +01:00
elif args.command == 'start':
2019-01-23 22:41:14 +01:00
2019-01-23 20:56:54 +01:00
if args.help:
args.start_parser.print_help()
return 0
2019-01-23 22:41:14 +01:00
default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
2019-01-24 00:08:41 +01:00
file_handler = logging.handlers.RotatingFileHandler(
conf.log_file_path, maxBytes=2097152, backupCount=5
)
2019-01-23 22:41:14 +01:00
file_handler.setFormatter(default_formatter)
log.addHandler(file_handler)
if not args.quiet:
handler = logging.StreamHandler()
handler.setFormatter(default_formatter)
log.addHandler(handler)
2019-01-23 22:41:14 +01:00
logging.getLogger('aioupnp').setLevel(logging.WARNING)
logging.getLogger('aiohttp').setLevel(logging.CRITICAL)
if args.verbose:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
if conf.share_usage_data:
loggly_handler = get_loggly_handler()
loggly_handler.setLevel(logging.ERROR)
log.addHandler(loggly_handler)
daemon = Daemon(conf)
loop = asyncio.get_event_loop()
loop.run_until_complete(daemon.start())
try:
loop.run_forever()
except (GracefulExit, KeyboardInterrupt):
pass
finally:
loop.run_until_complete(daemon.stop())
if hasattr(loop, 'shutdown_asyncgens'):
loop.run_until_complete(loop.shutdown_asyncgens())
2019-01-23 22:41:14 +01:00
2019-01-21 21:55:50 +01:00
elif args.command is not None:
2019-01-23 22:41:14 +01:00
2019-01-23 19:00:58 +01:00
doc = args.doc
api_method_name = args.api_method_name
if args.replaced_by:
print(f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}.")
doc = args.replaced_by['doc']
api_method_name = args.replaced_by['api_method_name']
2019-01-23 22:41:14 +01:00
2019-01-23 19:00:58 +01:00
if args.help:
print(doc)
2019-01-22 05:28:26 +01:00
else:
2019-01-23 19:00:58 +01:00
parsed = docopt(doc, command_args)
params = set_kwargs(parsed)
2019-01-23 22:41:14 +01:00
loop = asyncio.get_event_loop()
loop.run_until_complete(execute_command(conf, api_method_name, params))
2019-01-23 19:00:58 +01:00
elif args.group is not None:
args.group_parser.print_help()
2019-01-23 22:41:14 +01:00
2019-01-21 21:55:50 +01:00
else:
parser.print_help()
2019-01-23 22:41:14 +01:00
2018-07-26 05:27:53 +02:00
return 0
if __name__ == "__main__":
2018-07-27 13:30:42 +02:00
sys.exit(main())