Merge pull request #291 from lbryio/fix-pylint

fix pylint errors
This commit is contained in:
Job Evers‐Meltzer 2016-11-28 15:32:16 -06:00 committed by GitHub
commit 9140c95a0c
5 changed files with 62 additions and 52 deletions

View file

@ -26,7 +26,7 @@ from lbrynet.interfaces import IRequestCreator, IQueryHandlerFactory, IQueryHand
from lbrynet.core.client.ClientRequest import ClientRequest from lbrynet.core.client.ClientRequest import ClientRequest
from lbrynet.core.Error import UnknownNameError, InvalidStreamInfoError, RequestCanceledError from lbrynet.core.Error import UnknownNameError, InvalidStreamInfoError, RequestCanceledError
from lbrynet.core.Error import InsufficientFundsError from lbrynet.core.Error import InsufficientFundsError
from lbrynet.db_migrator.migrate1to2 import UNSET_NOUT from lbrynet.db_migrator.migrate1to2 import UNSET_NOUT
from lbrynet.metadata.Metadata import Metadata from lbrynet.metadata.Metadata import Metadata
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -44,22 +44,22 @@ class ClaimOutpoint(dict):
if len(txid) != 64: if len(txid) != 64:
raise TypeError('{} is not a txid'.format(txid)) raise TypeError('{} is not a txid'.format(txid))
self['txid'] = txid self['txid'] = txid
self['nout'] = nout self['nout'] = nout
def __repr__(self): def __repr__(self):
return "{}:{}".format(txid,nout) return "{}:{}".format(self['txid'], self['nout'])
def __eq__(self, compare): def __eq__(self, compare):
if isinstance(compare,dict): if isinstance(compare, dict):
# TODO: lbryum returns nout's in dicts as "nOut" , need to fix this # TODO: lbryum returns nout's in dicts as "nOut" , need to fix this
if 'nOut' in compare: if 'nOut' in compare:
return (self['txid'],self['nout']) == (compare['txid'],compare['nOut']) return (self['txid'], self['nout']) == (compare['txid'], compare['nOut'])
elif 'nout' in compare: elif 'nout' in compare:
return (self['txid'],self['nout']) == (compare['txid'],compare['nout']) return (self['txid'], self['nout']) == (compare['txid'], compare['nout'])
else: else:
raise TypeError('cannot compare {}'.format(type(compare))) raise TypeError('cannot compare {}'.format(type(compare)))
def __ne__(self, compare): def __ne__(self, compare):
return not self.__eq__(compare) return not self.__eq__(compare)
def _catch_connection_error(f): def _catch_connection_error(f):
@ -67,8 +67,9 @@ def _catch_connection_error(f):
try: try:
return f(*args) return f(*args)
except socket.error: except socket.error:
raise ValueError("Unable to connect to an lbrycrd server. Make sure an lbrycrd server " + raise ValueError(
"is running and that this application can connect to it.") "Unable to connect to an lbrycrd server. Make sure an lbrycrd server " +
"is running and that this application can connect to it.")
return w return w
@ -90,8 +91,9 @@ class Wallet(object):
self.queued_payments = defaultdict(Decimal) # {address(string): amount(Decimal)} self.queued_payments = defaultdict(Decimal) # {address(string): amount(Decimal)}
self.expected_balances = defaultdict(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)}
self.expected_balance_at_time = deque() # (Peer, address(string), amount(Decimal), time(datetime), count(int), # (Peer, address(string), amount(Decimal), time(datetime), count(int),
# incremental_amount(float)) # incremental_amount(float))
self.expected_balance_at_time = deque()
self.max_expected_payment_time = datetime.timedelta(minutes=3) self.max_expected_payment_time = datetime.timedelta(minutes=3)
self.stopped = True self.stopped = True
@ -193,7 +195,8 @@ class Wallet(object):
d.addCallback(lambda _: set_next_manage_call()) d.addCallback(lambda _: set_next_manage_call())
def log_error(err): def log_error(err):
log.error("Something went wrong during manage. Error message: %s", err.getErrorMessage()) log.error("Something went wrong during manage. Error message: %s",
err.getErrorMessage())
return err return err
d.addErrback(log_error) d.addErrback(log_error)
@ -213,14 +216,15 @@ class Wallet(object):
return LBRYcrdAddressQueryHandlerFactory(self) return LBRYcrdAddressQueryHandlerFactory(self)
def reserve_points(self, identifier, amount): def reserve_points(self, identifier, amount):
""" """Ensure a certain amount of points are available to be sent as
Ensure a certain amount of points are available to be sent as payment, before the service is rendered payment, before the service is rendered
@param identifier: The peer to which the payment will ultimately be sent @param identifier: The peer to which the payment will ultimately be sent
@param amount: The amount of points to reserve @param amount: The amount of points to reserve
@return: A ReservedPoints object which is given to send_points once the service has been rendered @return: A ReservedPoints object which is given to send_points
once the service has been rendered
""" """
rounded_amount = Decimal(str(round(amount, 8))) rounded_amount = Decimal(str(round(amount, 8)))
#if peer in self.peer_addresses: #if peer in self.peer_addresses:
@ -287,11 +291,13 @@ class Wallet(object):
rounded_amount = Decimal(str(round(amount, 8))) rounded_amount = Decimal(str(round(amount, 8)))
assert(peer in self.current_address_given_to_peer) assert(peer in self.current_address_given_to_peer)
address = self.current_address_given_to_peer[peer] address = self.current_address_given_to_peer[peer]
log.info("expecting a payment at address %s in the amount of %s", str(address), str(rounded_amount)) log.info("expecting a payment at address %s in the amount of %s",
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((peer, address, expected_balance, expected_time, 0, amount)) self.expected_balance_at_time.append(
(peer, address, expected_balance, expected_time, 0, amount))
peer.update_stats('expected_points', amount) peer.update_stats('expected_points', amount)
def update_peer_address(self, peer, address): def update_peer_address(self, peer, address):
@ -337,12 +343,12 @@ class Wallet(object):
return d return d
def get_stream_info_from_claim_outpoint(self, name, txid, nout): def get_stream_info_from_claim_outpoint(self, name, txid, nout):
claim_outpoint = ClaimOutpoint(txid, nout) claim_outpoint = ClaimOutpoint(txid, nout)
d = self.get_claims_from_tx(claim_outpoint['txid']) d = self.get_claims_from_tx(claim_outpoint['txid'])
def get_claim_for_name(claims): def get_claim_for_name(claims):
for claim in claims: for claim in claims:
if claim_outpoint == claim: if claim_outpoint == claim:
claim['txid'] = txid claim['txid'] = txid
return claim return claim
return Failure(UnknownNameError(name)) return Failure(UnknownNameError(name))
@ -370,7 +376,7 @@ class Wallet(object):
sd_hash = metadata['sources']['lbry_sd_hash'] sd_hash = metadata['sources']['lbry_sd_hash']
claim_outpoint = ClaimOutpoint(result['txid'], result['n']) claim_outpoint = ClaimOutpoint(result['txid'], result['n'])
d = self._save_name_metadata(name, claim_outpoint, sd_hash) d = self._save_name_metadata(name, claim_outpoint, sd_hash)
d.addCallback(lambda _: self.get_claimid(name, result['txid'],result['n'])) d.addCallback(lambda _: self.get_claimid(name, result['txid'], result['n']))
d.addCallback(lambda cid: _log_success(cid)) d.addCallback(lambda cid: _log_success(cid))
d.addCallback(lambda _: metadata) d.addCallback(lambda _: metadata)
return d return d
@ -416,9 +422,9 @@ class Wallet(object):
def get_claim_info(self, name, txid=None, nout=None): def get_claim_info(self, name, txid=None, nout=None):
if txid is None or nout is None: if txid is None or nout is None:
d = self._get_value_for_name(name) d = self._get_value_for_name(name)
d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'],r['n']))) d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'], r['n'])))
else: else:
d = self._get_claim_info(name, ClaimOutpoint(txid,nout)) d = self._get_claim_info(name, ClaimOutpoint(txid, nout))
d.addErrback(lambda _: False) d.addErrback(lambda _: False)
return d return d
@ -473,9 +479,9 @@ class Wallet(object):
def claim_name(self, name, bid, m): def claim_name(self, name, bid, m):
def _save_metadata(claim_out, metadata): def _save_metadata(claim_out, metadata):
if not claim_out['success']: if not claim_out['success']:
msg = 'Claim to name {} failed: {}'.format(name,claim_out['reason']) msg = 'Claim to name {} failed: {}'.format(name, claim_out['reason'])
defer.fail(Exception(msg)) defer.fail(Exception(msg))
claim_outpoint = ClaimOutpoint(claim_out['txid'],claim_out['nout']) claim_outpoint = ClaimOutpoint(claim_out['txid'], claim_out['nout'])
log.info("Saving metadata for claim %s %d" % (claim_outpoint['txid'], claim_outpoint['nout'])) log.info("Saving metadata for claim %s %d" % (claim_outpoint['txid'], claim_outpoint['nout']))
d = self._save_name_metadata(name, claim_outpoint, metadata['sources']['lbry_sd_hash']) d = self._save_name_metadata(name, claim_outpoint, metadata['sources']['lbry_sd_hash'])
d.addCallback(lambda _: claim_out) d.addCallback(lambda _: claim_out)
@ -488,7 +494,7 @@ class Wallet(object):
else: else:
log.info("Updating over own claim") log.info("Updating over own claim")
d = self.update_metadata(metadata, claim['value']) d = self.update_metadata(metadata, claim['value'])
claim_outpoint = ClaimOutpoint(claim['txid'],claim['nOut']) claim_outpoint = ClaimOutpoint(claim['txid'], claim['nOut'])
d.addCallback(lambda new_metadata: self._send_name_claim_update(name, claim['claim_id'], d.addCallback(lambda new_metadata: self._send_name_claim_update(name, claim['claim_id'],
claim_outpoint, claim_outpoint,
new_metadata, _bid)) new_metadata, _bid))
@ -534,11 +540,11 @@ class Wallet(object):
def get_name_and_validity_for_sd_hash(self, sd_hash): def get_name_and_validity_for_sd_hash(self, sd_hash):
def _get_status_of_claim(name_txid, sd_hash): def _get_status_of_claim(name_txid, sd_hash):
if name_txid: if name_txid:
claim_outpoint = ClaimOutpoint(name_txid[1],name_txid[2]) claim_outpoint = ClaimOutpoint(name_txid[1], name_txid[2])
name = name_txid[0] name = name_txid[0]
return self._get_status_of_claim(claim_outpoint, name, sd_hash) return self._get_status_of_claim(claim_outpoint, name, sd_hash)
else: else:
return None return None
d = self._get_claim_metadata_for_sd_hash(sd_hash) d = self._get_claim_metadata_for_sd_hash(sd_hash)
d.addCallback(lambda name_txid: _get_status_of_claim(name_txid, sd_hash)) d.addCallback(lambda name_txid: _get_status_of_claim(name_txid, sd_hash))
@ -1352,7 +1358,7 @@ class LBRYumWallet(Wallet):
return decoded_tx return decoded_tx
def _abandon_claim(self, claim_outpoint): def _abandon_claim(self, claim_outpoint):
log.info("Abandon %s %s" % (claim_outpoint['txid'],claim_outpoint['nout'])) log.info("Abandon %s %s" % (claim_outpoint['txid'], claim_outpoint['nout']))
cmd = known_commands['abandon'] cmd = known_commands['abandon']
func = getattr(self.cmd_runner, cmd.name) func = getattr(self.cmd_runner, cmd.name)
d = threads.deferToThread(func, claim_outpoint['txid'], claim_outpoint['nout']) d = threads.deferToThread(func, claim_outpoint['txid'], claim_outpoint['nout'])

