diff options
Diffstat (limited to 'src/pyssg/configuration.py')
-rw-r--r-- | src/pyssg/configuration.py | 151 |
1 files changed, 30 insertions, 121 deletions
diff --git a/src/pyssg/configuration.py b/src/pyssg/configuration.py index 5a09a7a..dd6bfaa 100644 --- a/src/pyssg/configuration.py +++ b/src/pyssg/configuration.py @@ -1,133 +1,42 @@ -from typing import Union +import sys from importlib.metadata import version +from importlib.resources import path as rpath from datetime import datetime, timezone +from configparser import ConfigParser -class Configuration: - def __init__(self, path: str): - self.path: str = path - # config file specific - self.src: str = None - self.dst: str = None - self.plt: str = None - self.url: str = None - self.static_url: str = None - self.default_image_url: str = None - self.title: str = None - self.dformat: str = None - self.l_dformat: str = None - self.lsep_dformat: str = None - self.force: bool = None +DEFAULT_CONFIG_PATH = '$XDG_CONFIG_HOME/pyssg/config.ini' +VERSION = version('pyssg') - # other - self.version: str = version('pyssg') - self.dformat_rss: str = '%a, %d %b %Y %H:%M:%S GMT' - self.dformat_sitemap: str = '%Y-%m-%d' - self.run_date_rss = datetime.now(tz=timezone.utc).strftime(self.dformat_rss) - self.run_date_sitemap = \ - datetime.now(tz=timezone.utc).strftime(self.dformat_sitemap) +def __check_well_formed_config(config: ConfigParser) -> None: + default_config: ConfigParser = ConfigParser() + with rpath('pyssg.plt', 'default.ini') as p: + default_config.read(p) - def read(self): - try: - lines: list[str] = None - with open(self.path, 'r') as f: - lines = f.readlines() + for section in default_config.sections(): + if not config.has_section(section): + print(f'config does not have section "{section}"') + sys.exit(1) + for option in default_config.options(section): + if not config.has_option(section, option): + print(f'config does not have option "{option}" in section "{section}"') + sys.exit(1) - opts: dict[str, Union[str, bool]] = dict() - for l in lines: - kv: list[str] = l.split('=', 1) - if len(kv) != 2: - raise Exception('wrong config syntax') - k: str = kv[0].strip().lower() - v_temp: str = kv[1].strip() - # check if value should be a boolean true - v: Union[str, bool] = v_temp\ - if v_temp.lower() not in ['true', '1', 'yes']\ - else True +def get_parsed_config(path: str) -> ConfigParser: + config: ConfigParser = ConfigParser() + config.read(path) - opts[k] = v + __check_well_formed_config(config) - try: - self.src = opts['src'] - except KeyError: pass + # set other required options + config.set('fmt', 'rss_date', '%%a, %%d %%b %%Y %%H:%%M:%%S GMT') + config.set('fmt', 'sitemap_date', '%%Y-%%m-%%d') + config.set('info', 'version', VERSION) + config.set('info', 'rss_run_date', datetime.now( + tz=timezone.utc).strftime(config.get('fmt', 'rss_date'))) + config.set('info', 'sitemap_run_date', datetime.now( + tz=timezone.utc).strftime(config.get('fmt', 'sitemap_date'))) - try: - self.dst = opts['dst'] - except KeyError: pass - - try: - self.plt = opts['plt'] - except KeyError: pass - - try: - self.url = opts['url'] - except KeyError: pass - - try: - self.static_url = opts['static_url'] - except KeyError: pass - - try: - self.default_image_url = opts['default_image_url'] - except KeyError: pass - - try: - self.title = opts['title'] - except KeyError: pass - - try: - self.dformat = opts['date_format'] - except KeyError: pass - - try: - self.l_dformat = opts['list_date_format'] - except KeyError: pass - - try: - self.lsep_dformat = opts['list_sep_date_format'] - except KeyError: pass - - try: - # if the parser above didn't read a boolean true, then take it - # as a false anyways - self.force = opts['force'] if opts['force'] is True else False - except KeyError: pass - - except OSError: pass - - - def fill_missing(self, opts: dict[str, Union[str, bool]]) -> None: - if self.src is None: - self.src = opts['src'] - - if self.dst is None: - self.dst = opts['dst'] - - if self.plt is None: - self.plt = opts['plt'] - - if self.url is None: - self.url = opts['url'] - - if self.static_url is None: - self.static_url = opts['static_url'] - - if self.default_image_url is None: - self.default_image_url = opts['default_image_url'] - - if self.title is None: - self.title = opts['title'] - - if self.dformat is None: - self.dformat = opts['date_format'] - - if self.l_dformat is None: - self.l_dformat = opts['list_date_format'] - - if self.lsep_dformat is None: - self.lsep_dformat = opts['list_sep_date_format'] - - if self.force is None: - self.force = opts['force'] + return config |