mirror of
https://github.com/WGDashboard/WGDashboard-PRW.git
synced 2026-08-04 06:52:58 +00:00
feat: init setup reading ini config
This commit is contained in:
@@ -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()
|
||||
@@ -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, {}
|
||||
@@ -0,0 +1,2 @@
|
||||
#!/bin/env python3
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#!/bin/env python3
|
||||
|
||||
@@ -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"
|
||||
@@ -0,0 +1 @@
|
||||
#!/bin/env python3
|
||||
Reference in New Issue
Block a user