Removing pylint issues

This commit is contained in:
Mathew WAller 2017-04-21 02:59:40 +01:00
parent 8f559c370a
commit 6e37a0536a
2 changed files with 87 additions and 51 deletions

View file

@ -29,12 +29,14 @@ log = logging.getLogger(__name__)
class ReservedPoints(object): class ReservedPoints(object):
def __init__(self, identifier, amount): def __init__(self, identifier, amount):
self.identifier = identifier self.identifier = identifier
self.amount = amount self.amount = amount
class ClaimOutpoint(dict): class ClaimOutpoint(dict):
def __init__(self, txid, nout): def __init__(self, txid, nout):
if len(txid) != 64: if len(txid) != 64:
raise TypeError('{} is not a txid'.format(txid)) raise TypeError('{} is not a txid'.format(txid))
@ -61,6 +63,7 @@ class ClaimOutpoint(dict):
class MetaDataStorage(object): class MetaDataStorage(object):
def load(self): def load(self):
return defer.succeed(True) return defer.succeed(True)
@ -81,6 +84,7 @@ class MetaDataStorage(object):
class InMemoryStorage(MetaDataStorage): class InMemoryStorage(MetaDataStorage):
def __init__(self): def __init__(self):
self.metadata = {} self.metadata = {}
self.claimids = {} self.claimids = {}
@ -98,7 +102,8 @@ class InMemoryStorage(MetaDataStorage):
return defer.succeed(None) return defer.succeed(None)
def update_claimid(self, claim_id, name, claim_outpoint): def update_claimid(self, claim_id, name, claim_outpoint):
self.claimids[(name, claim_outpoint['txid'], claim_outpoint['nout'])] = claim_id self.claimids[
(name, claim_outpoint['txid'], claim_outpoint['nout'])] = claim_id
return defer.succeed(True) return defer.succeed(True)
def get_claimid_for_tx(self, name, claim_outpoint): def get_claimid_for_tx(self, name, claim_outpoint):
@ -110,13 +115,15 @@ class InMemoryStorage(MetaDataStorage):
class SqliteStorage(MetaDataStorage): class SqliteStorage(MetaDataStorage):
def __init__(self, db_dir): def __init__(self, db_dir):
self.db_dir = db_dir self.db_dir = db_dir
self.db = None self.db = None
MetaDataStorage.__init__(self) MetaDataStorage.__init__(self)
def load(self): def load(self):
self.db = adbapi.ConnectionPool('sqlite3', os.path.join(self.db_dir, "blockchainname.db"), self.db = adbapi.ConnectionPool(
'sqlite3', os.path.join(self.db_dir, "blockchainname.db"),
check_same_thread=False) check_same_thread=False)
def create_tables(transaction): def create_tables(transaction):
@ -134,7 +141,8 @@ class SqliteStorage(MetaDataStorage):
return self.db.runInteraction(create_tables) return self.db.runInteraction(create_tables)
def clean_bad_records(self): def clean_bad_records(self):
d = self.db.runQuery("delete from name_metadata where length(txid) > 64 or txid is null") d = self.db.runQuery(
"delete from name_metadata where length(txid) > 64 or txid is null")
return d return d
def save_name_metadata(self, name, claim_outpoint, sd_hash): def save_name_metadata(self, name, claim_outpoint, sd_hash):
@ -153,7 +161,8 @@ class SqliteStorage(MetaDataStorage):
@rerun_if_locked @rerun_if_locked
def get_claim_metadata_for_sd_hash(self, sd_hash): def get_claim_metadata_for_sd_hash(self, sd_hash):
d = self.db.runQuery("select name, txid, n from name_metadata where sd_hash=?", (sd_hash,)) d = self.db.runQuery(
"select name, txid, n from name_metadata where sd_hash=?", (sd_hash,))
d.addCallback(lambda r: r[0] if r else None) d.addCallback(lambda r: r[0] if r else None)
return d return d
@ -181,6 +190,7 @@ class SqliteStorage(MetaDataStorage):
class Wallet(object): class Wallet(object):
"""This class implements the Wallet interface for the LBRYcrd payment system""" """This class implements the Wallet interface for the LBRYcrd payment system"""
implements(IWallet) implements(IWallet)
@ -192,8 +202,10 @@ class Wallet(object):
self.wallet_balance = Decimal(0.0) self.wallet_balance = Decimal(0.0)
self.total_reserved_points = Decimal(0.0) self.total_reserved_points = Decimal(0.0)
self.peer_addresses = {} # {Peer: string} self.peer_addresses = {} # {Peer: string}
self.queued_payments = defaultdict(Decimal) # {address(string): amount(Decimal)} self.queued_payments = defaultdict(
self.expected_balances = defaultdict(Decimal) # {address(string): amount(Decimal)} Decimal) # {address(string): amount(Decimal)}
self.expected_balances = defaultdict(
Decimal) # {address(string): amount(Decimal)}
self.current_address_given_to_peer = {} # {Peer: address(string)} self.current_address_given_to_peer = {} # {Peer: address(string)}
# (Peer, address(string), amount(Decimal), time(datetime), count(int), # (Peer, address(string), amount(Decimal), time(datetime), count(int),
# incremental_amount(float)) # incremental_amount(float))
@ -235,7 +247,8 @@ class Wallet(object):
@staticmethod @staticmethod
def log_stop_error(err): def log_stop_error(err):
log.error("An error occurred stopping the wallet: %s", err.getTraceback()) log.error(
"An error occurred stopping the wallet: %s", err.getTraceback())
def stop(self): def stop(self):
log.info("Stopping %s", self) log.info("Stopping %s", self)
@ -289,7 +302,8 @@ class Wallet(object):
def log_error(err): def log_error(err):
if isinstance(err, AttributeError): if isinstance(err, AttributeError):
log.warning("Failed to get an updated balance") log.warning("Failed to get an updated balance")
log.warning("Last balance update: %s", str(self.wallet_balance)) log.warning(
"Last balance update: %s", str(self.wallet_balance))
d.addCallbacks(lambda _: self.update_balance(), log_error) d.addCallbacks(lambda _: self.update_balance(), log_error)
return d return d
@ -298,7 +312,8 @@ class Wallet(object):
def set_next_manage_call(): def set_next_manage_call():
if not self.stopped: if not self.stopped:
self.next_manage_call = reactor.callLater(self._balance_refresh_time, self.manage) self.next_manage_call = reactor.callLater(
self._balance_refresh_time, self.manage)
d.addCallback(lambda _: set_next_manage_call()) d.addCallback(lambda _: set_next_manage_call())
@ -411,7 +426,8 @@ class Wallet(object):
str(address), str(rounded_amount)) str(address), str(rounded_amount))
self.expected_balances[address] += rounded_amount self.expected_balances[address] += rounded_amount
expected_balance = self.expected_balances[address] expected_balance = self.expected_balances[address]
expected_time = datetime.datetime.now() + self.max_expected_payment_time expected_time = datetime.datetime.now(
) + self.max_expected_payment_time
self.expected_balance_at_time.append( self.expected_balance_at_time.append(
(peer, address, expected_balance, expected_time, 0, amount)) (peer, address, expected_balance, expected_time, 0, amount))
peer.update_stats('expected_points', amount) peer.update_stats('expected_points', amount)
@ -432,7 +448,8 @@ class Wallet(object):
payments_to_send = {} payments_to_send = {}
for address, points in self.queued_payments.items(): for address, points in self.queued_payments.items():
if points > 0: if points > 0:
log.debug("Should be sending %s points to %s", str(points), str(address)) log.debug(
"Should be sending %s points to %s", str(points), str(address))
payments_to_send[address] = points payments_to_send[address] = points
self.total_reserved_points -= points self.total_reserved_points -= points
else: else:
@ -441,7 +458,8 @@ class Wallet(object):
del self.queued_payments[address] del self.queued_payments[address]
if payments_to_send: if payments_to_send:
log.debug("Creating a transaction with outputs %s", str(payments_to_send)) log.debug(
"Creating a transaction with outputs %s", str(payments_to_send))
d = self._do_send_many(payments_to_send) d = self._do_send_many(payments_to_send)
d.addCallback(lambda txid: log.debug("Sent transaction %s", txid)) d.addCallback(lambda txid: log.debug("Sent transaction %s", txid))
return d return d
@ -449,7 +467,7 @@ class Wallet(object):
log.debug("There were no payments to send") log.debug("There were no payments to send")
return defer.succeed(True) return defer.succeed(True)
###### #
@defer.inlineCallbacks @defer.inlineCallbacks
def get_claim(self, claim_id): def get_claim(self, claim_id):
@ -465,7 +483,8 @@ class Wallet(object):
claim['hex'] = claim['value'] claim['hex'] = claim['value']
claim['value'] = None claim['value'] = None
claim['error'] = "Failed to decode" claim['error'] = "Failed to decode"
log.warning("Failed to decode claim value for lbry://%s#%s", claim['name'], log.warning(
"Failed to decode claim value for lbry://%s#%s", claim['name'],
claim['claim_id']) claim['claim_id'])
defer.returnValue(claim) defer.returnValue(claim)
@ -576,7 +595,8 @@ class Wallet(object):
results['hex'] = claim_hex results['hex'] = claim_hex
results['value'] = claim_dict results['value'] = claim_dict
log.info("get claim info lbry://%s#%s", results['name'], results['claim_id']) log.info("get claim info lbry://%s#%s",
results['name'], results['claim_id'])
defer.returnValue(results) defer.returnValue(results)
@defer.inlineCallbacks @defer.inlineCallbacks
@ -626,7 +646,9 @@ class Wallet(object):
claim['hex'] = claim['value'] claim['hex'] = claim['value']
claim['value'] = None claim['value'] = None
claim['error'] = "Failed to decode" claim['error'] = "Failed to decode"
log.warning("Failed to decode claim value for lbry://%s#%s", claim['name'], log.warning(
"Failed to decode claim value for lbry://%s#%s", claim[
'name'],
claim['claim_id']) claim['claim_id'])
claims_for_return.append(claim) claims_for_return.append(claim)
@ -644,7 +666,8 @@ class Wallet(object):
raise Exception("Invalid channel name") raise Exception("Invalid channel name")
elif (parsed_channel_name.path or parsed_channel_name.claim_id or elif (parsed_channel_name.path or parsed_channel_name.claim_id or
parsed_channel_name.bid_position or parsed_channel_name.claim_sequence): parsed_channel_name.bid_position or parsed_channel_name.claim_sequence):
raise Exception("New channel claim should have no fields other than name") raise Exception(
"New channel claim should have no fields other than name")
log.info("Preparing to make certificate claim for %s", channel_name) log.info("Preparing to make certificate claim for %s", channel_name)
return self._claim_certificate(parsed_channel_name.name, amount) return self._claim_certificate(parsed_channel_name.name, amount)
@ -688,7 +711,8 @@ class Wallet(object):
claim = self._process_claim_out(claim) claim = self._process_claim_out(claim)
claim_outpoint = ClaimOutpoint(claim['txid'], claim['nout']) claim_outpoint = ClaimOutpoint(claim['txid'], claim['nout'])
log.info("Saving metadata for claim %s %d", claim['txid'], claim['nout']) log.info("Saving metadata for claim %s %d",
claim['txid'], claim['nout'])
yield self._update_claimid(claim['claim_id'], name, claim_outpoint) yield self._update_claimid(claim['claim_id'], name, claim_outpoint)
yield self._save_name_metadata(name, claim_outpoint, decoded.source_hash) yield self._save_name_metadata(name, claim_outpoint, decoded.source_hash)
defer.returnValue(claim) defer.returnValue(claim)
@ -698,7 +722,8 @@ class Wallet(object):
claim_out = yield self._abandon_claim(claim_id) claim_out = yield self._abandon_claim(claim_id)
if not claim_out['success']: if not claim_out['success']:
msg = 'Abandon of {} failed: {}'.format(claim_id, claim_out['reason']) msg = 'Abandon of {} failed: {}'.format(
claim_id, claim_out['reason'])
raise Exception(msg) raise Exception(msg)
claim_out = self._process_claim_out(claim_out) claim_out = self._process_claim_out(claim_out)
@ -707,7 +732,8 @@ class Wallet(object):
def support_claim(self, name, claim_id, amount): def support_claim(self, name, claim_id, amount):
def _parse_support_claim_out(claim_out): def _parse_support_claim_out(claim_out):
if not claim_out['success']: if not claim_out['success']:
msg = 'Support of {}:{} failed: {}'.format(name, claim_id, claim_out['reason']) msg = 'Support of {}:{} failed: {}'.format(
name, claim_id, claim_out['reason'])
raise Exception(msg) raise Exception(msg)
claim_out = self._process_claim_out(claim_out) claim_out = self._process_claim_out(claim_out)
return defer.succeed(claim_out) return defer.succeed(claim_out)
@ -740,22 +766,24 @@ class Wallet(object):
def get_balance(self, address=None): def get_balance(self, address=None):
if address is None: if address is None:
return self.wallet_balance - self.total_reserved_points - sum(self.queued_payments.values()) return self.wallet_balance-self.total_reserved_points-sum(self.queued_payments.values())
else: else:
c, u, x = self.wallet.get_addr_balance(address) c, u, x = self.wallet.get_addr_balance(address)
return Decimal(float(c) / COIN) return Decimal(float(c) / COIN)
def _check_expected_balances(self): def _check_expected_balances(self):
now = datetime.datetime.now() now = datetime.datetime.now()
balances_to_check = [] balances_to_check = []
try: try:
while self.expected_balance_at_time[0][3] < now: while self.expected_balance_at_time[0][3] < now:
balances_to_check.append(self.expected_balance_at_time.popleft()) balances_to_check.append(
self.expected_balance_at_time.popleft())
except IndexError: except IndexError:
pass pass
ds = [] ds = []
for balance_to_check in balances_to_check: for balance_to_check in balances_to_check:
log.debug("Checking balance of address %s", str(balance_to_check[1])) log.debug(
"Checking balance of address %s", str(balance_to_check[1]))
d = self._get_balance_for_address(balance_to_check[1]) d = self._get_balance_for_address(balance_to_check[1])
d.addCallback(lambda bal: bal >= balance_to_check[2]) d.addCallback(lambda bal: bal >= balance_to_check[2])
ds.append(d) ds.append(d)
@ -772,11 +800,13 @@ class Wallet(object):
balance[0], balance[0],
balance[1], balance[1],
balance[2], balance[2],
datetime.datetime.now() + self.max_expected_payment_time, datetime.datetime.now() +
self.max_expected_payment_time,
balance[4] + 1, balance[4] + 1,
balance[5] balance[5]
) )
self.expected_balance_at_time.append(new_expected_balance) self.expected_balance_at_time.append(
new_expected_balance)
peer.update_score(-5.0) peer.update_score(-5.0)
else: else:
peer.update_score(-50.0) peer.update_score(-50.0)
@ -787,7 +817,8 @@ class Wallet(object):
else: else:
log.warning("Something went wrong checking a balance. Peer: %s, account: %s," log.warning("Something went wrong checking a balance. Peer: %s, account: %s,"
"expected balance: %s, expected time: %s, count: %s, error: %s", "expected balance: %s, expected time: %s, count: %s, error: %s",
str(balance[0]), str(balance[1]), str(balance[2]), str(balance[3]), str(balance[0]), str(balance[1]), str(
balance[2]), str(balance[3]),
str(balance[4]), str(result.getErrorMessage())) str(balance[4]), str(result.getErrorMessage()))
dl.addCallback(handle_checks) dl.addCallback(handle_checks)
@ -866,6 +897,7 @@ class Wallet(object):
class LBRYumWallet(Wallet): class LBRYumWallet(Wallet):
def __init__(self, storage, config=None): def __init__(self, storage, config=None):
Wallet.__init__(self, storage) Wallet.__init__(self, storage)
self._config = config self._config = config
@ -896,7 +928,8 @@ class LBRYumWallet(Wallet):
def check_started(): def check_started():
if self.network.is_connecting(): if self.network.is_connecting():
if self._is_first_run(): if self._is_first_run():
log.info("Running the wallet for the first time. This may take a moment.") log.info(
"Running the wallet for the first time. This may take a moment.")
self.printed_retrieving_headers = True self.printed_retrieving_headers = True
return False return False
self._start_check.stop() self._start_check.stop()
@ -904,7 +937,8 @@ class LBRYumWallet(Wallet):
if self.network.is_connected(): if self.network.is_connected():
network_start_d.callback(True) network_start_d.callback(True)
else: else:
network_start_d.errback(ValueError("Failed to connect to network.")) network_start_d.errback(
ValueError("Failed to connect to network."))
self._start_check = task.LoopingCall(check_started) self._start_check = task.LoopingCall(check_started)
@ -960,8 +994,9 @@ class LBRYumWallet(Wallet):
def _check_large_wallet(self): def _check_large_wallet(self):
if len(self.wallet.addresses(include_change=False)) > 1000: if len(self.wallet.addresses(include_change=False)) > 1000:
log.warning(("Your wallet is excessively large, please follow instructions here: ", log.warning(
"https://github.com/lbryio/lbry/issues/437 to reduce your wallet size")) ("Your wallet is excessively large, please follow instructions here: ",
"https://github.com/lbryio/lbry/issues/437 to reduce your wallet size"))
def _load_blockchain(self): def _load_blockchain(self):
blockchain_caught_d = defer.Deferred() blockchain_caught_d = defer.Deferred()
@ -1006,7 +1041,7 @@ class LBRYumWallet(Wallet):
# run commands as a deferToThread, lbryum commands that only make # run commands as a deferToThread, lbryum commands that only make
# queries to lbryum server should be run this way # queries to lbryum server should be run this way
# TODO: keep track of running threads and cancel them on `stop` # TODO: keep track of running threads and cancel them on `stop`
# otherwise the application will hang, waiting for threads to complete # otherwise the application will hang, waiting for threads to complete
def _run_cmd_as_defer_to_thread(self, command_name, *args, **kwargs): def _run_cmd_as_defer_to_thread(self, command_name, *args, **kwargs):
cmd_runner = self._get_cmd_runner() cmd_runner = self._get_cmd_runner()
cmd = known_commands[command_name] cmd = known_commands[command_name]
@ -1016,7 +1051,8 @@ class LBRYumWallet(Wallet):
def _update_balance(self): def _update_balance(self):
accounts = None accounts = None
exclude_claimtrietx = True exclude_claimtrietx = True
d = self._run_cmd_as_defer_succeed('getbalance', accounts, exclude_claimtrietx) d = self._run_cmd_as_defer_succeed(
'getbalance', accounts, exclude_claimtrietx)
d.addCallback( d.addCallback(
lambda result: Decimal(result['confirmed']) + Decimal(result.get('unconfirmed', 0.0))) lambda result: Decimal(result['confirmed']) + Decimal(result.get('unconfirmed', 0.0)))
return d return d
@ -1071,7 +1107,8 @@ class LBRYumWallet(Wallet):
@defer.inlineCallbacks @defer.inlineCallbacks
def _send_name_claim(self, name, value, amount, certificate_id=None): def _send_name_claim(self, name, value, amount, certificate_id=None):
log.info("Send claim: %s for %s: %s ", name, amount, value) log.info("Send claim: %s for %s: %s ", name, amount, value)
claim_out = yield self._run_cmd_as_defer_succeed('claim', name, value, amount, claim_out = yield self._run_cmd_as_defer_succeed(
'claim', name, value, amount,
certificate_id=certificate_id) certificate_id=certificate_id)
defer.returnValue(claim_out) defer.returnValue(claim_out)
@ -1092,7 +1129,8 @@ class LBRYumWallet(Wallet):
@defer.inlineCallbacks @defer.inlineCallbacks
def _broadcast_claim_transaction(self, claim_out): def _broadcast_claim_transaction(self, claim_out):
if 'success' not in claim_out: if 'success' not in claim_out:
raise Exception('Unexpected claim command output: {}'.format(claim_out)) raise Exception(
'Unexpected claim command output: {}'.format(claim_out))
if claim_out['success']: if claim_out['success']:
yield self._broadcast_transaction(claim_out['tx']) yield self._broadcast_transaction(claim_out['tx'])
defer.returnValue(claim_out) defer.returnValue(claim_out)
@ -1108,11 +1146,14 @@ class LBRYumWallet(Wallet):
def _do_send_many(self, payments_to_send): def _do_send_many(self, payments_to_send):
def broadcast_send_many(paytomany_out): def broadcast_send_many(paytomany_out):
if 'hex' not in paytomany_out: if 'hex' not in paytomany_out:
raise Exception('Unepxected paytomany output:{}'.format(paytomany_out)) raise Exception(
'Unepxected paytomany output:{}'.format(paytomany_out))
return self._broadcast_transaction(paytomany_out['hex']) return self._broadcast_transaction(paytomany_out['hex'])
log.debug("Doing send many. payments to send: %s", str(payments_to_send)) log.debug(
d = self._run_cmd_as_defer_succeed('paytomany', payments_to_send.iteritems()) "Doing send many. payments to send: %s", str(payments_to_send))
d = self._run_cmd_as_defer_succeed(
'paytomany', payments_to_send.iteritems())
d.addCallback(lambda out: broadcast_send_many(out)) d.addCallback(lambda out: broadcast_send_many(out))
return d return d
@ -1199,7 +1240,8 @@ class LBRYcrdAddressRequester(object):
def _request_failed(self, err, peer): def _request_failed(self, err, peer):
if not err.check(RequestCanceledError): if not err.check(RequestCanceledError):
log.warning("A peer failed to send a valid public key response. Error: %s, peer: %s", log.warning(
"A peer failed to send a valid public key response. Error: %s, peer: %s",
err.getErrorMessage(), str(peer)) err.getErrorMessage(), str(peer))
return err return err
@ -1250,7 +1292,8 @@ class LBRYcrdAddressQueryHandler(object):
d.addCallback(create_response) d.addCallback(create_response)
return d return d
if self.address is None: if self.address is None:
log.warning("Expected a request for an address, but did not receive one") log.warning(
"Expected a request for an address, but did not receive one")
return defer.fail( return defer.fail(
Failure(ValueError("Expected but did not receive an address request"))) Failure(ValueError("Expected but did not receive an address request")))
else: else:

