From 4b6052f502e90cf776bef14cccdc9a020f14ad11 Mon Sep 17 00:00:00 2001 From: Marco Zeisler Date: Mon, 30 Nov 2020 18:58:04 +0100 Subject: [PATCH] reformat code; --- iotclient/iot_client.py | 55 ++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/iotclient/iot_client.py b/iotclient/iot_client.py index d33050d..222a060 100644 --- a/iotclient/iot_client.py +++ b/iotclient/iot_client.py @@ -5,12 +5,15 @@ import json import time from datetime import datetime + def print_response(response): print("Code: " + str(response.status_code) + "; Body: " + str(response.json())) + def unwrap_file(file_str: str) -> bytes: return base64.b64decode(file_str) + def send_image(identifier, image_path, metadata_payload): print("Sending image with identifier " + identifier) if not os.path.isfile(image_path): @@ -41,10 +44,10 @@ def send_image(identifier, image_path, metadata_payload): def get_image(identifier): - print("Getting image with identifier "+identifier) + print("Getting image with identifier " + identifier) baseurl = "http://127.0.0.1:8000" - get_url = "/image/get/"+identifier + get_url = "/image/get/" + identifier try: response = requests.get(baseurl + get_url) @@ -53,9 +56,10 @@ def get_image(identifier): print(payload['metadata']) b64encoded_image = unwrap_file(payload['image_data']) goal_folder_name = "images_fetch" - path = "../../"+goal_folder_name+"/"+payload['metadata']['filename'] + path = "../../" + goal_folder_name + "/" + payload['metadata']['filename'] - goal_folder_path = os.path.sep.join(os.path.abspath(__file__).split(os.path.sep)[:-3]) + os.path.sep + goal_folder_name + goal_folder_path = os.path.sep.join( + os.path.abspath(__file__).split(os.path.sep)[:-3]) + os.path.sep + goal_folder_name if not os.path.isdir(goal_folder_path): os.mkdir(goal_folder_path) @@ -70,10 +74,10 @@ def get_image(identifier): def delete_image(identifier): - print("Deleting image with identifier "+identifier) + print("Deleting image with identifier " + identifier) baseurl = "http://127.0.0.1:8000" - get_url = "/image/delete/"+identifier + get_url = "/image/delete/" + identifier try: response = requests.delete(baseurl + get_url) @@ -84,7 +88,6 @@ def delete_image(identifier): return True - def get_all(): print("Getting all images") @@ -98,6 +101,7 @@ def get_all(): print("Error sending request") return True + metadata = None index = 0 metadata_folder = "." + os.path.sep @@ -106,22 +110,22 @@ metadata_path = metadata_folder + os.path.sep + metadata_file image_folder = "." + os.path.sep + "images" - if (os.path.isfile(metadata_path)): print("Loading metadata to memory from " + metadata_path) - metadata = sorted(json.load(open(metadata_file, "r")), key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)')) + metadata = sorted(json.load(open(metadata_file, "r")), + key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)')) else: print("Default metadata_file not found.") if (os.path.isdir(image_folder)): - print("Image folder set to " + image_folder) + print("Image folder set to " + image_folder) else: print("Default image folder not found.") -#main input loop +# main input loop command = "dummy" while (command.lower() not in ["exit", "quit", "end"]): - + try: command = input("Please enter command: ") except: @@ -154,19 +158,19 @@ while (command.lower() not in ["exit", "quit", "end"]): print("select | - select image with given as next in line") print() print() - - if command.lower() == "dir": + + if command.lower() == "dir": print("Current image folder is " + image_folder) - + if command.lower() == "len": if metadata is None: print("No metadata selected") else: print("Length of metadata list " + str(len(metadata))) - - if command.lower() == "index": + + if command.lower() == "index": print("Current lineup index is " + str(index)) - + if command.lower() == "info": if metadata is None: print("No metadata selected") @@ -184,7 +188,8 @@ while (command.lower() not in ["exit", "quit", "end"]): print("Error: No file on path " + path) continue print("Loading metadata to memory from " + path) - metadata = sorted(json.load(open(metadata_file, "r")), key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)')) + metadata = sorted(json.load(open(metadata_file, "r")), + key=lambda k: datetime.strptime(k['datetime'], '%d-%b-%Y (%H:%M:%S.%f)')) index = 0 if command.lower() == "imagefolder": @@ -194,13 +199,13 @@ while (command.lower() not in ["exit", "quit", "end"]): path = attributes[0] if not os.path.isdir(path): print("Error: Path is no valid directory " + path) - continue + continue print("New image folder selected " + path) if command.lower() == "show": if metadata is None: print("No metadata loaded") - continue + continue if image_folder is None: print("No image folder selected") continue @@ -211,7 +216,7 @@ while (command.lower() not in ["exit", "quit", "end"]): if command.lower() == "trigger": if metadata is None: print("No metadata loaded") - continue + continue if image_folder is None: print("No image folder selected") continue @@ -232,7 +237,6 @@ while (command.lower() not in ["exit", "quit", "end"]): get_image(filename[:-4]) index = index + 1 - if command.lower() == "fetchall": if metadata is None: print("No metadata loaded") @@ -269,7 +273,7 @@ while (command.lower() not in ["exit", "quit", "end"]): print("Error: amount is no number") continue amount = int(attributes[0]) - for i in range(0,amount): + for i in range(0, amount): meta_payload = metadata[index] filename = meta_payload['filename'] send_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload) @@ -310,7 +314,6 @@ while (command.lower() not in ["exit", "quit", "end"]): continue index = ind - if command.lower() == "select": if metadata is None: print("Please first select metadata") @@ -329,4 +332,4 @@ while (command.lower() not in ["exit", "quit", "end"]): if not found: print("Could not find image by identifier " + identifier) -quit("Bye") \ No newline at end of file +quit("Bye")