64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import os
|
|
import logging
|
|
from rich.logging import RichHandler
|
|
from rich.traceback import install
|
|
import configparser
|
|
|
|
|
|
class CONST(object):
|
|
__slots__ = ()
|
|
LOG_FORMAT = "%(message)s"
|
|
CFG_THIS_FILE_DIRNAME = os.path.dirname(__file__)
|
|
CFG_DEFAULT_FILENAME = "config.ini"
|
|
CFG_DEFAULT_ABS_PATH = os.path.join(CFG_THIS_FILE_DIRNAME, CFG_DEFAULT_FILENAME)
|
|
CFG_KNOWN_DEFAULTS = [
|
|
{"key": "self_name", "value": "kodi-nfo-feeder"},
|
|
{"key": "watch_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s/watch")},
|
|
{"key": "output_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s/output")}
|
|
]
|
|
CFG_KNOWN_SECTION = [
|
|
{"key": "watch_dir", "is_mandatory": True},
|
|
{"key": "output_dir", "is_mandatory": True}
|
|
]
|
|
CFG_MANDATORY = [section_cfg["key"] for section_cfg in CFG_KNOWN_SECTION if section_cfg["is_mandatory"]]
|
|
|
|
|
|
CONST = CONST()
|
|
logging.basicConfig(
|
|
# Default for all modules in NOTSET so log everything
|
|
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")
|
|
# Our own code logs with this level
|
|
log.setLevel(logging.DEBUG)
|
|
install(show_locals=True)
|
|
|
|
|
|
class ConfigParser(
|
|
configparser.ConfigParser):
|
|
"""Can get options() without defaults
|
|
|
|
Taken from https://stackoverflow.com/a/12600066.
|
|
"""
|
|
|
|
def options(self, section, no_defaults=False, **kwargs):
|
|
if no_defaults:
|
|
try:
|
|
return list(self._sections[section].keys())
|
|
except KeyError:
|
|
raise configparser.NoSectionError(section)
|
|
else:
|
|
return super().options(section)
|
|
|
|
|
|
ini_defaults = []
|
|
internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS}
|
|
config = ConfigParser(defaults=internal_defaults)
|
|
config.read(CONST.CFG_DEFAULT_FILENAME)
|