feat: init setup reading ini config

This commit is contained in:
DaanSelen
2026-03-02 14:06:50 +01:00
parent 65aad6ae6b
commit 230b2b18fe
11 changed files with 187 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
#!/bin/env python3
import logging as log
from .utilities import preflight_checks
class reader():
def read_config() -> dict:
'''
check some basic things and then return the dict containing the config data
'''
ok, candidate_path = preflight_checks.search_known_paths()
if not ok:
return {}
ok, config_contents = preflight_checks.verify_contents(candidate_path)
if not ok:
return {}
return config_contents
def refresh_config(config_contents: dict) -> dict:
log.debug(f'refreshing config values')
return reader.read_config()
+67
View File
@@ -0,0 +1,67 @@
#!/bin/env python3
import logging as log
import configparser as cp
import os
class preflight_checks():
def search_known_paths() -> tuple[bool, str]:
'''
Look at predefined paths on the filesystem for a config file
'''
possible_config_locations = [
"./config.ini",
f"{os.getenv('HOME')}/.config/wgdashboard/config.ini",
"/etc/wgdashboard/config.ini",
]
try:
log.debug('searching predefined locations for the config file, takes first one.')
for path in possible_config_locations:
log.debug(f'testing path: {path}')
if os.path.exists(path):
log.debug(f'found a file at: {path}')
return True, path
else:
continue
return False, ''
except Exception as err:
log.error(f'error occured while searching for the config file: {err}')
return False, ''
def verify_contents(config_path: str) -> tuple[bool, dict]:
'''
Check the existing config file for contents
'''
config = cp.ConfigParser()
try:
log.debug('looking through the given config file')
config.read(config_path)
if len(config.sections()) == 0:
log.error('empty config, no sections')
return False, {}
for section in config.sections():
log.debug(f'checking integrity of section: {section}')
if len(config.items(section)) == 0:
log.error('empty section, no keys or values')
return False, {}
return True, dict(config.items())
except cp.ParsingError as err:
log.error(f'error parsing the ini config file: {err}')
return False, {}
except Exception as err:
log.error(f'error occured while looking through the config file: {err}')
return False, {}
+2
View File
@@ -0,0 +1,2 @@
#!/bin/env python3
+2
View File
@@ -0,0 +1,2 @@
#!/bin/env python3
+9
View File
@@ -0,0 +1,9 @@
#!/bin/env python3
class db_setup():
'''
This class functions as a collection of function to prepare a connection to a database.
'''
def compile_connection_string() -> string:
return "The Connection string"
+1
View File
@@ -0,0 +1 @@
#!/bin/env python3