From 0dc6a0b6c56f37533c255e0a9a06ded6595c81bf Mon Sep 17 00:00:00 2001 From: Arinerron Date: Sun, 22 Apr 2018 20:49:53 -0700 Subject: [PATCH] Refactor code --- onionr/onionr.py | 8 +++----- onionr/onionrproofs.py | 12 ++++++++---- onionr/onionrutils.py | 9 +++------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/onionr/onionr.py b/onionr/onionr.py index b04a9d4a..c3ef5a7a 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -556,13 +556,11 @@ class Onionr: return def get_hostname(self): - retVal = '' try: with open('./data/hs/hostname', 'r') as hostname: - retVal = hostname.read() - except FileNotFoundError: - retVal = None - return retVal + return hostname.read().strip() + except Exception: + return None def addFile(self): '''command to add a file to the onionr network''' diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index d190705a..fa62575b 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -17,10 +17,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' + import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger import btc + class POW: - def pow(self, reporting=False): + def pow(self, reporting = False): startTime = math.floor(time.time()) self.hashing = True self.reporting = reporting @@ -30,12 +32,12 @@ class POW: hbCount = 0 blockCheck = 300000 # How often the hasher should check if the bitcoin block is updated (slows hashing but prevents less wasted work) blockCheckCount = 0 - block = ''#self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) + block = '' #self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) while self.hashing: if blockCheckCount == blockCheck: if self.reporting: logger.debug('Refreshing Bitcoin block') - block = ''#self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) + block = '' #self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) blockCheckCount = 0 blockCheckCount += 1 hbCount += 1 @@ -77,7 +79,9 @@ class POW: self.difficulty = newDiff def getResult(self): - '''Returns the result then sets to false, useful to automatically clear the result''' + ''' + Returns the result then sets to false, useful to automatically clear the result + ''' try: retVal = self.result except AttributeError: diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 9baa41be..a61e9757 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -120,14 +120,11 @@ class OnionrUtils: def getMyAddress(self): try: - myAddressFile = open("data/hs/hostname", 'r') - myAddress = myAddressFile.read() - myAddressFile.close() - - return myAddress.strip() + with open('./data/hs/hostname', 'r') as hostname: + return hostname.read().strip() except Exception as error: logger.error('Failed to read my address.', error=error) - return '' + return None def localCommand(self, command, silent = True): '''