Skip to content

Commit

Permalink
Update config_handler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
j54j6 committed May 12, 2024
1 parent 2b61496 commit e5570e6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#default config_name
default_config_name:str = "config.ini"

#This function is used to define a default config.
def create_default_config(
path=Path.joinpath(pathlib.Path(__file__).parent.resolve(), "config.ini")
):
Expand All @@ -47,6 +48,7 @@ def create_default_config(
logger.error("Error while creating default config! - Error: %s", e)
return False

#This function checks if a given path is a valid .ini file
def check_for_config(path=False):
#As fallback (per Default) the config is located in the same folder as the main.py.
#Set the default search path to the current file dir.
Expand All @@ -60,20 +62,22 @@ def check_for_config(path=False):
if path.lower().endwith(".ini"):
check_path = Path(path)
else:
logger.error("The given file %s does not end with \".ini\". Only INI Files are supported", path)
logger.error("""The given file %s does not end with \".ini\".
Only INI Files are supported""", path)
sys.exit()
except TypeError as e:
logger.error("Error while converting given configuration path to Path Object. Error: %s", e)
logger.error("""Error while converting given configuration path to Path Object.
Error: %s""", e)
sys.exit()

logger.info("Check for config file. Provided path: %s", path)

#Check if check_path exists on the filesystem
if not os.path.exists(check_path):
logger.error("Config file does not exist! - Create default config...")
config_created:bool = create_default_config()

if(not config_created):
if not config_created:
sys.exit()

#Config File exists - check if it is valid json (load file)
Expand Down

0 comments on commit e5570e6

Please sign in to comment.