Compare commits
1 Commits
wot
...
refactorin
Author | SHA1 | Date | |
---|---|---|---|
|
39ac87b6ad |
@ -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
|
||||
|
@ -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):
|
||||
'''
|
||||
|
@ -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:
|
||||
|
@ -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.'
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ class DataPOW:
|
||||
|
||||
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))]
|
||||
@ -215,7 +215,7 @@ class POW:
|
||||
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))]
|
||||
|
@ -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)
|
||||
logger.debug('No blocks found to import', terminal=True)
|
||||
|
@ -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'])
|
||||
|
@ -56,7 +56,7 @@ class SubprocessPOW:
|
||||
# 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))]
|
||||
@ -117,4 +117,3 @@ class SubprocessPOW:
|
||||
pipe.send(payload)
|
||||
break
|
||||
nonce += 1
|
||||
|
Loading…
Reference in New Issue
Block a user