View file

@ -8,9 +8,9 @@ def migrate_db(db_dir, start, end):
from lbrynet.db_migrator.migrate1to2 import do_migration from lbrynet.db_migrator.migrate1to2 import do_migration
do_migration(db_dir) do_migration(db_dir)
else: else:
raise Exception("DB migration of version {} to {} is not available".format(current,current+1)) raise Exception(
"DB migration of version {} to {} is not available".format(current, current+1))
current += 1 current += 1
return None return None
@ -21,7 +21,7 @@ def run_migration_script():
sys.stdout = open("migrator.out.log", 'w') sys.stdout = open("migrator.out.log", 'w')
sys.stderr = open("migrator.err.log", 'w') sys.stderr = open("migrator.err.log", 'w')
migrate_db(sys.argv[1], int(sys.argv[2]), int(sys.argv[3])) migrate_db(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
if __name__ == "__main__": if __name__ == "__main__":
run_migration_script() run_migration_script()

View file

@ -3,7 +3,7 @@ import os
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
UNSET_NOUT = -1 UNSET_NOUT = -1
def do_migration(db_dir): def do_migration(db_dir):
log.info("Doing the migration") log.info("Doing the migration")
@ -12,10 +12,10 @@ def do_migration(db_dir):
def migrate_blockchainname_db(db_dir): def migrate_blockchainname_db(db_dir):
blockchainname_db = os.path.join(db_dir,"blockchainname.db") blockchainname_db = os.path.join(db_dir, "blockchainname.db")
# skip migration on fresh installs # skip migration on fresh installs
if not os.path.isfile(blockchainname_db): if not os.path.isfile(blockchainname_db):
return return
temp_db = sqlite3.connect(":memory:") temp_db = sqlite3.connect(":memory:")
db_file = sqlite3.connect(blockchainname_db) db_file = sqlite3.connect(blockchainname_db)
file_cursor = db_file.cursor() file_cursor = db_file.cursor()
@ -36,12 +36,16 @@ def migrate_blockchainname_db(db_dir):
name_metadata = file_cursor.execute("select * from name_metadata").fetchall() name_metadata = file_cursor.execute("select * from name_metadata").fetchall()
claim_metadata = file_cursor.execute("select * from claim_ids").fetchall() claim_metadata = file_cursor.execute("select * from claim_ids").fetchall()
# fill n as V1_UNSET_NOUT, Wallet.py will be responsible for filling in correct n # fill n as V1_UNSET_NOUT, Wallet.py will be responsible for filling in correct n
for name, txid, sd_hash in name_metadata: for name, txid, sd_hash in name_metadata:
mem_cursor.execute("insert into name_metadata values (?, ?, ?, ?) ", (name, txid, UNSET_NOUT, sd_hash)) mem_cursor.execute(
"insert into name_metadata values (?, ?, ?, ?) ",
(name, txid, UNSET_NOUT, sd_hash))
for claim_id, name, txid in claim_metadata: for claim_id, name, txid in claim_metadata:
mem_cursor.execute("insert into claim_ids values (?, ?, ?, ?)", (claim_id, name, txid, UNSET_NOUT)) mem_cursor.execute(
"insert into claim_ids values (?, ?, ?, ?)",
(claim_id, name, txid, UNSET_NOUT))
temp_db.commit() temp_db.commit()
new_name_metadata = mem_cursor.execute("select * from name_metadata").fetchall() new_name_metadata = mem_cursor.execute("select * from name_metadata").fetchall()

View file

@ -47,7 +47,7 @@ from lbrynet.core.Wallet import LBRYcrdWallet, LBRYumWallet
from lbrynet.core.looping_call_manager import LoopingCallManager from lbrynet.core.looping_call_manager import LoopingCallManager
from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory
from lbrynet.core.server.ServerProtocol import ServerProtocolFactory from lbrynet.core.server.ServerProtocol import ServerProtocolFactory
from lbrynet.core.Error import InsufficientFundsError, InvalidNameError, UnknownNameError from lbrynet.core.Error import InsufficientFundsError, InvalidNameError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -437,12 +437,12 @@ class Daemon(AuthJSONRPCServer):
if not self.connected_to_internet: if not self.connected_to_internet:
self.connection_problem = CONNECTION_PROBLEM_CODES[1] self.connection_problem = CONNECTION_PROBLEM_CODES[1]
# claim_out is dictionary containing 'txid' and 'nout' # claim_out is dictionary containing 'txid' and 'nout'
def _add_to_pending_claims(self, name, claim_out): def _add_to_pending_claims(self, name, claim_out):
txid = claim_out['txid'] txid = claim_out['txid']
nout = claim_out['nout'] nout = claim_out['nout']
log.info("Adding lbry://%s to pending claims, txid %s nout %d" % (name, txid, nout)) log.info("Adding lbry://%s to pending claims, txid %s nout %d" % (name, txid, nout))
self.pending_claims[name] = (txid,nout) self.pending_claims[name] = (txid, nout)
return claim_out return claim_out
def _check_pending_claims(self): def _check_pending_claims(self):
@ -459,7 +459,7 @@ class Daemon(AuthJSONRPCServer):
def re_add_to_pending_claims(name): def re_add_to_pending_claims(name):
log.warning("Re-add %s to pending claims", name) log.warning("Re-add %s to pending claims", name)
txid, nout = self.pending_claims.pop(name) txid, nout = self.pending_claims.pop(name)
claim_out = {'txid':txid,'nout':nout} claim_out = {'txid':txid, 'nout':nout}
self._add_to_pending_claims(name, claim_out) self._add_to_pending_claims(name, claim_out)
def _process_lbry_file(name, lbry_file): def _process_lbry_file(name, lbry_file):
@ -467,7 +467,7 @@ class Daemon(AuthJSONRPCServer):
# TODO: check for sd_hash in addition to txid # TODO: check for sd_hash in addition to txid
ready_to_start = ( ready_to_start = (
lbry_file and lbry_file and
self.pending_claims[name] == (lbry_file.txid,lbry_file.nout) self.pending_claims[name] == (lbry_file.txid, lbry_file.nout)
) )
if ready_to_start: if ready_to_start:
_get_and_start_file(name) _get_and_start_file(name)
@ -679,7 +679,7 @@ class Daemon(AuthJSONRPCServer):
return defer.succeed(True) return defer.succeed(True)
def _write_db_revision_file(self,version_num): def _write_db_revision_file(self, version_num):
with open(self.db_revision_file, mode='w') as db_revision: with open(self.db_revision_file, mode='w') as db_revision:
db_revision.write(str(version_num)) db_revision.write(str(version_num))
@ -693,7 +693,7 @@ class Daemon(AuthJSONRPCServer):
if not os.path.exists(self.blobfile_dir): if not os.path.exists(self.blobfile_dir):
os.mkdir(self.blobfile_dir) os.mkdir(self.blobfile_dir)
log.debug("Created the blobfile directory: %s", str(self.blobfile_dir)) log.debug("Created the blobfile directory: %s", str(self.blobfile_dir))
if not os.path.exists(self.db_revision_file): if not os.path.exists(self.db_revision_file):
log.warning("db_revision file not found. Creating it") log.warning("db_revision file not found. Creating it")
self._write_db_revision_file(old_revision) self._write_db_revision_file(old_revision)
@ -1705,7 +1705,7 @@ class Daemon(AuthJSONRPCServer):
success : True if succesful , False otherwise success : True if succesful , False otherwise
reason : if not succesful, give reason reason : if not succesful, give reason
txid : txid of resulting transaction if succesful txid : txid of resulting transaction if succesful
fee : fee paid for the transaction if succesful fee : fee paid for the transaction if succesful
""" """
if 'txid' in p.keys() and 'nout' in p.keys(): if 'txid' in p.keys() and 'nout' in p.keys():
txid = p['txid'] txid = p['txid']
@ -1718,7 +1718,7 @@ class Daemon(AuthJSONRPCServer):
return self._render_response(x, OK_CODE) return self._render_response(x, OK_CODE)
d = defer.Deferred() d = defer.Deferred()
d.addCallback(lambda _: self.session.wallet.abandon_claim(txid,nout)) d.addCallback(lambda _: self.session.wallet.abandon_claim(txid, nout))
d.addCallback(_disp) d.addCallback(_disp)
d.callback(None) d.callback(None)

View file

@ -57,7 +57,7 @@ class Wallet(object):
pass pass
def get_claim_metadata_for_sd_hash(self, sd_hash): def get_claim_metadata_for_sd_hash(self, sd_hash):
return "fakeuri", "faketxid" return "fakeuri", "faketxid", "fakenout"
class PeerFinder(object): class PeerFinder(object):