Some testing with raise -calls.

This commit is contained in:
kalzu 2023-01-01 22:44:20 +02:00
parent 21f0d0c229
commit 52d380a4ee
1 changed files with 29 additions and 5 deletions

View File

@ -33,7 +33,6 @@ server_public_key = server_private_key.get_verifying_key()
# We need the hexadecimal form for sharing over http/json
server_public_key_hex = binascii.hexlify(server_public_key.to_string()).decode("utf-8")
database_lock = threading.Lock()
# Empty response json
empty_dict = {
@ -51,10 +50,32 @@ empty_json = json.dumps(empty_dict)
def read_keys():
with open(KEYSFILE, "r") as cfile:
user_keys = json.load(cfile)
return user_keys["user_publickeys"]
"""
Reads the declared KEYSFILE and returns "user_publickeys" from it.
Returns: json object
"""
# test if the file is there, we could make one if there is non
try:
# ascii is subset of UTF-8, this should be quite safe...
with open(KEYSFILE, "r", encoding='utf-8') as cfile:
user_keys = json.load(cfile)
if 'user_publickeys' not in user_keys:
raise ValueError('Missing required property "user_publickeys" in config file')
return user_keys["user_publickeys"]
except FileNotFoundError:
empty_userkeys = {
"user_publickeys": {
"user_name": "user_ecdsa_public key"
}
}
empty_userkeys_json = json.dumps(empty_userkeys)
with open(KEYSFILE, "w", encoding='utf-8') as nfile:
print('You seem be lacking the users key file, created example for you. Exiting')
json.dump(empty_userkeys_json, nfile)
raise EmptyUsersFile('Error')
return {}
def check_database():
"""
@ -398,9 +419,12 @@ def give_serverkey():
if __name__ == "__main__":
# Make sanity checks for the database
check_database()
database_lock = threading.Lock()
# Get the users public keys
user_publickeys = read_keys()
try:
user_publickeys = read_keys()
# Start the data fetching backend process§
fetch_thread = threading.Thread(target=get_the_data)