From adf007bf309302a9c387f04a685db53df4a5150d Mon Sep 17 00:00:00 2001 From: Arinerron Date: Thu, 10 May 2018 22:18:39 -0700 Subject: [PATCH] Make progress bar --- Makefile | 5 +++++ onionr/communicator.py | 2 +- onionr/onionr.py | 1 - onionr/onionrutils.py | 22 ++++++++++++++++++- .../gui/main.py | 0 .../pluginmanager/main.py | 18 ++++++++++++++- 6 files changed, 44 insertions(+), 4 deletions(-) rename onionr/static-data/{default_plugins => default-plugins}/gui/main.py (100%) rename onionr/static-data/{default_plugins => default-plugins}/pluginmanager/main.py (84%) diff --git a/Makefile b/Makefile index 607478ba..7a7396df 100644 --- a/Makefile +++ b/Makefile @@ -31,3 +31,8 @@ reset: @echo "Hard-resetting Onionr..." rm -rf onionr/data/ | true > /dev/null 2>&1 #@./RUN-LINUX.sh version | grep -v "Failed" --color=always + +plugins-reset: + @echo "Resetting plugins..." + rm -rf onionr/data/plugins/ | true > /dev/null 2>&1 + @./RUN-LINUX.sh version | grep -v "Failed" --color=always diff --git a/onionr/communicator.py b/onionr/communicator.py index 70370e5e..34f4edd3 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -541,7 +541,7 @@ class OnionrCommunicate: logger.warn('Block is unsaved: %s' % str(i)) data = self.downloadBlock(i) - # if block was successfull gotten (hash already verified) + # if block was successfully gotten (hash already verified) if data: del self.newHashes[i] # remove from probation list diff --git a/onionr/onionr.py b/onionr/onionr.py index aa1ea838..643bb8ef 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -109,7 +109,6 @@ class Onionr: os.mkdir('data/blocks/') # Copy default plugins into plugins folder - if not os.path.exists(plugins.get_plugins_folder()): if os.path.exists('static-data/default-plugins/'): names = [f for f in os.listdir("static-data/default-plugins/") if not os.path.isfile(f)] diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 47c28ba7..0270023a 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' # Misc functions that do not fit in the main api, but are useful -import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob +import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil import nacl.signing, nacl.encoding if sys.version_info < (3, 6): @@ -448,6 +448,23 @@ class OnionrUtils: logger.warn('Failed to verify hash for ' + block) + def progressBar(self, value = 0, endvalue = 100, width = None): + ''' + Outputs a progress bar with a percentage. Write \n after use. + ''' + + if width is None or height is None: + width, height = shutil.get_terminal_size((80, 24)) + + bar_length = width - 6 + + percent = float(value) / endvalue + arrow = '─' * int(round(percent * bar_length)-1) + '>' + spaces = ' ' * (bar_length - len(arrow)) + + sys.stdout.write("\r┣{0}┫ {1}%".format(arrow + spaces, int(round(percent * 100)))) + sys.stdout.flush() + def size(path='.'): ''' Returns the size of a folder's contents in bytes @@ -465,6 +482,9 @@ def size(path='.'): return total def humanSize(num, suffix='B'): + ''' + Converts from bytes to a human readable format. + ''' for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: if abs(num) < 1024.0: return "%.1f %s%s" % (num, unit, suffix) diff --git a/onionr/static-data/default_plugins/gui/main.py b/onionr/static-data/default-plugins/gui/main.py similarity index 100% rename from onionr/static-data/default_plugins/gui/main.py rename to onionr/static-data/default-plugins/gui/main.py diff --git a/onionr/static-data/default_plugins/pluginmanager/main.py b/onionr/static-data/default-plugins/pluginmanager/main.py similarity index 84% rename from onionr/static-data/default_plugins/pluginmanager/main.py rename to onionr/static-data/default-plugins/pluginmanager/main.py index cfcf1911..d4f3129a 100644 --- a/onionr/static-data/default_plugins/pluginmanager/main.py +++ b/onionr/static-data/default-plugins/pluginmanager/main.py @@ -4,7 +4,7 @@ # useful libraries import logger, config -import os, sys, json +import os, sys, json, time, random plugin_name = 'pluginmanager' @@ -100,6 +100,14 @@ def commandInstallPlugin(): elif valid_hash and real_block: blockhash = str(pkobh) logger.debug('Using block %s...' % blockhash) + + logger.info('Downloading plugin...') + for i in range(0, 100): + pluginapi.get_utils().progressBar(i, 100) + time.sleep(random.random() / 5) + logger.info('Finished downloading plugin, verifying and installing...') + time.sleep(1) + logger.info('Installation successful.') elif valid_key and not real_key: logger.error('Public key not found. Try adding the node by address manually, if possible.') logger.debug('Is valid key, but the key is not a known one.') @@ -108,6 +116,14 @@ def commandInstallPlugin(): logger.debug('Using public key %s...' % publickey) saveKey(pluginname, pkobh) + + logger.info('Downloading plugin...') + for i in range(0, 100): + pluginapi.get_utils().progressBar(i, 100) + time.sleep(random.random() / 5) + logger.info('Finished downloading plugin, verifying and installing...') + time.sleep(1) + logger.info('Installation successful.') else: logger.error('Unknown data "%s"; must be public key or block hash.' % str(pkobh)) return