minimal changes to cli.py

This commit is contained in:
Jack Robison 2019-01-23 16:41:14 -05:00 committed by Lex Berezhny
parent 94cec1b86d
commit 89efe3d1b9
5 changed files with 83 additions and 89 deletions

View file

@ -2,63 +2,20 @@ import sys
import json import json
import asyncio import asyncio
import argparse import argparse
import typing import logging
import logging.handlers import logging.handlers
import aiohttp
from docopt import docopt from docopt import docopt
from textwrap import dedent from textwrap import dedent
import aiohttp
from lbrynet import __name__ as lbrynet_name, __version__ as lbrynet_version from lbrynet import __name__ as lbrynet_name, __version__ as lbrynet_version
from lbrynet.extras.daemon.loggly_handler import get_loggly_handler
from lbrynet.conf import Config, CLIConfig from lbrynet.conf import Config, CLIConfig
from lbrynet.extras.daemon.Daemon import Daemon from lbrynet.extras.daemon.Daemon import Daemon
from lbrynet.extras.daemon.client import LBRYAPIClient, JSONRPCException
from lbrynet.extras.daemon.loggly_handler import get_loggly_handler
log = logging.getLogger(lbrynet_name) log = logging.getLogger(lbrynet_name)
log.addHandler(logging.NullHandler()) log.addHandler(logging.NullHandler())
default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
optional_path_getter_type = typing.Optional[typing.Callable[[], str]]
async def start_daemon(conf: Config, args):
file_handler = logging.handlers.RotatingFileHandler(conf.log_file_path,
maxBytes=2097152, backupCount=5)
file_handler.setFormatter(default_formatter)
log.addHandler(file_handler)
if not args.quiet:
handler = logging.StreamHandler()
handler.setFormatter(default_formatter)
log.addHandler(handler)
# mostly disable third part logging
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
logging.getLogger('BitcoinRPC').setLevel(logging.INFO)
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)
log.info("Starting lbrynet-daemon from command line")
daemon = Daemon(conf)
try:
await daemon.start_listening()
except (OSError, asyncio.CancelledError):
return 1
try:
await daemon.server.wait_closed()
except (KeyboardInterrupt, asyncio.CancelledError):
await daemon.shutdown()
return 0
def display(data): def display(data):
@ -66,14 +23,23 @@ def display(data):
async def execute_command(conf, method, params): async def execute_command(conf, method, params):
client = LBRYAPIClient(conf) async with aiohttp.ClientSession() as session:
try: try:
result = await getattr(client, method)(params) message = {'method': method, 'params': params}
print(display(result)) 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: except aiohttp.ClientConnectionError:
print("Could not connect to daemon. Are you sure it's running?") print("Could not connect to daemon. Are you sure it's running?")
except JSONRPCException as err:
print(err)
def print_help(): def print_help():
@ -211,36 +177,71 @@ def main(argv=None):
argv = argv or sys.argv[1:] argv = argv or sys.argv[1:]
parser = get_argument_parser() parser = get_argument_parser()
args, command_args = parser.parse_known_args(argv) args, command_args = parser.parse_known_args(argv)
conf = Config.create_from_arguments(args) conf = Config.create_from_arguments(args)
if args.cli_version: if args.cli_version:
print(f"{lbrynet_name} {lbrynet_version}") print(f"{lbrynet_name} {lbrynet_version}")
return 0
elif args.command == 'start': elif args.command == 'start':
if args.help: if args.help:
args.start_parser.print_help() args.start_parser.print_help()
return 0 return 0
try: default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
asyncio.run(start_daemon(conf, args)) file_handler = logging.handlers.RotatingFileHandler(conf.log_file_path, maxBytes = 2097152, backupCount = 5)
except (KeyboardInterrupt, asyncio.CancelledError): file_handler.setFormatter(default_formatter)
return 0 log.addHandler(file_handler)
if not args.quiet:
handler = logging.StreamHandler()
handler.setFormatter(default_formatter)
log.addHandler(handler)
# mostly disable third part logging
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
logging.getLogger('BitcoinRPC').setLevel(logging.INFO)
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)
log.debug('Final Settings: %s', conf.settings_dict)
log.info("Starting lbrynet-daemon from command line")
daemon = Daemon(conf)
asyncio.run(daemon.start())
elif args.command is not None: elif args.command is not None:
doc = args.doc doc = args.doc
api_method_name = args.api_method_name api_method_name = args.api_method_name
if args.replaced_by: if args.replaced_by:
print(f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}.") print(f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}.")
doc = args.replaced_by['doc'] doc = args.replaced_by['doc']
api_method_name = args.replaced_by['api_method_name'] api_method_name = args.replaced_by['api_method_name']
if args.help: if args.help:
print(doc) print(doc)
return 0
else: else:
parsed = docopt(doc, command_args) parsed = docopt(doc, command_args)
params = set_kwargs(parsed) params = set_kwargs(parsed)
asyncio.run(execute_command(conf, api_method_name, params)) loop = asyncio.get_event_loop()
loop.run_until_complete(execute_command(conf, api_method_name, params))
elif args.group is not None: elif args.group is not None:
args.group_parser.print_help() args.group_parser.print_help()
else: else:
parser.print_help() parser.print_help()
return 0 return 0

View file

