2018-07-26 05:27:53 +02:00
|
|
|
import sys
|
2018-07-27 03:19:29 +02:00
|
|
|
import json
|
2018-07-26 05:27:53 +02:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
from docopt import docopt
|
|
|
|
from textwrap import dedent
|
|
|
|
|
2018-07-27 13:30:42 +02:00
|
|
|
from lbrynet.core.system_info import get_platform
|
2018-07-26 05:27:53 +02:00
|
|
|
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:
|
2018-07-27 03:19:29 +02:00
|
|
|
print(json.dumps(await resp.json(), indent=4))
|
2018-07-26 05:27:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def print_help():
|
|
|
|
print(dedent("""
|
|
|
|
NAME
|
|
|
|
lbry - LBRY command line client.
|
|
|
|
|
|
|
|
USAGE
|
|
|
|
lbry [--conf <config file>] <command> [<args>]
|
|
|
|
|
|
|
|
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):
|
2018-07-27 13:30:42 +02:00
|
|
|
fn = Daemon.callable_methods.get(command)
|
|
|
|
if fn:
|
|
|
|
print(dedent(fn.__doc__))
|
|
|
|
else:
|
|
|
|
print("Invalid command name")
|
|
|
|
|
|
|
|
|
2018-07-26 05:27:53 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-07-27 14:23:37 +02:00
|
|
|
def main(argv=None):
|
|
|
|
argv = argv or sys.argv[1:]
|
2018-07-26 05:27:53 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
elif method in ['version', '--version', '-v']:
|
2018-07-27 14:29:22 +02:00
|
|
|
print(json.dumps(get_platform(get_ip=False), sort_keys=True, indent=4, separators=(',', ': ')))
|
|
|
|
|
2018-07-26 05:27:53 +02:00
|
|
|
|
|
|
|
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__":
|
2018-07-27 13:30:42 +02:00
|
|
|
sys.exit(main())
|