summaryrefslogtreecommitdiff
path: root/src/pyssg/configuration.py
blob: 895df5ccac147d20722217269ce156547e797edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import yaml
import pprint
from importlib.metadata import version
from importlib.resources import path as rpath
from datetime import datetime, timezone
from configparser import ConfigParser
from logging import Logger, getLogger

from .utils import get_expanded_path

log: Logger = getLogger(__name__)


DEFAULT_CONFIG_PATH: str = '$XDG_CONFIG_HOME/pyssg/config.yaml'
VERSION = version('pyssg')


def __expand_all_paths(config: ConfigParser) -> None:
    log.debug('expanding all path options')
    for option in config.options('path'):
        path: str = config['path'][option]
        config.set('path', option, get_expanded_path(path))


def __check_well_formed_config(config: ConfigParser) -> None:
    log.debug('checking that config file is well formed')
    default_config: ConfigParser = ConfigParser()
    with rpath('pyssg.plt', 'default.ini') as p:
        log.debug('reading config file "%s"', p)
        default_config.read(p)

    for section in default_config.sections():
        log.debug('checking section "%s"', section)
        if not config.has_section(section):
            log.error('config does not have section "%s"', section)
            sys.exit(1)
        for option in default_config.options(section):
            log.debug('checking option "%s"', option)
            if not config.has_option(section, option):
                log.error('config does not have option "%s" in section "%s"', option, section)
                sys.exit(1)


def get_parsed_config(path: str) -> ConfigParser:
    config: ConfigParser = ConfigParser()
    log.debug('reading config file "%s"', path)
    config.read(path)

    __check_well_formed_config(config)
    __expand_all_paths(config)

    # set other required options
    log.debug('setting extra config 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['fmt']['rss_date']))
    config.set('info', 'sitemap_run_date', datetime.now(
        tz=timezone.utc).strftime(config['fmt']['sitemap_date']))

    return config