View file

@ -740,7 +740,6 @@ class Daemon(AuthJSONRPCServer):
@defer.inlineCallbacks @defer.inlineCallbacks
def _publish_stream(self, name, bid, claim_dict, file_path=None, certificate_id=None): def _publish_stream(self, name, bid, claim_dict, file_path=None, certificate_id=None):
publisher = Publisher(self.session, self.lbry_file_manager, self.session.wallet, publisher = Publisher(self.session, self.lbry_file_manager, self.session.wallet,
certificate_id) certificate_id)
verify_name_characters(name) verify_name_characters(name)
@ -1303,8 +1302,6 @@ class Daemon(AuthJSONRPCServer):
""" """
Return a list of available commands Return a list of available commands
Returns: Returns:
(list) list of available commands (list) list of available commands
""" """
@ -1313,24 +1310,20 @@ class Daemon(AuthJSONRPCServer):
if 'DEPRECATED' not in getattr(self, "jsonrpc_" + command).__doc__] if 'DEPRECATED' not in getattr(self, "jsonrpc_" + command).__doc__]
)) ))
def jsonrpc_get_balance(self, address=None): def jsonrpc_get_balance(self):
""" """
DEPRECATED. Use `wallet_balance` instead. DEPRECATED. Use `wallet_balance` instead.
""" """
return self.jsonrpc_wallet_balance(address) return self.jsonrpc_wallet_balance()
def jsonrpc_wallet_balance(self, address=None): def jsonrpc_wallet_balance(self):
""" """
Return the balance of the wallet Return the balance of the wallet
Args:
'address' (optional): (str) Address to get balance of
Returns: Returns:
(float) amount of lbry credits in wallet (float) amount of lbry credits in wallet
""" """
return self._render_response(float(self.session.wallet.get_balance(address))) return self._render_response(float(self.session.wallet.get_balance()))
def jsonrpc_stop(self): def jsonrpc_stop(self):
""" """