misc formatting improvements

master
Kevin Froman 2020-08-25 03:13:32 -05:00
parent 39650a4ca0
commit a83351a73c
4 changed files with 50 additions and 39 deletions

View File

@ -1,9 +1,22 @@
'''
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
Misc client API endpoints too small to need their own file and that need access to the client api inst
'''
'''
"""
# For the client creation thread
import threading
# For direct connection management HTTP endpoints
from flask import Response
# To make the direct connection management blueprint in the webUI
from flask import Blueprint
# Mainly to access the shared toomanyobjs object
from flask import g
import deadsimplekv
import filepaths
import onionrservices
from onionrservices import pool
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
@ -16,17 +29,8 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import threading # For the client creation thread
"""
from flask import Response # For direct connection management HTTP endpoints
from flask import Blueprint # To make the direct connection management blueprint in the webUI
from flask import g # Mainly to access the shared toomanyobjs object
import deadsimplekv
import filepaths
import onionrservices
from onionrservices import pool
def _get_communicator(g):
while True:
@ -35,9 +39,11 @@ def _get_communicator(g):
except KeyError:
pass
class DirectConnectionManagement:
def __init__(self, client_api):
direct_conn_management_bp = Blueprint('direct_conn_management', __name__)
direct_conn_management_bp = Blueprint(
'direct_conn_management', __name__)
self.direct_conn_management_bp = direct_conn_management_bp
cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage)
@ -54,7 +60,8 @@ class DirectConnectionManagement:
def make_new_connection(pubkey):
communicator = _get_communicator(g)
resp = "pending"
if pubkey in communicator.shared_state.get(pool.ServicePool).bootstrap_pending:
if pubkey in communicator.shared_state.get(
pool.ServicePool).bootstrap_pending:
return Response(resp)
if pubkey in communicator.direct_connection_clients:
@ -63,7 +70,9 @@ class DirectConnectionManagement:
"""Spawn a thread that will create the client and eventually add it to the
communicator.active_services
"""
threading.Thread(target=onionrservices.OnionrServices().create_client,
threading.Thread(
target=onionrservices.OnionrServices().create_client,
args=[pubkey, communicator], daemon=True).start()
return Response(resp)

View File

@ -1,8 +1,7 @@
'''
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
Import block data and save it
'''
"""
from onionrexceptions import BlacklistedBlock
from onionrexceptions import DiskAllocationReached
from onionrexceptions import InvalidProof
@ -10,12 +9,13 @@ from onionrexceptions import InvalidMetadata
import logger
from onionrutils import validatemetadata
from onionrutils import blockmetadata
from onionrutils import bytesconverter
from coredb import blockmetadb
import onionrstorage
import onionrcrypto as crypto
from . import onionrblacklist
'''
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
@ -28,17 +28,14 @@ from . import onionrblacklist
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
"""
def import_block_from_data(content):
blacklist = onionrblacklist.OnionrBlackList()
ret_data = False
try:
content = content.encode()
except AttributeError:
pass
content = bytesconverter.str_to_bytes(content)
data_hash = crypto.hashers.sha3_hash(content)
@ -56,15 +53,15 @@ def import_block_from_data(content):
logger.info(f'Imported block passed proof, saving: {data_hash}.',
terminal=True)
try:
blockHash = onionrstorage.set_data(content)
block_hash = onionrstorage.set_data(content)
except DiskAllocationReached:
logger.warn('Failed to save block due to full disk allocation')
raise
else:
blockmetadb.add_to_block_DB(blockHash, dataSaved=True)
blockmetadb.add_to_block_DB(block_hash, dataSaved=True)
# caches block metadata values to block database
blockmetadata.process_block_metadata(blockHash)
ret_data = blockHash
blockmetadata.process_block_metadata(block_hash)
ret_data = block_hash
else:
raise InvalidProof
else:

View File

@ -41,7 +41,8 @@ class BlockList:
if auto_refresh:
def auto_refresher():
observer = Observer()
observer.schedule(Refresher(), identify_home(), recursive=False)
observer.schedule(
Refresher(), identify_home(), recursive=False)
observer.start()
while observer.is_alive():
# call import func with timeout

View File

@ -35,7 +35,8 @@ class OnionrBlackList:
return
def inBlacklist(self, data):
hashed = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(data))
hashed = bytesconverter.bytes_to_str(
onionrcrypto.hashers.sha3_hash(data))
retData = False
if not hashed.isalnum():
@ -43,8 +44,10 @@ class OnionrBlackList:
if len(hashed) > 64:
raise Exception("Hashed data is too large")
for i in self._dbExecute("SELECT * FROM blacklist WHERE hash = ?", (hashed,)):
retData = True # this only executes if an entry is present by that hash
for i in self._dbExecute(
"SELECT * FROM blacklist WHERE hash = ?", (hashed,)):
# this only executes if an entry is present by that hash
retData = True
break
return retData
@ -70,7 +73,8 @@ class OnionrBlackList:
except AttributeError:
raise TypeError("dataType must be int")
for i in self._dbExecute('SELECT * FROM blacklist WHERE dataType = ?', (dataType,)):
for i in self._dbExecute(
'SELECT * FROM blacklist WHERE dataType = ?', (dataType,)):
if i[1] == dataType:
if (curTime - i[2]) >= i[3]:
deleteList.append(i[0])