pylint in progress in lbry/extras/daemon

This commit is contained in:
Lex Berezhny 2020-01-03 01:15:33 -05:00
parent 867478697d
commit 88c7cfc745
5 changed files with 43 additions and 35 deletions

View file

@ -146,7 +146,7 @@ class WalletComponent(Component):
async def start(self): async def start(self):
log.info("Starting wallet") log.info("Starting wallet")
self.wallet_manager = await LbryWalletManager.from_lbrynet_config(self.conf) self.wallet_manager = await WalletManager.from_lbrynet_config(self.conf)
await self.wallet_manager.start() await self.wallet_manager.start()
async def stop(self): async def stop(self):
@ -333,9 +333,8 @@ class PeerProtocolServerComponent(Component):
async def start(self): async def start(self):
log.info("start blob server") log.info("start blob server")
upnp = self.component_manager.get_component(UPNP_COMPONENT)
blob_manager: BlobManager = self.component_manager.get_component(BLOB_COMPONENT) blob_manager: BlobManager = self.component_manager.get_component(BLOB_COMPONENT)
wallet: LbryWalletManager = self.component_manager.get_component(WALLET_COMPONENT) wallet: WalletManager = self.component_manager.get_component(WALLET_COMPONENT)
peer_port = self.conf.tcp_port peer_port = self.conf.tcp_port
address = await wallet.get_unused_address() address = await wallet.get_unused_address()
self.blob_server = BlobServer(asyncio.get_event_loop(), blob_manager, address) self.blob_server = BlobServer(asyncio.get_event_loop(), blob_manager, address)
@ -442,9 +441,9 @@ class UPnPComponent(Component):
log.info("refreshed upnp redirect for peer port: %i", tcp_port) log.info("refreshed upnp redirect for peer port: %i", tcp_port)
except (asyncio.TimeoutError, UPnPError, NotImplementedError): except (asyncio.TimeoutError, UPnPError, NotImplementedError):
del self.upnp_redirects['TCP'] del self.upnp_redirects['TCP']
if ('TCP' in self.upnp_redirects if ('TCP' in self.upnp_redirects and
and PEER_PROTOCOL_SERVER_COMPONENT not in self.component_manager.skip_components) and ( PEER_PROTOCOL_SERVER_COMPONENT not in self.component_manager.skip_components) and \
'UDP' in self.upnp_redirects and DHT_COMPONENT not in self.component_manager.skip_components): ('UDP' in self.upnp_redirects and DHT_COMPONENT not in self.component_manager.skip_components):
if self.upnp_redirects: if self.upnp_redirects:
log.debug("upnp redirects are still active") log.debug("upnp redirects are still active")

View file

