Refactor initializer
parent
10ebdddb24
commit
d77bb92e28
|
@ -426,7 +426,7 @@ class Onionr:
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
return
|
return
|
||||||
|
|
||||||
addedHash = Block('txt', messageToAdd).save()
|
addedHash = Block(type = 'txt', content = messageToAdd).save()
|
||||||
if addedHash != None:
|
if addedHash != None:
|
||||||
logger.info("Message inserted as as block %s" % addedHash)
|
logger.info("Message inserted as as block %s" % addedHash)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -25,34 +25,16 @@ class Block:
|
||||||
blockCacheOrder = list() # NEVER write your own code that writes to this!
|
blockCacheOrder = list() # NEVER write your own code that writes to this!
|
||||||
blockCache = dict() # should never be accessed directly, look at Block.getCache()
|
blockCache = dict() # should never be accessed directly, look at Block.getCache()
|
||||||
|
|
||||||
def __init__(self, hash = None, core = None):
|
def __init__(self, hash = None, core = None, type = None, content = None):
|
||||||
'''
|
# take from arguments
|
||||||
Initializes Onionr
|
self.hash = hash
|
||||||
|
self.core = core
|
||||||
|
self.btype = type
|
||||||
|
self.bcontent = content
|
||||||
|
|
||||||
Inputs:
|
# sometimes people input a bytes object instead of str in `hash`
|
||||||
- hash (str): the hash of the block to be imported, if any
|
if isinstance(hash, bytes):
|
||||||
- core (Core/str):
|
hash = hash.decode()
|
||||||
- if (Core): this is the Core instance to be used, don't create a new one
|
|
||||||
- if (str): treat `core` as the block content, and instead, treat `hash` as the block type
|
|
||||||
|
|
||||||
Outputs:
|
|
||||||
- (Block): the new Block instance
|
|
||||||
'''
|
|
||||||
|
|
||||||
# input from arguments
|
|
||||||
if (type(hash) == str) and (type(core) == str):
|
|
||||||
self.btype = hash
|
|
||||||
self.bcontent = core
|
|
||||||
self.hash = None
|
|
||||||
self.core = None
|
|
||||||
else:
|
|
||||||
if type(hash) == bytes:
|
|
||||||
hash = hash.decode()
|
|
||||||
|
|
||||||
self.btype = ''
|
|
||||||
self.bcontent = ''
|
|
||||||
self.hash = hash
|
|
||||||
self.core = core
|
|
||||||
|
|
||||||
# initialize variables
|
# initialize variables
|
||||||
self.valid = True
|
self.valid = True
|
||||||
|
@ -70,6 +52,8 @@ class Block:
|
||||||
# handle arguments
|
# handle arguments
|
||||||
if self.getCore() is None:
|
if self.getCore() is None:
|
||||||
self.core = onionrcore.Core()
|
self.core = onionrcore.Core()
|
||||||
|
|
||||||
|
# update the blocks' contents if it exists
|
||||||
if not self.getHash() is None:
|
if not self.getHash() is None:
|
||||||
if not self.update():
|
if not self.update():
|
||||||
logger.debug('Failed to open block %s.' % self.getHash())
|
logger.debug('Failed to open block %s.' % self.getHash())
|
||||||
|
@ -126,13 +110,13 @@ class Block:
|
||||||
self.raw = str(blockdata)
|
self.raw = str(blockdata)
|
||||||
self.bheader = json.loads(self.getRaw()[:self.getRaw().index('\n')])
|
self.bheader = json.loads(self.getRaw()[:self.getRaw().index('\n')])
|
||||||
self.bcontent = self.getRaw()[self.getRaw().index('\n') + 1:]
|
self.bcontent = self.getRaw()[self.getRaw().index('\n') + 1:]
|
||||||
self.bmetadata = json.loads(self.getHeader('meta'))
|
self.bmetadata = json.loads(self.getHeader('meta', None))
|
||||||
self.parent = (None if not 'parent' in self.getMetadata() else self.getMetadata('parent'))
|
self.parent = self.getMetadata('parent', None)
|
||||||
self.btype = self.getMetadata('type')
|
self.btype = self.getMetadata('type', None)
|
||||||
self.powHash = self.getMetadata('powHash')
|
self.powHash = self.getMetadata('powHash', None)
|
||||||
self.powToken = self.getMetadata('powToken')
|
self.powToken = self.getMetadata('powToken', None)
|
||||||
self.signed = ('sig' in self.getHeader() and self.getHeader('sig') != '')
|
self.signed = ('sig' in self.getHeader() and self.getHeader('sig') != '')
|
||||||
self.signature = (None if not self.isSigned() else self.getHeader('sig'))
|
self.signature = self.getHeader('sig', None)
|
||||||
self.signedData = (None if not self.isSigned() else self.getHeader('meta') + '\n' + self.getContent())
|
self.signedData = (None if not self.isSigned() else self.getHeader('meta') + '\n' + self.getContent())
|
||||||
self.date = self.getCore().getBlockDate(self.getHash())
|
self.date = self.getCore().getBlockDate(self.getHash())
|
||||||
|
|
||||||
|
@ -235,7 +219,7 @@ class Block:
|
||||||
|
|
||||||
return str(self.raw)
|
return str(self.raw)
|
||||||
|
|
||||||
def getHeader(self, key = None):
|
def getHeader(self, key = None, default = None):
|
||||||
'''
|
'''
|
||||||
Returns the header information
|
Returns the header information
|
||||||
|
|
||||||
|
@ -247,10 +231,12 @@ class Block:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if not key is None:
|
if not key is None:
|
||||||
return self.getHeader()[key]
|
if key in self.getHeader():
|
||||||
|
return self.getHeader()[key]
|
||||||
|
return default
|
||||||
return self.bheader
|
return self.bheader
|
||||||
|
|
||||||
def getMetadata(self, key = None):
|
def getMetadata(self, key = None, default = None):
|
||||||
'''
|
'''
|
||||||
Returns the metadata information
|
Returns the metadata information
|
||||||
|
|
||||||
|
@ -262,7 +248,9 @@ class Block:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if not key is None:
|
if not key is None:
|
||||||
return self.getMetadata()[key]
|
if key in self.getMetadata():
|
||||||
|
return self.getMetadata()[key]
|
||||||
|
return default
|
||||||
return self.bmetadata
|
return self.bmetadata
|
||||||
|
|
||||||
def getContent(self):
|
def getContent(self):
|
||||||
|
@ -287,7 +275,7 @@ class Block:
|
||||||
if self.parent == self.getHash():
|
if self.parent == self.getHash():
|
||||||
self.parent = self
|
self.parent = self
|
||||||
elif Block.exists(self.parent):
|
elif Block.exists(self.parent):
|
||||||
self.parent = Block(self.getMetadata('parent'))
|
self.parent = Block(self.getMetadata('parent'), core = self.getCore())
|
||||||
else:
|
else:
|
||||||
self.parent = None
|
self.parent = None
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,7 @@ class OnionrTests(unittest.TestCase):
|
||||||
content = 'Onionr test block'
|
content = 'Onionr test block'
|
||||||
|
|
||||||
from onionrblockapi import Block
|
from onionrblockapi import Block
|
||||||
hash = Block('test', content).save()
|
hash = Block(type = 'test', content = content).save()
|
||||||
block = Block(hash) # test init
|
block = Block(hash) # test init
|
||||||
|
|
||||||
if len(Block.getBlocks(type = 'test')) == 0:
|
if len(Block.getBlocks(type = 'test')) == 0:
|
||||||
|
|
Loading…
Reference in New Issue