From 9fc741106acd82e78cac8e374f339a18c9d379fc Mon Sep 17 00:00:00 2001 From: Kevin Date: Fri, 17 Jul 2020 13:49:18 -0500 Subject: [PATCH] made localcommand pep8 compliant --- src/__init__.py | 3 +- .../downloadblocks/__init__.py | 2 +- src/httpapi/miscpublicapi/upload.py | 2 +- src/netcontroller/torcontrol/rebuildtor.py | 2 +- src/onionrblocks/insert/main.py | 6 ++-- src/onionrcommands/restartonionr.py | 2 +- src/onionrcommands/runtimetestcmd.py | 4 +-- src/onionrutils/localcommand.py | 36 ++++++++++--------- .../default-plugins/chat/peerserver.py | 5 +-- 9 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 08275f20..c53609c2 100755 --- a/src/__init__.py +++ b/src/__init__.py @@ -39,7 +39,8 @@ if __name__ == "__main__": ran_as_script = True try: from etc import dependencycheck # noqa except ModuleNotFoundError as e: - print('Onionr needs ' + str(e) + ' installed') + print('Missing requirement: ' + str(e) + ' installed') + sys.exit(1) # Import 3rd party libraries diff --git a/src/communicatorutils/downloadblocks/__init__.py b/src/communicatorutils/downloadblocks/__init__.py index cfb280dc..a2fca198 100755 --- a/src/communicatorutils/downloadblocks/__init__.py +++ b/src/communicatorutils/downloadblocks/__init__.py @@ -123,7 +123,7 @@ def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"): f'/daemon-event/upload_event', post=True, is_json=True, - postData={'block': blockHash} + post_data={'block': blockHash} ) else: logger.warn('POW failed for block %s.' % (blockHash,)) diff --git a/src/httpapi/miscpublicapi/upload.py b/src/httpapi/miscpublicapi/upload.py index cc0d27bb..6a4e102a 100755 --- a/src/httpapi/miscpublicapi/upload.py +++ b/src/httpapi/miscpublicapi/upload.py @@ -46,7 +46,7 @@ def accept_upload(request): f'/daemon-event/upload_event', post=True, is_json=True, - postData={'block': b_hash} + post_data={'block': b_hash} ).get(timeout=10) resp = 'success' else: diff --git a/src/netcontroller/torcontrol/rebuildtor.py b/src/netcontroller/torcontrol/rebuildtor.py index f063d9a1..77d91a91 100644 --- a/src/netcontroller/torcontrol/rebuildtor.py +++ b/src/netcontroller/torcontrol/rebuildtor.py @@ -30,5 +30,5 @@ def rebuild(): f'/daemon-event/restart_tor', post=True, is_json=True, - postData={} + post_data={} ).get(10) diff --git a/src/onionrblocks/insert/main.py b/src/onionrblocks/insert/main.py index c21b824b..14f00ea6 100644 --- a/src/onionrblocks/insert/main.py +++ b/src/onionrblocks/insert/main.py @@ -46,7 +46,7 @@ def _check_upload_queue(): raises OverflowError if max, false if api not running """ max_upload_queue: int = 5000 - queue = localcommand.local_command('/gethidden', maxWait=10) + queue = localcommand.local_command('/gethidden', max_wait=10) up_queue = False try: @@ -231,7 +231,7 @@ def insert_block(data: Union[str, bytes], header: str = 'txt', '/daemon-event/upload_event', post=True, is_json=True, - postData={'block': retData} + post_data={'block': retData} ).get(timeout=5) coredb.blockmetadb.add.add_to_block_DB( retData, selfInsert=True, dataSaved=True) @@ -268,7 +268,7 @@ def insert_block(data: Union[str, bytes], header: str = 'txt', localcommand.local_command, '/daemon-event/remove_from_insert_queue_wrapper', post=True, - postData={'block_hash': retData}, + post_data={'block_hash': retData}, is_json=True ).get(timeout=5) return retData diff --git a/src/onionrcommands/restartonionr.py b/src/onionrcommands/restartonionr.py index 616f8108..c58913af 100644 --- a/src/onionrcommands/restartonionr.py +++ b/src/onionrcommands/restartonionr.py @@ -52,7 +52,7 @@ def restart(): with open(filepaths.restarting_indicator, 'w') as f: f.write('t') daemonlaunch.kill_daemon() - while localcommand.local_command('ping', maxWait=8) == 'pong!': + while localcommand.local_command('ping', max_wait=8) == 'pong!': time.sleep(0.3) time.sleep(15) while (os.path.exists(filepaths.private_API_host_file) or diff --git a/src/onionrcommands/runtimetestcmd.py b/src/onionrcommands/runtimetestcmd.py index d523ba04..5cc092fb 100644 --- a/src/onionrcommands/runtimetestcmd.py +++ b/src/onionrcommands/runtimetestcmd.py @@ -28,8 +28,8 @@ def do_runtime_test(): f'daemon-event/test_runtime', post=True, is_json=True, - postData={}, - maxWait=300 + post_data={}, + max_wait=300 ).get(300) diff --git a/src/onionrutils/localcommand.py b/src/onionrutils/localcommand.py index b7e7aab2..1dbb31b0 100644 --- a/src/onionrutils/localcommand.py +++ b/src/onionrutils/localcommand.py @@ -4,7 +4,6 @@ send a command to the local API server """ import urllib import time -from typing import TYPE_CHECKING, Callable import requests import deadsimplekv @@ -37,7 +36,7 @@ cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage, def get_hostname(): hostname = '' waited = 0 - maxWait = 3 + max_wait = 3 while True: if cache.get('client_api') is None: try: @@ -51,22 +50,22 @@ def get_hostname(): hostname = cache.get('hostname') if hostname == '' or hostname is None: time.sleep(1) - if waited == maxWait: + if waited == max_wait: return False else: return hostname -def local_command(command, data='', silent = True, post=False, - postData = {}, maxWait=20, +def local_command(command, data='', silent=True, post=False, + post_data={}, max_wait=20, is_json=False ): - """ - Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers. - """ - # TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless. + """Send a command to the local http API server, securely. + Intended for local clients, DO NOT USE for remote peers.""" hostname = get_hostname() - if hostname == False: return False + # if the api host cannot be reached, return False + if not hostname: + return False if data != '': data = '&data=' + urllib.parse.quote_plus(data) @@ -79,21 +78,26 @@ def local_command(command, data='', silent = True, post=False, if is_json: ret_data = requests.post( payload, - json=postData, + json=post_data, headers={'token': config.get('client.webpassword'), 'Connection': 'close'}, - timeout=(maxWait, maxWait)).text + timeout=(max_wait, max_wait)).text else: ret_data = requests.post( payload, - data=postData, + data=post_data, headers={'token': config.get('client.webpassword'), 'Connection': 'close'}, - timeout=(maxWait, maxWait)).text + timeout=(max_wait, max_wait)).text else: - ret_data = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text + ret_data = requests.get(payload, + headers={'token': + config.get('client.webpassword'), + 'Connection': 'close'}, + timeout=(max_wait, max_wait)).text except Exception as error: if not silent: - logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) + logger.error('Failed to make local request (command: %s):%s' % + (command, error), terminal=True) ret_data = False return ret_data diff --git a/static-data/default-plugins/chat/peerserver.py b/static-data/default-plugins/chat/peerserver.py index 7bf0dda2..a37bf4b0 100755 --- a/static-data/default-plugins/chat/peerserver.py +++ b/static-data/default-plugins/chat/peerserver.py @@ -55,10 +55,11 @@ def sendto(): msg = '' else: msg = json.dumps(msg) - localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, postData=msg) + localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, post_data=msg) return Response('success') @direct_blueprint.route('/chat/poll') def poll_chat(): """Endpoints peers get new messages from""" - return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,))) \ No newline at end of file + return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,))) + \ No newline at end of file