improve error handling

This commit is contained in:
Matthias@Dell 2023-10-21 20:41:20 +02:00
parent 59159e4ad2
commit 5960e8b79a

View File

@ -2,8 +2,9 @@ from os import path, getcwd, listdir, mkdir, makedirs, rename
import re import re
def read_config(filepath): def read_config(filepath):
if not path.isfile(filepath): return False if not path.isfile(filepath):
raise FileNotFoundError(f"read_config: Invalid filepath {filepath}")
file = open(filepath, 'r') file = open(filepath, 'r')
keys = {} keys = {}
for line in file.readlines(): for line in file.readlines():
@ -26,13 +27,12 @@ def create_config():
""" """
=================================================================================================== ===================================================================================================
Creating a new config Creating a new config
Please enter at least one key and one directory.
The key must be one single letter, a single digit number or some other keyboard key like .-#+&/ ...
The key can not be 'q', 's', 'o' or 'u'.
The directory must be a valid path to a directory, but is does not have to exist.
You can use an absolute path (starting with '/', not '~') or a relative path (from here).
=================================================================================================== ===================================================================================================
Please enter at least one key and one directory.
The key must be one single letter, a single digit number or some other keyboard key like .-#+&/ ...
The key can not be 'q', 's' or 'u'.
The directory must be a valid path to a directory, but is does not have to exist.
You can use an absolute path (starting with '/') or a relative path (from here).
Do not use '~'!
""" """
) )
@ -77,7 +77,7 @@ def select_config():
config_path = path.expanduser("~") + "/.config/imgsort" config_path = path.expanduser("~") + "/.config/imgsort"
if not path.isdir(config_path) or len(listdir(config_path)) == 0: if not path.isdir(config_path) or len(listdir(config_path)) == 0:
return False return False
configs = {} configs = {}
i = 1 i = 1
@ -93,9 +93,9 @@ def select_config():
print(f"{n}: {conf}") print(f"{n}: {conf}")
choice = input("Please select a config: ") choice = input("Please select a config: ")
if choice == "0": return False if choice == "0": return None
elif choice in configs: elif choice in configs:
return path.normpath(config_path + "/" + configs[choice]) return path.normpath(config_path + "/" + configs[choice])
else: else:
print("Invalid choice - creating new config") print("Invalid choice - creating new config")
return False return None