configuration.py 1.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
import os
import yaml
import uuid

from boop.environment import Environment


def load(configuration_path, environment, output_path):
    configuration = {
        "SITE_URL": "filesystem",
        "SITE_TITLE": "A website",
        "SITE_TIMEZONE": "UTC",
berumuron's avatar
berumuron committed
13
        "SITE_BLOG_SLUG": "blog",
14
        "SITE_ENVIRONMENT": environment.name.lower(),
berumuron's avatar
berumuron committed
15
        "SITE_WEBSUB_HUB": "",
16
17
18
19
    }

    if os.path.exists(configuration_path):
        with open(configuration_path) as conf_file:
berumuron's avatar
berumuron committed
20
            conf_data = yaml.safe_load(conf_file)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        for key, value in conf_data.items():
            if key == "development":
                continue
            site_key = f"SITE_{key.upper()}"
            configuration[site_key] = value

        if environment == Environment.DEVELOPMENT and "development" in conf_data:
            for key, value in conf_data["development"].items():
                site_key = f"SITE_{key.upper()}"
                configuration[site_key] = value

    configuration["SITE_UUID"] = str(
        uuid.uuid5(uuid.NAMESPACE_URL, configuration["SITE_URL"])
    )
    if "SITE_AUTHOR" not in configuration:
        configuration["SITE_AUTHOR"] = f"{configuration['SITE_TITLE']}'s author"
    if configuration["SITE_URL"] == "filesystem":
        configuration["SITE_URL"] = f"file://{os.path.abspath(output_path)}/"

    return configuration