Basic options and imports setup

This commit is contained in:
hygienic-books 2022-03-21 02:48:01 +01:00
parent 2d4d6d8ae5
commit 2ec5e8b29b
4 changed files with 79 additions and 0 deletions

3
config.ini Normal file
View File

@ -0,0 +1,3 @@
[maus]
watch_dir = /var/lib/kodi-nfo-feeder/watch
output_dir = /var/lib/kodi-nfo-feeder/output

63
kodi-nfo-feeder.py Normal file
View File

@ -0,0 +1,63 @@
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)

1
requirements.in Normal file
View File

@ -0,0 +1 @@
rich

12
requirements.txt Normal file
View File

@ -0,0 +1,12 @@
#
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile
#
commonmark==0.9.1
# via rich
pygments==2.11.2
# via rich
rich==12.0.0
# via -r requirements.in