madblog/madblog/config.py

62 lines
1.6 KiB
Python
Raw Normal View History

2022-01-11 20:16:27 +01:00
import os
2023-05-01 23:18:46 +02:00
from typing import List
2022-01-11 20:16:27 +01:00
import yaml
2023-05-01 23:18:46 +02:00
from dataclasses import dataclass, field
2022-01-11 20:16:27 +01:00
@dataclass
class Config:
2023-05-01 23:18:46 +02:00
title = "Blog"
description = ""
link = "/"
home_link = "/"
language = "en-US"
logo = "/img/icon.png"
header = True
2023-05-01 23:18:46 +02:00
content_dir = "."
categories: List[str] = field(default_factory=list)
short_feed = False
2022-01-11 20:16:27 +01:00
basedir = os.path.abspath(os.path.dirname(__file__))
2023-05-01 23:18:46 +02:00
templates_dir = os.path.join(basedir, "templates")
static_dir = os.path.join(basedir, "static")
default_css_dir = os.path.join(static_dir, "css")
default_js_dir = os.path.join(static_dir, "js")
default_fonts_dir = os.path.join(static_dir, "fonts")
default_img_dir = os.path.join(static_dir, "img")
2022-01-11 20:16:27 +01:00
config = Config()
2023-05-01 23:18:46 +02:00
def init_config(content_dir=".", config_file="config.yaml"):
2022-01-11 20:16:27 +01:00
cfg = {}
config.content_dir = content_dir
if os.path.isfile(config_file):
2023-05-01 23:18:46 +02:00
with open(config_file, "r") as f:
2022-01-11 20:16:27 +01:00
cfg = yaml.safe_load(f)
2023-05-01 23:18:46 +02:00
if cfg.get("title"):
config.title = cfg["title"]
if cfg.get("description"):
config.description = cfg["description"]
if cfg.get("link"):
config.link = cfg["link"]
if cfg.get("home_link"):
config.home_link = cfg["home_link"]
if cfg.get("logo") is not None:
config.logo = cfg["logo"]
if cfg.get("language"):
config.language = cfg["language"]
if cfg.get("header") is False:
config.header = False
2023-05-01 23:18:46 +02:00
if cfg.get("short_feed"):
config.short_feed = True
2022-01-11 20:16:27 +01:00
2023-05-01 23:18:46 +02:00
config.categories = cfg.get("categories", [])
2022-01-11 20:16:27 +01:00
# vim:sw=4:ts=4:et: