From 6f2e2e727e9420d2fae759b4c27981e7a87aeb7f Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 28 Nov 2016 13:23:10 -0600 Subject: [PATCH 1/2] fix pylint errors --- lbrynet/core/Wallet.py | 68 ++++++++++++++++-------------- lbrynet/db_migrator/dbmigrator.py | 8 ++-- lbrynet/db_migrator/migrate1to2.py | 18 +++++--- lbrynet/lbrynet_daemon/Daemon.py | 18 ++++---- 4 files changed, 61 insertions(+), 51 deletions(-) diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index 2db3b6ab2..ad7ee6a81 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -26,7 +26,7 @@ from lbrynet.interfaces import IRequestCreator, IQueryHandlerFactory, IQueryHand from lbrynet.core.client.ClientRequest import ClientRequest from lbrynet.core.Error import UnknownNameError, InvalidStreamInfoError, RequestCanceledError 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 log = logging.getLogger(__name__) @@ -44,22 +44,22 @@ class ClaimOutpoint(dict): if len(txid) != 64: raise TypeError('{} is not a txid'.format(txid)) self['txid'] = txid - self['nout'] = nout + self['nout'] = nout def __repr__(self): - return "{}:{}".format(txid,nout) + return "{}:{}".format(self['txid'], self['nout']) 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 - if 'nOut' in compare: - return (self['txid'],self['nout']) == (compare['txid'],compare['nOut']) - elif 'nout' in compare: - return (self['txid'],self['nout']) == (compare['txid'],compare['nout']) + if 'nOut' in compare: + return (self['txid'], self['nout']) == (compare['txid'], compare['nOut']) + elif 'nout' in compare: + return (self['txid'], self['nout']) == (compare['txid'], compare['nout']) else: raise TypeError('cannot compare {}'.format(type(compare))) def __ne__(self, compare): - return not self.__eq__(compare) + return not self.__eq__(compare) def _catch_connection_error(f): @@ -67,8 +67,9 @@ def _catch_connection_error(f): try: return f(*args) except socket.error: - raise ValueError("Unable to connect to an lbrycrd server. Make sure an lbrycrd server " + - "is running and that this application can connect to it.") + raise ValueError( + "Unable to connect to an lbrycrd server. Make sure an lbrycrd server " + + "is running and that this application can connect to it.") return w @@ -90,8 +91,9 @@ class Wallet(object): self.queued_payments = 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.expected_balance_at_time = deque() # (Peer, address(string), amount(Decimal), time(datetime), count(int), - # incremental_amount(float)) + # (Peer, address(string), amount(Decimal), time(datetime), count(int), + # incremental_amount(float)) + self.expected_balance_at_time = deque() self.max_expected_payment_time = datetime.timedelta(minutes=3) self.stopped = True @@ -193,7 +195,8 @@ class Wallet(object): d.addCallback(lambda _: set_next_manage_call()) 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 d.addErrback(log_error) @@ -213,14 +216,15 @@ class Wallet(object): return LBRYcrdAddressQueryHandlerFactory(self) def reserve_points(self, identifier, amount): - """ - Ensure a certain amount of points are available to be sent as payment, before the service is rendered + """Ensure a certain amount of points are available to be sent as + payment, before the service is rendered @param identifier: The peer to which the payment will ultimately be sent @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))) #if peer in self.peer_addresses: @@ -287,11 +291,13 @@ class Wallet(object): rounded_amount = Decimal(str(round(amount, 8))) assert(peer in self.current_address_given_to_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 expected_balance = self.expected_balances[address] 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) def update_peer_address(self, peer, address): @@ -337,12 +343,12 @@ class Wallet(object): return d 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']) def get_claim_for_name(claims): for claim in claims: - if claim_outpoint == claim: + if claim_outpoint == claim: claim['txid'] = txid return claim return Failure(UnknownNameError(name)) @@ -370,7 +376,7 @@ class Wallet(object): sd_hash = metadata['sources']['lbry_sd_hash'] claim_outpoint = ClaimOutpoint(result['txid'], result['n']) 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 _: metadata) return d @@ -416,9 +422,9 @@ class Wallet(object): def get_claim_info(self, name, txid=None, nout=None): if txid is None or nout is None: d = self._get_value_for_name(name) - d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'],r['n']))) - else: - d = self._get_claim_info(name, ClaimOutpoint(txid,nout)) + d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'], r['n']))) + else: + d = self._get_claim_info(name, ClaimOutpoint(txid, nout)) d.addErrback(lambda _: False) return d @@ -473,9 +479,9 @@ class Wallet(object): def claim_name(self, name, bid, m): def _save_metadata(claim_out, metadata): 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)) - 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'])) d = self._save_name_metadata(name, claim_outpoint, metadata['sources']['lbry_sd_hash']) d.addCallback(lambda _: claim_out) @@ -488,7 +494,7 @@ class Wallet(object): else: log.info("Updating over own claim") 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'], claim_outpoint, new_metadata, _bid)) @@ -534,11 +540,11 @@ class Wallet(object): def get_name_and_validity_for_sd_hash(self, sd_hash): def _get_status_of_claim(name_txid, sd_hash): 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] return self._get_status_of_claim(claim_outpoint, name, sd_hash) else: - return None + return None d = self._get_claim_metadata_for_sd_hash(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 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'] func = getattr(self.cmd_runner, cmd.name) d = threads.deferToThread(func, claim_outpoint['txid'], claim_outpoint['nout']) diff --git a/lbrynet/db_migrator/dbmigrator.py b/lbrynet/db_migrator/dbmigrator.py index 152f24476..29603f821 100644 --- a/lbrynet/db_migrator/dbmigrator.py +++ b/lbrynet/db_migrator/dbmigrator.py @@ -8,9 +8,9 @@ def migrate_db(db_dir, start, end): from lbrynet.db_migrator.migrate1to2 import do_migration do_migration(db_dir) 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 - return None @@ -21,7 +21,7 @@ def run_migration_script(): sys.stdout = open("migrator.out.log", 'w') sys.stderr = open("migrator.err.log", 'w') migrate_db(sys.argv[1], int(sys.argv[2]), int(sys.argv[3])) - - + + if __name__ == "__main__": run_migration_script() diff --git a/lbrynet/db_migrator/migrate1to2.py b/lbrynet/db_migrator/migrate1to2.py index 7ec6b5d79..23480d5ab 100644 --- a/lbrynet/db_migrator/migrate1to2.py +++ b/lbrynet/db_migrator/migrate1to2.py @@ -3,7 +3,7 @@ import os import logging log = logging.getLogger(__name__) -UNSET_NOUT = -1 +UNSET_NOUT = -1 def do_migration(db_dir): log.info("Doing the migration") @@ -12,10 +12,10 @@ def do_migration(db_dir): def migrate_blockchainname_db(db_dir): - blockchainname_db = os.path.join(db_dir,"blockchainname.db") - # skip migration on fresh installs + blockchainname_db = os.path.join(db_dir, "blockchainname.db") + # skip migration on fresh installs if not os.path.isfile(blockchainname_db): - return + return temp_db = sqlite3.connect(":memory:") db_file = sqlite3.connect(blockchainname_db) 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() 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: - 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: - 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() new_name_metadata = mem_cursor.execute("select * from name_metadata").fetchall() diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 8b2d0eaf0..8c7114381 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -47,7 +47,7 @@ from lbrynet.core.Wallet import LBRYcrdWallet, LBRYumWallet from lbrynet.core.looping_call_manager import LoopingCallManager from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory 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__) @@ -437,12 +437,12 @@ class Daemon(AuthJSONRPCServer): if not self.connected_to_internet: 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): txid = claim_out['txid'] nout = claim_out['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 def _check_pending_claims(self): @@ -459,7 +459,7 @@ class Daemon(AuthJSONRPCServer): def re_add_to_pending_claims(name): log.warning("Re-add %s to pending claims", 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) def _process_lbry_file(name, lbry_file): @@ -467,7 +467,7 @@ class Daemon(AuthJSONRPCServer): # TODO: check for sd_hash in addition to txid ready_to_start = ( 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: _get_and_start_file(name) @@ -679,7 +679,7 @@ class Daemon(AuthJSONRPCServer): 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: db_revision.write(str(version_num)) @@ -693,7 +693,7 @@ class Daemon(AuthJSONRPCServer): if not os.path.exists(self.blobfile_dir): os.mkdir(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") self._write_db_revision_file(old_revision) @@ -1679,7 +1679,7 @@ class Daemon(AuthJSONRPCServer): success : True if succesful , False otherwise reason : if not succesful, give reason 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(): txid = p['txid'] @@ -1692,7 +1692,7 @@ class Daemon(AuthJSONRPCServer): return self._render_response(x, OK_CODE) 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.callback(None) From 4b52c88d45f1f561b9d9037c8ab11671c0a9c92f Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 28 Nov 2016 13:55:38 -0600 Subject: [PATCH 2/2] fix functional tests --- tests/mocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mocks.py b/tests/mocks.py index 55c3ed45e..1525b51ba 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -57,7 +57,7 @@ class Wallet(object): pass def get_claim_metadata_for_sd_hash(self, sd_hash): - return "fakeuri", "faketxid" + return "fakeuri", "faketxid", "fakenout" class PeerFinder(object):