@ -6,20 +6,21 @@ import json
import time import time
import inspect import inspect
import typing import typing
import base58
import random import random
import ecdsa
import hashlib import hashlib
from urllib.parse import urlencode, quote from urllib.parse import urlencode, quote
from typing import Callable, Optional, List from typing import Callable, Optional, List
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from traceback import format_exc from traceback import format_exc
from aiohttp import web
from functools import wraps, partial from functools import wraps, partial
import ecdsa
import base58
from aiohttp import web
from google.protobuf.message import DecodeError from google.protobuf.message import DecodeError
from lbry.wallet import ( from lbry.wallet import (
Wallet, WalletManager, ENCRYPT_ON_DISK, SingleKey, HierarchicalDeterministic, Wallet, ENCRYPT_ON_DISK, SingleKey, HierarchicalDeterministic,
Ledger, Transaction, Output, Input, Account Transaction, Output, Input, Account
) )
from lbry.wallet.dewies import dewies_to_lbc, lbc_to_dewies, dict_values_to_lbc from lbry.wallet.dewies import dewies_to_lbc, lbc_to_dewies, dict_values_to_lbc
@ -52,6 +53,7 @@ if typing.TYPE_CHECKING:
from lbry.extras.daemon.exchange_rate_manager import ExchangeRateManager from lbry.extras.daemon.exchange_rate_manager import ExchangeRateManager
from lbry.extras.daemon.storage import SQLiteStorage from lbry.extras.daemon.storage import SQLiteStorage
from lbry.stream.stream_manager import StreamManager from lbry.stream.stream_manager import StreamManager
from lbry.wallet import WalletManager, Ledger
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -67,8 +69,8 @@ def requires(*components, **conditions):
raise SyntaxError("invalid conditions argument") raise SyntaxError("invalid conditions argument")
condition_names = conditions.get("conditions", []) condition_names = conditions.get("conditions", [])
def _wrap(fn): def _wrap(method):
@wraps(fn) @wraps(method)
def _inner(*args, **kwargs): def _inner(*args, **kwargs):
component_manager = args[0].component_manager component_manager = args[0].component_manager
for condition_name in condition_names: for condition_name in condition_names:
@ -79,7 +81,7 @@ def requires(*components, **conditions):
raise ComponentsNotStartedError( raise ComponentsNotStartedError(
f"the following required components have not yet started: {json.dumps(components)}" f"the following required components have not yet started: {json.dumps(components)}"
) )
return fn(*args, **kwargs) return method(*args, **kwargs)
return _inner return _inner
@ -315,6 +317,9 @@ class Daemon(metaclass=JSONRPCServerType):
streaming_app.router.add_get('/stream/{sd_hash}', self.handle_stream_range_request) streaming_app.router.add_get('/stream/{sd_hash}', self.handle_stream_range_request)
self.streaming_runner = web.AppRunner(streaming_app) self.streaming_runner = web.AppRunner(streaming_app)
self.callable_methods = {}
self.deprecated_methods = {}
@property @property
def dht_node(self) -> typing.Optional['Node']: def dht_node(self) -> typing.Optional['Node']:
return self.component_manager.get_component(DHT_COMPONENT) return self.component_manager.get_component(DHT_COMPONENT)
@ -511,7 +516,7 @@ class Daemon(metaclass=JSONRPCServerType):
try: try:
encoded_result = jsonrpc_dumps_pretty( encoded_result = jsonrpc_dumps_pretty(
result, ledger=ledger, include_protobuf=include_protobuf) result, ledger=ledger, include_protobuf=include_protobuf)
except: except Exception:
log.exception('Failed to encode JSON RPC result:') log.exception('Failed to encode JSON RPC result:')
encoded_result = jsonrpc_dumps_pretty(JSONRPCError( encoded_result = jsonrpc_dumps_pretty(JSONRPCError(
JSONRPCError.CODE_APPLICATION_ERROR, JSONRPCError.CODE_APPLICATION_ERROR,
@ -576,7 +581,7 @@ class Daemon(metaclass=JSONRPCServerType):
) )
try: try:
fn = self._get_jsonrpc_method(function_name) method = self._get_jsonrpc_method(function_name)
except UnknownAPIMethodError: except UnknownAPIMethodError:
return JSONRPCError( return JSONRPCError(
JSONRPCError.CODE_METHOD_NOT_FOUND, JSONRPCError.CODE_METHOD_NOT_FOUND,
@ -603,7 +608,7 @@ class Daemon(metaclass=JSONRPCServerType):
if is_transactional_function(function_name): if is_transactional_function(function_name):
log.info("%s %s %s", function_name, _args, _kwargs) log.info("%s %s %s", function_name, _args, _kwargs)
params_error, erroneous_params = self._check_params(fn, _args, _kwargs) params_error, erroneous_params = self._check_params(method, _args, _kwargs)
if params_error is not None: if params_error is not None:
params_error_message = '{} for {} command: {}'.format( params_error_message = '{} for {} command: {}'.format(
params_error, function_name, ', '.join(erroneous_params) params_error, function_name, ', '.join(erroneous_params)
@ -615,7 +620,7 @@ class Daemon(metaclass=JSONRPCServerType):
) )
try: try:
result = fn(self, *_args, **_kwargs) result = method(self, *_args, **_kwargs)
if asyncio.iscoroutine(result): if asyncio.iscoroutine(result):
result = await result result = await result
return result return result
@ -660,7 +665,7 @@ class Daemon(metaclass=JSONRPCServerType):
for required_param in argspec.args[len(args_tup) + 1:-num_optional_params] for required_param in argspec.args[len(args_tup) + 1:-num_optional_params]
if required_param not in args_dict if required_param not in args_dict
] ]
if len(missing_required_params): if len(missing_required_params) > 0:
return 'Missing required parameters', missing_required_params return 'Missing required parameters', missing_required_params
extraneous_params = [] if argspec.varkw is not None else [ extraneous_params = [] if argspec.varkw is not None else [
@ -668,7 +673,7 @@ class Daemon(metaclass=JSONRPCServerType):
for extra_param in args_dict for extra_param in args_dict
if extra_param not in argspec.args[1:] if extra_param not in argspec.args[1:]
] ]
if len(extraneous_params): if len(extraneous_params) > 0:
return 'Extraneous parameters', extraneous_params return 'Extraneous parameters', extraneous_params
return None, None return None, None
@ -710,7 +715,8 @@ class Daemon(metaclass=JSONRPCServerType):
# # # #
############################################################################ ############################################################################
def jsonrpc_stop(self): @staticmethod
def jsonrpc_stop():
""" """
Stop lbrynet API server. Stop lbrynet API server.
@ -836,7 +842,8 @@ class Daemon(metaclass=JSONRPCServerType):
response[component.component_name] = status response[component.component_name] = status
return response return response
def jsonrpc_version(self): @staticmethod
def jsonrpc_version():
""" """
Get lbrynet API server version information Get lbrynet API server version information
@ -859,7 +866,7 @@ class Daemon(metaclass=JSONRPCServerType):
} }
""" """
platform_info = system_info.get_platform() platform_info = system_info.get_platform()
log.info("Get version info: " + json.dumps(platform_info)) log.info("Get version info: %s", json.dumps(platform_info))
return platform_info return platform_info
@requires(WALLET_COMPONENT) @requires(WALLET_COMPONENT)
@ -938,12 +945,12 @@ class Daemon(metaclass=JSONRPCServerType):
results = {} results = {}
valid_urls = set() valid_urls = set()
for u in urls: for url in urls:
try: try:
URL.parse(u) URL.parse(url)
valid_urls.add(u) valid_urls.add(url)
except ValueError: except ValueError:
results[u] = {"error": f"{u} is not a valid url"} results[url] = {"error": f"{url} is not a valid url"}
resolved = await self.resolve(wallet.accounts, list(valid_urls)) resolved = await self.resolve(wallet.accounts, list(valid_urls))
@ -2288,7 +2295,7 @@ class Daemon(metaclass=JSONRPCServerType):
kwargs['signature_valid'] = 0 kwargs['signature_valid'] = 0
page_num, page_size = abs(kwargs.pop('page', 1)), min(abs(kwargs.pop('page_size', DEFAULT_PAGE_SIZE)), 50) page_num, page_size = abs(kwargs.pop('page', 1)), min(abs(kwargs.pop('page_size', DEFAULT_PAGE_SIZE)), 50)
kwargs.update({'offset': page_size * (page_num - 1), 'limit': page_size}) kwargs.update({'offset': page_size * (page_num - 1), 'limit': page_size})
txos, offset, total = await self.ledger.claim_search(wallet.accounts, **kwargs) txos, _, total = await self.ledger.claim_search(wallet.accounts, **kwargs)
result = {"items": txos, "page": page_num, "page_size": page_size} result = {"items": txos, "page": page_num, "page_size": page_size}
if not kwargs.pop('no_totals', False): if not kwargs.pop('no_totals', False):
result['total_pages'] = int((total + (page_size - 1)) / page_size) result['total_pages'] = int((total + (page_size - 1)) / page_size)
@ -4754,7 +4761,8 @@ class Daemon(metaclass=JSONRPCServerType):
pass pass
return results return results
def _old_get_temp_claim_info(self, tx, txo, address, claim_dict, name, bid): @staticmethod
def _old_get_temp_claim_info(tx, txo, address, claim_dict, name, bid):
return { return {
"claim_id": txo.claim_id, "claim_id": txo.claim_id,
"name": name, "name": name,
@ -4768,9 +4776,9 @@ class Daemon(metaclass=JSONRPCServerType):
} }
def loggly_time_string(dt): def loggly_time_string(date):
formatted_dt = dt.strftime("%Y-%m-%dT%H:%M:%S") formatted_dt = date.strftime("%Y-%m-%dT%H:%M:%S")
milliseconds = str(round(dt.microsecond * (10.0 ** -5), 3)) milliseconds = str(round(date.microsecond * (10.0 ** -5), 3))
return quote(formatted_dt + milliseconds + "Z") return quote(formatted_dt + milliseconds + "Z")

