103 lines
3.1 KiB
Python
103 lines
3.1 KiB
Python
import configparser
|
|
import logging
|
|
import os
|
|
import sys
|
|
from rich.logging import RichHandler
|
|
|
|
|
|
# Exit codes
|
|
# 1: Config file invalid, it has no sections
|
|
# 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY
|
|
|
|
|
|
class CONST(object):
|
|
__slots__ = ()
|
|
LOG_FORMAT = "%(message)s"
|
|
CFG_DEFAULT_FILENAME = "config.ini"
|
|
CFG_DEFAULT_ABS_PATH = os.path.join(os.getcwd(), CFG_DEFAULT_FILENAME)
|
|
CFG_MANDATORY = "query"
|
|
|
|
|
|
CONST = CONST()
|
|
logging.basicConfig(
|
|
level="NOTSET",
|
|
format=CONST.LOG_FORMAT,
|
|
datefmt="[%X]",
|
|
handlers=[RichHandler(
|
|
show_time=False if "SYSTEMD_EXEC_PID" in os.environ else True,
|
|
rich_tracebacks=True
|
|
)]
|
|
)
|
|
log = logging.getLogger("rich")
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(CONST.CFG_DEFAULT_FILENAME)
|
|
|
|
|
|
def validate_config_defaults(config_obj: configparser.ConfigParser()) -> None:
|
|
log.debug(f"Loading config from file '{CONST.CFG_DEFAULT_ABS_PATH}' ...")
|
|
if not config_obj.sections():
|
|
log.error(f"No config sections found in '{CONST.CFG_DEFAULT_ABS_PATH}'. Exiting 1 ...")
|
|
sys.exit(1)
|
|
if config.defaults():
|
|
for default in config_obj.defaults():
|
|
log.debug(f" {default} = {config_obj[config_obj.default_section][default]}")
|
|
else:
|
|
log.debug(f"No defaults defined")
|
|
|
|
|
|
def config_has_valid_section(config_obj: configparser.ConfigParser()) -> bool:
|
|
has_valid_section = False
|
|
for config_obj_section in config_obj.sections():
|
|
if CONST.CFG_MANDATORY in config_obj.options(config_obj_section):
|
|
has_valid_section = True
|
|
break
|
|
return has_valid_section
|
|
|
|
|
|
#def option_has_default_value(config_option: configparser.ConfigParser(), section: str) -> bool:
|
|
#
|
|
#~ local option that doesn't exist in defaults section
|
|
#+ local option that overrides a default
|
|
#* global option
|
|
|
|
def validate_config_sections(config_obj: configparser.ConfigParser()) -> None:
|
|
for section in config_obj.sections():
|
|
log.debug(f"Loading config '[{section}]' ...")
|
|
for key in config_obj[section]:
|
|
# log.debug(config_obj.has_option(section, key))
|
|
is_local_option = True if config_obj.has_option(section, key) else False
|
|
log.debug(f""" {key}{"/" if is_local_option else ".."} = {config_obj[section][key]}""")
|
|
|
|
|
|
validate_config_defaults(config)
|
|
if config_has_valid_section(config):
|
|
validate_config_sections(config)
|
|
else:
|
|
log.error(f"No valid config section found. A valid config section has at least the '{CONST.CFG_MANDATORY}' "
|
|
f"option set. Exiting 2 ...")
|
|
sys.exit(2)
|
|
|
|
|
|
# https://stackoverflow.com/a/2451872
|
|
quit()
|
|
|
|
# This is a sample Python script.
|
|
|
|
# Press Umschalt+F10 to execute it or replace it with your code.
|
|
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
|
|
|
|
|
def print_hi(name):
|
|
# Use a breakpoint in the code line below to debug your script.
|
|
print(f'Hi, {name}') # Press Strg+F8 to toggle the breakpoint.
|
|
|
|
|
|
# Press the green button in the gutter to run the script.
|
|
if __name__ == '__main__':
|
|
print_hi('PyCharm')
|
|
|
|
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|