diff --git a/onionr/logger.py b/onionr/logger.py index 01ada8ce..63469c8c 100755 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -32,6 +32,7 @@ class colors: strikethrough='\033[09m' invisible='\033[08m' italics='\033[3m' + class fg: black='\033[30m' red='\033[31m' @@ -64,9 +65,7 @@ class colors: ''' Use the bitwise operators to merge these settings ''' -USE_ANSI = 0b100 -if os.name == 'nt': - USE_ANSI = 0b000 +USE_ANSI = 0b000 if os.name == 'nt' else 0b100 OUTPUT_TO_CONSOLE = 0b010 OUTPUT_TO_FILE = 0b001 @@ -150,6 +149,7 @@ def log(prefix, data, color = '', timestamp=True, fd = sys.stdout, prompt = True data : The actual data to output color : The color to output before the data ''' + curTime = '' if timestamp: curTime = time.strftime("%m-%d %H:%M:%S") + ' ' @@ -210,33 +210,33 @@ def confirm(default = 'y', message = 'Are you sure %s? '): return default == 'y' # debug: when there is info that could be useful for debugging purposes only -def debug(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_DEBUG): +def debug(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_DEBUG): if get_level() <= level: log('/', data, timestamp = timestamp, prompt = prompt, terminal = terminal) if not error is None: debug('Error: ' + str(error) + parse_error()) # info: when there is something to notify the user of, such as the success of a process -def info(data, timestamp = False, prompt = True, terminal = False, level = LEVEL_INFO): +def info(data, timestamp = False, prompt = True, terminal = True, level = LEVEL_INFO): if get_level() <= level: log('+', data, colors.fg.green, timestamp = timestamp, prompt = prompt, terminal = terminal) # warn: when there is a potential for something bad to happen -def warn(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_WARN): +def warn(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_WARN): if not error is None: debug('Error: ' + str(error) + parse_error()) if get_level() <= level: log('!', data, colors.fg.orange, timestamp = timestamp, prompt = prompt, terminal = terminal) # error: when only one function, module, or process of the program encountered a problem and must stop -def error(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_ERROR): +def error(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_ERROR): if get_level() <= level: log('-', data, colors.fg.red, timestamp = timestamp, fd = sys.stderr, prompt = prompt, terminal = terminal) if not error is None: debug('Error: ' + str(error) + parse_error()) # fatal: when the something so bad has happened that the program must stop -def fatal(data, error = None, timestamp=True, prompt = True, terminal = False, level = LEVEL_FATAL): +def fatal(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_FATAL): if not error is None: debug('Error: ' + str(error) + parse_error(), terminal = terminal) if get_level() <= level: @@ -248,6 +248,9 @@ def parse_error(): output = '' for line in details: - output += '\n ... module %s in %s:%i' % (line[2], line[0], line[1]) + output += ('\n ... ' + colors.bold + '%s' + colors.reset + ' in %s:' + colors.bold + '%i' + colors.reset) % (line[2], line[0], line[1]) + + if not (get_settings() & USE_ANSI): + output = colors.filter(output) return output diff --git a/onionr/onionr.py b/onionr/onionr.py index 6ca5002e..be1a0f58 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -134,13 +134,8 @@ class Onionr: # initialize plugins events.event('init', onionr = self, threaded = False) - command = '' - try: - command = sys.argv[1].lower() - except IndexError: - command = '' - finally: - self.execute(command) + command = sys.argv[1].lower() if len(sys.argv) >= 2 else '' + self.execute(command) return @@ -285,8 +280,7 @@ class Onionr: # define commands commands = self.getCommands() - command = commands.get(argument, self.notFound) - command() + return commands.get(argument, self.notFound)() def version(self, verbosity = 5, function = logger.info): ''' diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index 8464659f..74460ded 100755 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -151,12 +151,16 @@ class Block: Outputs: - (bool): indicates whether or not the operation was successful ''' + try: # import from string blockdata = data # import from file if blockdata is None: + if self.getHash() is None: + return None + try: blockdata = onionrstorage.getData(self.core, self.getHash()).decode() except AttributeError: @@ -190,7 +194,7 @@ class Block: if len(self.getRaw()) <= config.get('allocations.blockCache', 500000): self.cache() - + if self.autoDecrypt: self.decrypt() @@ -677,7 +681,7 @@ class Block: ''' if isinstance(bHash, Block): bHash = bHash.getHash() - + ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None)) return not ret diff --git a/onionr/onionrcommands/__init__.py b/onionr/onionrcommands/__init__.py index 59c9d6bc..08a47a99 100755 --- a/onionr/onionrcommands/__init__.py +++ b/onionr/onionrcommands/__init__.py @@ -164,4 +164,4 @@ cmd_help = { 'change-id': 'Change active ID', 'open-home': 'Open your node\'s home/info screen', 'reset-tor': 'Delete the Tor data directory. Only do this if Tor never starts.' - } +} diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 51f35325..cd4240e4 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -24,8 +24,8 @@ from onionrutils import bytesconverter config.reload() def getDifficultyModifier(coreOrUtilsInst=None): - '''Accepts a core or utils instance returns - the difficulty modifier for block storage based + '''Accepts a core or utils instance returns + the difficulty modifier for block storage based on a variety of factors, currently only disk use. ''' classInst = coreOrUtilsInst @@ -110,14 +110,14 @@ class DataPOW: self.data = self.data.encode() except AttributeError: pass - + self.data = nacl.hash.blake2b(self.data) - logger.info('Computing POW (difficulty: %s)...' % self.difficulty) + logger.debug('Computing POW (difficulty: %s)...' % self.difficulty) self.mainHash = '0' * 70 self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))] - + myCore = core.Core() for i in range(max(1, threadCount)): t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore)) @@ -131,7 +131,7 @@ class DataPOW: answer = '' heartbeat = 200000 hbCount = 0 - + while self.hashing: rand = nacl.utils.random() token = nacl.hash.blake2b(rand + self.data).decode() @@ -141,7 +141,7 @@ class DataPOW: self.hashing = False iFound = True break - + if iFound: endTime = math.floor(time.time()) if self.reporting: @@ -160,12 +160,12 @@ class DataPOW: ''' Returns the result then sets to false, useful to automatically clear the result ''' - + try: retVal = self.result except AttributeError: retVal = False - + self.result = False return retVal @@ -207,15 +207,15 @@ class POW: self.data = self.data.encode() except AttributeError: pass - + if forceDifficulty > 0: self.difficulty = forceDifficulty else: # Calculate difficulty. Dumb for now, may use good algorithm in the future. self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=myCore) - - - logger.info('Computing POW (difficulty: %s)...' % self.difficulty) + + + logger.debug('Computing POW (difficulty: %s)...' % self.difficulty) self.mainHash = '0' * 64 self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))] @@ -251,7 +251,7 @@ class POW: self.result = payload break nonce += 1 - + if iFound: endTime = math.floor(time.time()) if self.reporting: @@ -268,12 +268,12 @@ class POW: ''' Returns the result then sets to false, useful to automatically clear the result ''' - + try: retVal = self.result except AttributeError: retVal = False - + self.result = False return retVal @@ -292,4 +292,4 @@ class POW: except KeyboardInterrupt: self.shutdown() logger.warn('Got keyboard interrupt while waiting for POW result, stopping') - return result \ No newline at end of file + return result diff --git a/onionr/onionrutils/importnewblocks.py b/onionr/onionrutils/importnewblocks.py index e1793adf..4a7f6327 100644 --- a/onionr/onionrutils/importnewblocks.py +++ b/onionr/onionrutils/importnewblocks.py @@ -45,4 +45,4 @@ def import_new_blocks(core_inst=None, scanDir=''): else: logger.warn('Failed to verify hash for %s' % block, terminal=True) if not exist: - logger.info('No blocks found to import', terminal=True) \ No newline at end of file + logger.debug('No blocks found to import', terminal=True) diff --git a/onionr/static-data/default-plugins/pluginmanager/main.py b/onionr/static-data/default-plugins/pluginmanager/main.py index 807feec6..07f957c9 100755 --- a/onionr/static-data/default-plugins/pluginmanager/main.py +++ b/onionr/static-data/default-plugins/pluginmanager/main.py @@ -226,7 +226,15 @@ def pluginToBlock(plugin, import_block = True): except: pass - metadata = {'author' : author, 'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), 'name' : plugin, 'info' : info, 'compiled-by' : plugin_name, 'content' : data.decode('utf-8'), 'description' : description} + metadata = { + 'author' : author, + 'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), + 'name' : plugin, + 'info' : info, + 'compiled-by' : plugin_name, + 'content' : data.decode('utf-8'), + 'description' : description + } block = Block(core = pluginapi.get_core()) @@ -250,6 +258,7 @@ def pluginToBlock(plugin, import_block = True): def installBlock(block): try: block = Block(block, core = pluginapi.get_core()) + print(block.getContent()) blockContent = json.loads(block.getContent()) name = sanitize(blockContent['name']) diff --git a/onionr/subprocesspow.py b/onionr/subprocesspow.py index 1ca2f9b1..03f375f0 100755 --- a/onionr/subprocesspow.py +++ b/onionr/subprocesspow.py @@ -29,7 +29,7 @@ class SubprocessPOW: def __init__(self, data, metadata, core_inst=None, subproc_count=None): ''' Onionr proof of work using multiple processes - Accepts block data, block metadata + Accepts block data, block metadata and optionally an onionr core library instance. if subproc_count is not set, os.cpu_count() is used to determine the number of processes @@ -55,8 +55,8 @@ class SubprocessPOW: self.data = bytesconverter.str_to_bytes(data) # Calculate difficulty. Dumb for now, may use good algorithm in the future. self.difficulty = onionrproofs.getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=self.core_inst) - - logger.info('Computing POW (difficulty: %s)...' % self.difficulty) + + logger.debug('Computing POW (difficulty: %s)...' % self.difficulty) self.mainHash = '0' * 64 self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))] @@ -74,7 +74,7 @@ class SubprocessPOW: else: self.shutdown = True return self.payload - + def _spawn_proc(self): # Create a child proof of work process, wait for data and send shutdown signal when its found parent_conn, child_conn = Pipe() @@ -117,4 +117,3 @@ class SubprocessPOW: pipe.send(payload) break nonce += 1 - \ No newline at end of file