reformat code;

This commit is contained in:
Marco Zeisler 2020-11-30 18:58:04 +01:00
parent 8748611fa6
commit 4b6052f502

View File

@ -5,12 +5,15 @@ import json
import time import time
from datetime import datetime from datetime import datetime
def print_response(response): def print_response(response):
print("Code: " + str(response.status_code) + "; Body: " + str(response.json())) print("Code: " + str(response.status_code) + "; Body: " + str(response.json()))
def unwrap_file(file_str: str) -> bytes: def unwrap_file(file_str: str) -> bytes:
return base64.b64decode(file_str) return base64.b64decode(file_str)
def send_image(identifier, image_path, metadata_payload): def send_image(identifier, image_path, metadata_payload):
print("Sending image with identifier " + identifier) print("Sending image with identifier " + identifier)
if not os.path.isfile(image_path): if not os.path.isfile(image_path):
@ -41,10 +44,10 @@ def send_image(identifier, image_path, metadata_payload):
def get_image(identifier): def get_image(identifier):
print("Getting image with identifier "+identifier) print("Getting image with identifier " + identifier)
baseurl = "http://127.0.0.1:8000" baseurl = "http://127.0.0.1:8000"
get_url = "/image/get/"+identifier get_url = "/image/get/" + identifier
try: try:
response = requests.get(baseurl + get_url) response = requests.get(baseurl + get_url)
@ -53,9 +56,10 @@ def get_image(identifier):
print(payload['metadata']) print(payload['metadata'])
b64encoded_image = unwrap_file(payload['image_data']) b64encoded_image = unwrap_file(payload['image_data'])
goal_folder_name = "images_fetch" 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): if not os.path.isdir(goal_folder_path):
os.mkdir(goal_folder_path) os.mkdir(goal_folder_path)
@ -70,10 +74,10 @@ def get_image(identifier):
def delete_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" baseurl = "http://127.0.0.1:8000"
get_url = "/image/delete/"+identifier get_url = "/image/delete/" + identifier
try: try:
response = requests.delete(baseurl + get_url) response = requests.delete(baseurl + get_url)
@ -84,7 +88,6 @@ def delete_image(identifier):
return True return True
def get_all(): def get_all():
print("Getting all images") print("Getting all images")
@ -98,6 +101,7 @@ def get_all():
print("Error sending request") print("Error sending request")
return True return True
metadata = None metadata = None
index = 0 index = 0
metadata_folder = "." + os.path.sep metadata_folder = "." + os.path.sep
@ -106,22 +110,22 @@ metadata_path = metadata_folder + os.path.sep + metadata_file
image_folder = "." + os.path.sep + "images" image_folder = "." + os.path.sep + "images"
if (os.path.isfile(metadata_path)): if (os.path.isfile(metadata_path)):
print("Loading metadata to memory from " + 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: else:
print("Default metadata_file not found.") print("Default metadata_file not found.")
if (os.path.isdir(image_folder)): if (os.path.isdir(image_folder)):
print("Image folder set to " + image_folder) print("Image folder set to " + image_folder)
else: else:
print("Default image folder not found.") print("Default image folder not found.")
#main input loop # main input loop
command = "dummy" command = "dummy"
while (command.lower() not in ["exit", "quit", "end"]): while (command.lower() not in ["exit", "quit", "end"]):
try: try:
command = input("Please enter command: ") command = input("Please enter command: ")
except: except:
@ -154,19 +158,19 @@ while (command.lower() not in ["exit", "quit", "end"]):
print("select |<identifier> - select image with given <identifier> as next in line") print("select |<identifier> - select image with given <identifier> as next in line")
print() print()
print() print()
if command.lower() == "dir": if command.lower() == "dir":
print("Current image folder is " + image_folder) print("Current image folder is " + image_folder)
if command.lower() == "len": if command.lower() == "len":
if metadata is None: if metadata is None:
print("No metadata selected") print("No metadata selected")
else: else:
print("Length of metadata list " + str(len(metadata))) print("Length of metadata list " + str(len(metadata)))
if command.lower() == "index": if command.lower() == "index":
print("Current lineup index is " + str(index)) print("Current lineup index is " + str(index))
if command.lower() == "info": if command.lower() == "info":
if metadata is None: if metadata is None:
print("No metadata selected") print("No metadata selected")
@ -184,7 +188,8 @@ while (command.lower() not in ["exit", "quit", "end"]):
print("Error: No file on path " + path) print("Error: No file on path " + path)
continue continue
print("Loading metadata to memory from " + path) 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 index = 0
if command.lower() == "imagefolder": if command.lower() == "imagefolder":
@ -194,13 +199,13 @@ while (command.lower() not in ["exit", "quit", "end"]):
path = attributes[0] path = attributes[0]
if not os.path.isdir(path): if not os.path.isdir(path):
print("Error: Path is no valid directory " + path) print("Error: Path is no valid directory " + path)
continue continue
print("New image folder selected " + path) print("New image folder selected " + path)
if command.lower() == "show": if command.lower() == "show":
if metadata is None: if metadata is None:
print("No metadata loaded") print("No metadata loaded")
continue continue
if image_folder is None: if image_folder is None:
print("No image folder selected") print("No image folder selected")
continue continue
@ -211,7 +216,7 @@ while (command.lower() not in ["exit", "quit", "end"]):
if command.lower() == "trigger": if command.lower() == "trigger":
if metadata is None: if metadata is None:
print("No metadata loaded") print("No metadata loaded")
continue continue
if image_folder is None: if image_folder is None:
print("No image folder selected") print("No image folder selected")
continue continue
@ -232,7 +237,6 @@ while (command.lower() not in ["exit", "quit", "end"]):
get_image(filename[:-4]) get_image(filename[:-4])
index = index + 1 index = index + 1
if command.lower() == "fetchall": if command.lower() == "fetchall":
if metadata is None: if metadata is None:
print("No metadata loaded") print("No metadata loaded")
@ -269,7 +273,7 @@ while (command.lower() not in ["exit", "quit", "end"]):
print("Error: amount is no number") print("Error: amount is no number")
continue continue
amount = int(attributes[0]) amount = int(attributes[0])
for i in range(0,amount): for i in range(0, amount):
meta_payload = metadata[index] meta_payload = metadata[index]
filename = meta_payload['filename'] filename = meta_payload['filename']
send_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload) 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 continue
index = ind index = ind
if command.lower() == "select": if command.lower() == "select":
if metadata is None: if metadata is None:
print("Please first select metadata") print("Please first select metadata")
@ -329,4 +332,4 @@ while (command.lower() not in ["exit", "quit", "end"]):
if not found: if not found:
print("Could not find image by identifier " + identifier) print("Could not find image by identifier " + identifier)
quit("Bye") quit("Bye")