aboutsummaryrefslogtreecommitdiff
path: root/sjdbmk/pack.py
blob: 902e256d4340be7e1ba60d2082f24c5e59cc82e4 (plain) (blame)
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
#!/usr/bin/env python3
#
# Daily script to pack the YK Pao School Daily Bulletin HTML from JSON data
# Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

from configparser import ConfigParser
import os
import json
import datetime
import argparse
import logging
import zoneinfo
from jinja2 import Template, StrictUndefined


def main(date: str, config: ConfigParser) -> None:

    with open(
        os.path.join(config["templates"]["directory"], config["templates"]["main"]),
        "r",
        encoding="utf-8",
    ) as template_file:
        template = Template(
            template_file.read(), undefined=StrictUndefined, autoescape=True
        )

    with open(
        os.path.join(
            config["general"]["build_path"], "day-" + date.replace("-", "") + ".json"
        ),
        "r",
        encoding="utf-8",
    ) as fd:
        data = json.load(fd)

    # extra_data = {
    # }
    #
    # data = data | extra_data

    template.stream(**data).dump(
        os.path.join(
            config["general"]["build_path"], "sjdb-%s.html" % date.replace("-", "")
        )
    )

    # FIXME: Escape the dangerous HTML!


if __name__ == "__main__":
    try:
        logging.basicConfig(level=logging.INFO)
        parser = argparse.ArgumentParser(description="Daily Bulletin Packer")
        parser.add_argument(
            "--date",
            default=None,
            help="the day to generate for, in local time, in YYYY-MM-DD; defaults to tomorrow",
            # TODO: Verify validity of date
            # TODO: Verify consistency of date elsewhere
        )
        parser.add_argument(
            "--config", default="config.ini", help="path to the configuration file"
        )
        args = parser.parse_args()
        config = ConfigParser()
        config.read(args.config)
        if args.date:
            date = args.date
        else:
            now = datetime.datetime.now(
                zoneinfo.ZoneInfo(config["general"]["timezone"])
            )
            date = (now + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        logging.info("Generating for day %s" % date)
        # main(date, config)
        main(date, config)
    except KeyboardInterrupt:
        logging.critical("KeyboardInterrupt")