From c02ea2849ac8f1567594c61a9546329754e1e014 Mon Sep 17 00:00:00 2001 From: kalzu Date: Sun, 1 Jan 2023 23:25:02 +0200 Subject: [PATCH] Clearer error handling on read_keys(). --- btc_tracker/kraken_fetch.py | 26 +++++++++++++------------- btc_tracker/userkeys.json | 4 ++-- btc_tracker/userkeys.json_bak | 5 ----- 3 files changed, 15 insertions(+), 20 deletions(-) delete mode 100644 btc_tracker/userkeys.json_bak diff --git a/btc_tracker/kraken_fetch.py b/btc_tracker/kraken_fetch.py index ab192be..a4e2652 100755 --- a/btc_tracker/kraken_fetch.py +++ b/btc_tracker/kraken_fetch.py @@ -11,6 +11,7 @@ import math import json import os import time +import sys import sqlite3 import binascii import threading @@ -54,6 +55,11 @@ def read_keys(): Reads the declared KEYSFILE and returns "user_publickeys" from it. Returns: json object """ + empty_userkeys = { + "user_publickeys": { + "user_name": "user_ecdsa_public key" + } + } # 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... @@ -61,20 +67,18 @@ def read_keys(): user_keys = json.load(cfile) if 'user_publickeys' not in user_keys: - raise ValueError('Missing required property "user_publickeys" in config file') + print('Missing required property "user_publickeys" in config file') + sys.exit(1) + if user_keys == empty_userkeys: + print('Your userkeys.json file seems to not filled. Please insert your key there.') + sys.exit(1) return user_keys["user_publickeys"] except FileNotFoundError: - empty_userkeys = { - "user_publickeys": { - "user_name": "user_ecdsa_public key" - } - } 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, nfile, indent=2) - raise FileNotFoundError - return {} + sys.exit(1) def check_database(): """ @@ -421,11 +425,7 @@ if __name__ == "__main__": database_lock = threading.Lock() # Get the users public keys - try: - user_publickeys = read_keys() - except FileNotFoundError: - exit(1) - + user_publickeys = read_keys() # Start the data fetching backend processĀ§ fetch_thread = threading.Thread(target=get_the_data) diff --git a/btc_tracker/userkeys.json b/btc_tracker/userkeys.json index 6683f22..86add59 100644 --- a/btc_tracker/userkeys.json +++ b/btc_tracker/userkeys.json @@ -1,5 +1,5 @@ { "user_publickeys": { - "user_name": "user_ecdsa_public key" + "user1": "f1debc13fb21fe0eee54525aa4f8aae5733b201c755edaa55f8893c90aa375b261a62eaa3110651ac5d7705d402581256a37508b0a1ca28bd919ea44710d9c88" } -} \ No newline at end of file +} diff --git a/btc_tracker/userkeys.json_bak b/btc_tracker/userkeys.json_bak deleted file mode 100644 index 86add59..0000000 --- a/btc_tracker/userkeys.json_bak +++ /dev/null @@ -1,5 +0,0 @@ -{ - "user_publickeys": { - "user1": "f1debc13fb21fe0eee54525aa4f8aae5733b201c755edaa55f8893c90aa375b261a62eaa3110651ac5d7705d402581256a37508b0a1ca28bd919ea44710d9c88" - } -}