From 15b8891fce5dedc5bb69090640a253f044b5d938 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 7 Jul 2020 10:52:41 -0400 Subject: [PATCH] make resolve command work from cli --- lbry/blockchain/database.py | 4 +-- lbry/cli.py | 8 +++-- lbry/service/__init__.py | 2 +- lbry/service/api.py | 2 +- lbry/service/base.py | 2 +- lbry/service/daemon.py | 18 +++++----- lbry/service/full_node.py | 7 ++-- lbry/service/parser.py | 2 +- tests/unit/service/test_parser.py | 58 ++++++++++++++++--------------- 9 files changed, 55 insertions(+), 48 deletions(-) diff --git a/lbry/blockchain/database.py b/lbry/blockchain/database.py index 0f273ce32..e4c35e013 100644 --- a/lbry/blockchain/database.py +++ b/lbry/blockchain/database.py @@ -16,10 +16,10 @@ FILES = [ def make_short_url(r): try: - f'{normalize_name(r["name"].decode())}#{r["shortestID"] or r["claimID"][::-1].hex()[0]}' + return f'{normalize_name(r["name"].decode())}#{r["shortestID"] or r["claimID"][::-1].hex()[0]}' except UnicodeDecodeError: print(f'failed making short url due to name parse error for claim_id: {r["claimID"][::-1].hex()}') - return '' + return 'FAILED' class FindShortestID: diff --git a/lbry/cli.py b/lbry/cli.py index b6f5a0ed5..fa53ade7f 100644 --- a/lbry/cli.py +++ b/lbry/cli.py @@ -8,7 +8,7 @@ from docopt import docopt from lbry import __version__ from lbry.conf import Config, CLIConfig -from lbry.service import Daemon +from lbry.service import Daemon, Client from lbry.service.metadata import interface from lbry.service.full_node import FullNode from lbry.blockchain.ledger import Ledger @@ -165,7 +165,11 @@ def ensure_directory_exists(path: str): async def execute_command(conf, method, params): - pass + client = Client(f"http://{conf.api}/ws") + await client.connect() + resp = await client.send(method, **params) + print(await resp.first) + await client.close() def normalize_value(x, key=None): diff --git a/lbry/service/__init__.py b/lbry/service/__init__.py index f510e5270..1c3395b0d 100644 --- a/lbry/service/__init__.py +++ b/lbry/service/__init__.py @@ -1,4 +1,4 @@ -from .api import API +from .api import API, Client from .daemon import Daemon, jsonrpc_dumps_pretty from .full_node import FullNode from .light_client import LightClient diff --git a/lbry/service/api.py b/lbry/service/api.py index c94ef6497..daabd71a2 100644 --- a/lbry/service/api.py +++ b/lbry/service/api.py @@ -574,7 +574,7 @@ class API: if isinstance(urls, str): urls = [urls] return await self.service.resolve( - urls, wallet=self.wallets.get_or_default(wallet_id), + urls, wallet=None,#self.wallets.get_or_default(wallet_id), include_purchase_receipt=include_purchase_receipt, include_is_my_output=include_is_my_output, include_sent_supports=include_sent_supports, diff --git a/lbry/service/base.py b/lbry/service/base.py index d86c24f0f..8ef67daa6 100644 --- a/lbry/service/base.py +++ b/lbry/service/base.py @@ -80,7 +80,7 @@ class Service: await self.sync.stop() await self.db.close() - def get_status(self): + async def get_status(self): pass def get_version(self): diff --git a/lbry/service/daemon.py b/lbry/service/daemon.py index 39dec3bba..2433bb48f 100644 --- a/lbry/service/daemon.py +++ b/lbry/service/daemon.py @@ -147,21 +147,19 @@ class Daemon: return web_socket async def on_message(self, web_socket: WebSocketManager, msg: dict): - if msg['type'] == 'subscribe': + if msg['method'] == 'subscribe': streams = msg['streams'] if isinstance(streams, str): streams = [streams] web_socket.subscribe(streams, self.app['subscriptions']) - elif msg['type'] == 'rpc': - component, method_name = msg['method'].split('.') - method = getattr(self.components[component], method_name) - if asyncio.iscoroutinefunction(method): - result = await method(**msg['args']) - else: - result = method(**msg['args']) + else: + params = msg.get('params', {}) + method = getattr(self.api, msg['method']) + result = await method(**params) + encoded_result = jsonrpc_dumps_pretty(result, service=self.service) await web_socket.send_json({ - 'type': 'rpc', 'id': msg.get('id', ''), - 'result': result + 'id': msg.get('id', ''), + 'result': encoded_result }) @staticmethod diff --git a/lbry/service/full_node.py b/lbry/service/full_node.py index 78d2bb8d9..facafeb20 100644 --- a/lbry/service/full_node.py +++ b/lbry/service/full_node.py @@ -29,6 +29,9 @@ class FullNode(Service): await super().stop() await self.chain.close() + async def get_status(self): + return 'everything is wonderful' + async def get_block_address_filters(self): return { hexlify(f['block_hash']).decode(): hexlify(f['block_filter']).decode() @@ -57,5 +60,5 @@ class FullNode(Service): async def wait(self, tx: Transaction, height=-1, timeout=1): pass - async def resolve(self, accounts, urls, **kwargs): - raise NotImplementedError + async def resolve(self, urls, **kwargs): + return await self.db.resolve(*urls) diff --git a/lbry/service/parser.py b/lbry/service/parser.py index 635c4b557..228f9885f 100644 --- a/lbry/service/parser.py +++ b/lbry/service/parser.py @@ -198,7 +198,7 @@ def generate_options(method, indent) -> List[str]: flags.append(f"--{arg['name']}") else: flags.append(f"--{arg['name']}=<{arg['name']}>") - max_len = max(len(f) for f in flags) + 1 + max_len = max(len(f) for f in flags) + 2 flags = [f.ljust(max_len) for f in flags] options = [] for flag, arg in zip(flags, method['arguments']): diff --git a/tests/unit/service/test_parser.py b/tests/unit/service/test_parser.py index 534b18dbc..45267ee2c 100644 --- a/tests/unit/service/test_parser.py +++ b/tests/unit/service/test_parser.py @@ -152,13 +152,13 @@ class TestGenerator(TestCase): expanders = get_expanders() self.assertEqual( generate_options(parse_method(FakeAPI.thing_list, expanders), indent=' '), [ - ' --value1= : (str) the first value', - ' --value2= : (int) the second value with a very very long description which', - ' needs to be wrapped', - ' --value3 : (bool) a bool multi-line', - ' --page= : (int) page to return for paginating', - ' --page_size= : (int) number of items on page for pagination', - ' --include_total : (bool) calculate total number of items and pages', + ' --value1= : (str) the first value', + ' --value2= : (int) the second value with a very very long description which', + ' needs to be wrapped', + ' --value3 : (bool) a bool multi-line', + ' --page= : (int) page to return for paginating', + ' --page_size= : (int) number of items on page for pagination', + ' --include_total : (bool) calculate total number of items and pages', ] ) @@ -181,9 +181,9 @@ class TestGenerator(TestCase): thing create Options: - --name= : (str) the name - --value1= : (str) the first value [default: 'hi'] - --value2= : (int) the second value [default: 9] + --name= : (str) the name + --value1= : (str) the first value [default: 'hi'] + --value2= : (int) the second value [default: 9] Returns: (str) thing name""") @@ -196,20 +196,22 @@ class TestGenerator(TestCase): Usage: thing delete [--wallet_id=] [--change_account_id=] - [--fund_account_id=...] [--preview] [--blocking] + [--fund_account_id=...] [--preview] [--no_wait] [--page=] [--page_size=] [--include_total] Options: - --value1= : (str) - --wallet_id= : (str) restrict operation to specific wallet - --change_account_id= : (str) account to send excess change (LBC) - --fund_account_id= : (str, list) accounts to fund the transaction - --preview : (bool) do not broadcast the transaction - --blocking : (bool) wait until transaction is in mempool - --page= : (int) page to return for paginating - --page_size= : (int) number of items on page for pagination - --include_total : (bool) calculate total number of items and - pages + --value1= : (str) + --wallet_id= : (str) restrict operation to specific wallet + --change_account_id= : (str) account to send excess change (LBC) + --fund_account_id= : (str, list) accounts to fund the + transaction + --preview : (bool) do not broadcast the transaction + --no_wait : (bool) do not wait for mempool confirmation + --page= : (int) page to return for paginating + --page_size= : (int) number of items on page for + pagination + --include_total : (bool) calculate total number of items and + pages Returns: (Wallet) deleted thing @@ -227,13 +229,13 @@ class TestGenerator(TestCase): thing list Options: - --value1= : (str) the first value - --value2= : (int) the second value with a very very long description - which needs to be wrapped - --value3 : (bool) a bool multi-line - --page= : (int) page to return for paginating - --page_size= : (int) number of items on page for pagination - --include_total : (bool) calculate total number of items and pages + --value1= : (str) the first value + --value2= : (int) the second value with a very very long description + which needs to be wrapped + --value3 : (bool) a bool multi-line + --page= : (int) page to return for paginating + --page_size= : (int) number of items on page for pagination + --include_total : (bool) calculate total number of items and pages Returns: (Paginated[Wallet]) list of wallets