Updated code quality bare except warning code changes in class_singleWorker.py

Added specific exceptions, added log warning and added and removed except handling after some method changes

Imported queue & configparser from six.moves

Added specific exceptions & updated logger warning msgs
This commit is contained in:
kuldeep.k@cisinlabs.com 2021-08-16 21:13:44 +05:30
parent d0c7ef2608
commit fc19c54836
No known key found for this signature in database
GPG Key ID: AF4FB299BF7C7C2A

View File

@ -32,6 +32,7 @@ from bmconfigparser import BMConfigParser
from helper_sql import sqlExecute, sqlQuery from helper_sql import sqlExecute, sqlQuery
from inventory import Inventory from inventory import Inventory
from network import knownnodes, StoppableThread from network import knownnodes, StoppableThread
from six.moves import configparser, queue
def sizeof_fmt(num, suffix='h/s'): def sizeof_fmt(num, suffix='h/s'):
@ -56,8 +57,8 @@ class singleWorker(StoppableThread):
try: try:
queues.workerQueue.put(("stopThread", "data")) queues.workerQueue.put(("stopThread", "data"))
except: except queue.Full:
pass self.logger.error('workerQueue is Full')
super(singleWorker, self).stopThread() super(singleWorker, self).stopThread()
def run(self): def run(self):
@ -148,38 +149,38 @@ class singleWorker(StoppableThread):
if command == 'sendmessage': if command == 'sendmessage':
try: try:
self.sendMsg() self.sendMsg()
except: except: # noqa:E722
pass self.logger.warning("sendMsg didn't work")
elif command == 'sendbroadcast': elif command == 'sendbroadcast':
try: try:
self.sendBroadcast() self.sendBroadcast()
except: except: # noqa:E722
pass self.logger.warning("sendBroadcast didn't work")
elif command == 'doPOWForMyV2Pubkey': elif command == 'doPOWForMyV2Pubkey':
try: try:
self.doPOWForMyV2Pubkey(data) self.doPOWForMyV2Pubkey(data)
except: except: # noqa:E722
pass self.logger.warning("doPOWForMyV2Pubkey didn't work")
elif command == 'sendOutOrStoreMyV3Pubkey': elif command == 'sendOutOrStoreMyV3Pubkey':
try: try:
self.sendOutOrStoreMyV3Pubkey(data) self.sendOutOrStoreMyV3Pubkey(data)
except: except: # noqa:E722
pass self.logger.warning("sendOutOrStoreMyV3Pubkey didn't work")
elif command == 'sendOutOrStoreMyV4Pubkey': elif command == 'sendOutOrStoreMyV4Pubkey':
try: try:
self.sendOutOrStoreMyV4Pubkey(data) self.sendOutOrStoreMyV4Pubkey(data)
except: except: # noqa:E722
pass self.logger.warning("sendOutOrStoreMyV4Pubkey didn't work")
elif command == 'sendOnionPeerObj': elif command == 'sendOnionPeerObj':
try: try:
self.sendOnionPeerObj(data) self.sendOnionPeerObj(data)
except: except: # noqa:E722
pass self.logger.warning("sendOnionPeerObj didn't work")
elif command == 'resetPoW': elif command == 'resetPoW':
try: try:
proofofwork.resetPoW() proofofwork.resetPoW()
except: except: # noqa:E722
pass self.logger.warning("proofofwork.resetPoW didn't work")
elif command == 'stopThread': elif command == 'stopThread':
self.busy = 0 self.busy = 0
return return
@ -243,8 +244,8 @@ class singleWorker(StoppableThread):
'PoW took %.1f seconds, speed %s.', 'PoW took %.1f seconds, speed %s.',
delta, sizeof_fmt(nonce / delta) delta, sizeof_fmt(nonce / delta)
) )
except: # NameError except: # noqa:E722 # NameError
pass self.logger.warning("Proof of Work exception")
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
return payload return payload
@ -271,6 +272,8 @@ class singleWorker(StoppableThread):
# privSigningKeyHex, privEncryptionKeyHex # privSigningKeyHex, privEncryptionKeyHex
_, _, pubSigningKey, pubEncryptionKey = \ _, _, pubSigningKey, pubEncryptionKey = \
self._getKeysForAddress(myAddress) self._getKeysForAddress(myAddress)
except (configparser.NoSectionError, configparser.NoOptionError) as err:
self.logger.warning("Section or Option did not found: %s", err)
except Exception as err: except Exception as err:
self.logger.error( self.logger.error(
'Error within doPOWForMyV2Pubkey. Could not read' 'Error within doPOWForMyV2Pubkey. Could not read'
@ -299,10 +302,12 @@ class singleWorker(StoppableThread):
BMConfigParser().set( BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time()))) myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save() BMConfigParser().save()
except: except configparser.NoSectionError:
# The user deleted the address out of the keys.dat file # The user deleted the address out of the keys.dat file
# before this finished. # before this finished.
pass pass
except: # noqa:E722
self.logger.warning("BMConfigParser().set didn't work")
def sendOutOrStoreMyV3Pubkey(self, adressHash): def sendOutOrStoreMyV3Pubkey(self, adressHash):
""" """
@ -312,8 +317,9 @@ class singleWorker(StoppableThread):
""" """
try: try:
myAddress = shared.myAddressesByHash[adressHash] myAddress = shared.myAddressesByHash[adressHash]
except: except KeyError:
# The address has been deleted. # The address has been deleted.
self.logger.warning("Can't find %s in myAddressByHash", hexlify(adressHash))
return return
if BMConfigParser().safeGetBoolean(myAddress, 'chan'): if BMConfigParser().safeGetBoolean(myAddress, 'chan'):
self.logger.info('This is a chan address. Not sending pubkey.') self.logger.info('This is a chan address. Not sending pubkey.')
@ -345,6 +351,8 @@ class singleWorker(StoppableThread):
# , privEncryptionKeyHex # , privEncryptionKeyHex
privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \ privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \
self._getKeysForAddress(myAddress) self._getKeysForAddress(myAddress)
except (configparser.NoSectionError, configparser.NoOptionError) as err:
self.logger.warning("Section or Option did not found: %s", err)
except Exception as err: except Exception as err:
self.logger.error( self.logger.error(
'Error within sendOutOrStoreMyV3Pubkey. Could not read' 'Error within sendOutOrStoreMyV3Pubkey. Could not read'
@ -382,10 +390,12 @@ class singleWorker(StoppableThread):
BMConfigParser().set( BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time()))) myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save() BMConfigParser().save()
except: except configparser.NoSectionError:
# The user deleted the address out of the keys.dat file # The user deleted the address out of the keys.dat file
# before this finished. # before this finished.
pass pass
except: # noqa:E722
self.logger.warning("BMConfigParser().set didn't work")
def sendOutOrStoreMyV4Pubkey(self, myAddress): def sendOutOrStoreMyV4Pubkey(self, myAddress):
""" """
@ -415,6 +425,8 @@ class singleWorker(StoppableThread):
# , privEncryptionKeyHex # , privEncryptionKeyHex
privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \ privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \
self._getKeysForAddress(myAddress) self._getKeysForAddress(myAddress)
except (configparser.NoSectionError, configparser.NoOptionError) as err:
self.logger.warning("Section or Option did not found: %s", err)
except Exception as err: except Exception as err:
self.logger.error( self.logger.error(
'Error within sendOutOrStoreMyV4Pubkey. Could not read' 'Error within sendOutOrStoreMyV4Pubkey. Could not read'
@ -546,7 +558,14 @@ class singleWorker(StoppableThread):
# , privEncryptionKeyHex # , privEncryptionKeyHex
privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \ privSigningKeyHex, _, pubSigningKey, pubEncryptionKey = \
self._getKeysForAddress(fromaddress) self._getKeysForAddress(fromaddress)
except: except (configparser.NoSectionError, configparser.NoOptionError) as err:
self.logger.warning("Section or Option did not found: %s", err)
except Exception as err:
self.logger.error(
'Error within sendBroadcast. Could not read'
' the keys from the keys.dat file for a requested'
' address. %s\n', err
)
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateSentItemStatusByAckdata', ( 'updateSentItemStatusByAckdata', (
ackdata, ackdata,
@ -1072,7 +1091,7 @@ class singleWorker(StoppableThread):
try: try:
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
toaddress, 'privencryptionkey') toaddress, 'privencryptionkey')
except Exception as err: except (configparser.NoSectionError, configparser.NoOptionError) as err:
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateSentItemStatusByAckdata', ( 'updateSentItemStatusByAckdata', (
ackdata, ackdata,
@ -1120,7 +1139,14 @@ class singleWorker(StoppableThread):
privSigningKeyHex, privEncryptionKeyHex, \ privSigningKeyHex, privEncryptionKeyHex, \
pubSigningKey, pubEncryptionKey = self._getKeysForAddress( pubSigningKey, pubEncryptionKey = self._getKeysForAddress(
fromaddress) fromaddress)
except: except (configparser.NoSectionError, configparser.NoOptionError) as err:
self.logger.warning("Section or Option did not found: %s", err)
except Exception as err:
self.logger.error(
'Error within sendMsg. Could not read'
' the keys from the keys.dat file for a requested'
' address. %s\n', err
)
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateSentItemStatusByAckdata', ( 'updateSentItemStatusByAckdata', (
ackdata, ackdata,
@ -1192,7 +1218,8 @@ class singleWorker(StoppableThread):
encrypted = highlevelcrypto.encrypt( encrypted = highlevelcrypto.encrypt(
payload, "04" + hexlify(pubEncryptionKeyBase256) payload, "04" + hexlify(pubEncryptionKeyBase256)
) )
except: except: # noqa:E722
self.logger.warning("highlevelcrypto.encrypt didn't work")
sqlExecute( sqlExecute(
'''UPDATE sent SET status='badkey' WHERE ackdata=? AND folder='sent' ''', '''UPDATE sent SET status='badkey' WHERE ackdata=? AND folder='sent' ''',
ackdata ackdata
@ -1243,8 +1270,8 @@ class singleWorker(StoppableThread):
time.time() - powStartTime, time.time() - powStartTime,
sizeof_fmt(nonce / (time.time() - powStartTime)) sizeof_fmt(nonce / (time.time() - powStartTime))
) )
except: except: # noqa:E722
pass self.logger.warning("Proof of Work exception")
encryptedPayload = pack('>Q', nonce) + encryptedPayload encryptedPayload = pack('>Q', nonce) + encryptedPayload
@ -1324,12 +1351,11 @@ class singleWorker(StoppableThread):
# has arrived. # has arrived.
if BMConfigParser().safeGetBoolean( if BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'apienabled'): 'bitmessagesettings', 'apienabled'):
try:
apiNotifyPath = BMConfigParser().get( apiNotifyPath = BMConfigParser().safeGet(
'bitmessagesettings', 'apinotifypath') 'bitmessagesettings', 'apinotifypath')
except:
apiNotifyPath = '' if apiNotifyPath:
if apiNotifyPath != '':
call([apiNotifyPath, "newMessage"]) call([apiNotifyPath, "newMessage"])
def requestPubKey(self, toAddress): def requestPubKey(self, toAddress):