View file

@ -1,8 +1,8 @@
import asyncio import asyncio
import collections import collections
import logging import logging
import aiohttp
import typing import typing
import aiohttp
from lbry import utils from lbry import utils
from lbry.conf import Config from lbry.conf import Config
from lbry.extras import system_info from lbry.extras import system_info

View file

@ -54,7 +54,7 @@ class MarketFeed:
def is_online(self): def is_online(self):
return self.last_check+self.update_interval+self.request_timeout > time.time() return self.last_check+self.update_interval+self.request_timeout > time.time()
def get_rate_from_response(self, response): def get_rate_from_response(self, json_response):
raise NotImplementedError() raise NotImplementedError()
async def get_response(self): async def get_response(self):

View file

@ -10,7 +10,7 @@ source =
ignore_missing_imports = True ignore_missing_imports = True
[pylint] [pylint]
ignore=words,server,rpc,schema,winpaths.py ignore=words,server,rpc,schema,winpaths.py,migrator
max-parents=10 max-parents=10
max-args=10 max-args=10
max-line-length=120 max-line-length=120
@ -26,6 +26,7 @@ disable=
expression-not-assigned, expression-not-assigned,
inconsistent-return-statements, inconsistent-return-statements,
too-few-public-methods, too-few-public-methods,
too-many-lines,
too-many-locals, too-many-locals,
too-many-branches, too-many-branches,
too-many-arguments, too-many-arguments,