@ -379,7 +379,7 @@ class Daemon(metaclass=JSONRPCServerType):
if not os.path.isdir(self.conf.download_dir): if not os.path.isdir(self.conf.download_dir):
os.makedirs(self.conf.download_dir) os.makedirs(self.conf.download_dir)
async def start_listening(self): async def start(self):
try: try:
self.server = await asyncio.get_event_loop().create_server( self.server = await asyncio.get_event_loop().create_server(
self.handler, self.conf.api_host, self.conf.api_port self.handler, self.conf.api_host, self.conf.api_port

View file

@ -16,18 +16,12 @@ class JSONRPCException(Exception):
self.error = rpc_error self.error = rpc_error
class LBRYAPIClient: async def daemon_rpc(conf: Config, method: str, *args, **kwargs):
def __init__(self, conf: Config):
self._conf = conf
def __getattr__(self, method):
async def f(*args, **kwargs):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
message = {'method': method, 'params': [args, kwargs]} message = {'method': method, 'params': [args, kwargs]}
async with session.get(self._conf.api_connection_url, json=message) as resp: async with session.get(conf.api_connection_url, json=message) as resp:
data = await resp.json() data = await resp.json()
if 'result' in data: if 'result' in data:
return data['result'] return data['result']
elif 'error' in data: elif 'error' in data:
raise JSONRPCException(data['error']) raise JSONRPCException(data['error'])
return f

View file

@ -2,7 +2,7 @@ import curses
import time import time
import asyncio import asyncio
from lbrynet.conf import Config from lbrynet.conf import Config
from lbrynet.extras.daemon.client import LBRYAPIClient from lbrynet.extras.daemon.client import daemon_rpc
stdscr = curses.initscr() stdscr = curses.initscr()
@ -47,13 +47,12 @@ def refresh(routing_table_info):
async def main(): async def main():
api = LBRYAPIClient(Config()) conf = Config()
try: try:
init_curses() init_curses()
c = None c = None
while c not in [ord('q'), ord('Q')]: while c not in [ord('q'), ord('Q')]:
routing_info = await api.routing_table_get() routing_info = await daemon_rpc(conf, 'routing_table_get')
refresh(routing_info) refresh(routing_info)
c = stdscr.getch() c = stdscr.getch()
time.sleep(0.1) time.sleep(0.1)

View file

@ -9,7 +9,7 @@ from aiohttp import ClientConnectorError
from lbrynet import __version__ from lbrynet import __version__
from lbrynet.conf import Config from lbrynet.conf import Config
from lbrynet.schema.uri import parse_lbry_uri from lbrynet.schema.uri import parse_lbry_uri
from lbrynet.extras.daemon.client import LBRYAPIClient from lbrynet.extras.daemon.client import daemon_rpc
from lbrynet.extras import system_info, cli from lbrynet.extras import system_info, cli
@ -80,10 +80,10 @@ async def wait_for_done(api, uri):
async def main(start_daemon=True, uris=None): async def main(start_daemon=True, uris=None):
if not uris: if not uris:
uris = await get_frontpage_uris() uris = await get_frontpage_uris()
api = LBRYAPIClient(Config()) conf = Config()
daemon = None daemon = None
try: try:
await api.status() await daemon_rpc(conf, 'status')
except (ClientConnectorError, ConnectionError): except (ClientConnectorError, ConnectionError):
print("Could not connect to daemon") print("Could not connect to daemon")
return 1 return 1
@ -92,7 +92,7 @@ async def main(start_daemon=True, uris=None):
resolvable = [] resolvable = []
for name in uris: for name in uris:
resolved = await api.resolve(uri=name) resolved = await daemon_rpc(conf, 'resolve', uri=name)
if 'error' not in resolved.get(name, {}): if 'error' not in resolved.get(name, {}):
resolvable.append(name) resolvable.append(name)
@ -104,12 +104,12 @@ async def main(start_daemon=True, uris=None):
download_failures = [] download_failures = []
for uri in resolvable: for uri in resolvable:
await api.file_delete(delete_from_download_dir=True, claim_name=parse_lbry_uri(uri).name) await daemon_rpc(conf, 'file_delete', delete_from_download_dir=True, claim_name=parse_lbry_uri(uri).name)
for i, uri in enumerate(resolvable): for i, uri in enumerate(resolvable):
start = time.time() start = time.time()
try: try:
await api.get(uri) await daemon_rpc(conf, 'get', uri)
first_byte = time.time() first_byte = time.time()
first_byte_times.append(first_byte - start) first_byte_times.append(first_byte - start)
print(f"{i + 1}/{len(resolvable)} - {first_byte - start} {uri}") print(f"{i + 1}/{len(resolvable)} - {first_byte - start} {uri}")
@ -123,7 +123,7 @@ async def main(start_daemon=True, uris=None):
except: except:
print(f"{i + 1}/{len(uris)} - timeout in {time.time() - start} {uri}") print(f"{i + 1}/{len(uris)} - timeout in {time.time() - start} {uri}")
failures.append(uri) failures.append(uri)
await api.file_delete(delete_from_download_dir=True, claim_name=parse_lbry_uri(uri).name) await daemon_rpc(conf, 'file_delete', delete_from_download_dir=True, claim_name=parse_lbry_uri(uri).name)
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
print("**********************************************") print("**********************************************")