summaryrefslogtreecommitdiff
path: root/src/pyssg/configuration.py
blob: 5a09a7af378232454ff5e319735ccd5021ab9eaf (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from typing import Union
from importlib.metadata import version
from datetime import datetime, timezone


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

        # 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 read(self):
        try:
            lines: list[str] = None
            with open(self.path, 'r') as f:
                lines = f.readlines()

            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

                opts[k] = v

            try:
                self.src = opts['src']
            except KeyError: pass

            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']