Replace JSON config with .env files
- Add python-dotenv dependency for environment variable loading - Replace params.json and params-local.json with .env files - Add --local and --local-stacosys flags to makesite.py - Update Makefile to auto-detect stacosys availability - Document configuration and usage in README.md
This commit is contained in:
parent
199911aaae
commit
3b7484fbe6
9 changed files with 94 additions and 44 deletions
56
makesite.py
56
makesite.py
|
|
@ -2,19 +2,15 @@
|
|||
|
||||
"""Make static website/blog with Python."""
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import locale
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import time
|
||||
import unicodedata
|
||||
from email import utils
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import requests
|
||||
import mistune
|
||||
from pygments import highlight
|
||||
|
|
@ -701,28 +697,36 @@ def generate_sitemap(posts, params):
|
|||
)
|
||||
|
||||
|
||||
def get_params(param_file):
|
||||
"""Load site parameters from JSON file with defaults.
|
||||
def get_params(env_file=None):
|
||||
"""Load site parameters from .env files.
|
||||
|
||||
Args:
|
||||
param_file: Path to JSON parameters file
|
||||
env_file: Optional .env file to load and override .env values
|
||||
|
||||
Returns:
|
||||
dict: Site parameters with defaults and loaded values
|
||||
"""
|
||||
# Default parameters.
|
||||
# Load .env file first
|
||||
load_dotenv(".env")
|
||||
|
||||
# Load override file if specified
|
||||
if env_file:
|
||||
load_dotenv(env_file, override=True)
|
||||
log("use params from " + env_file)
|
||||
else:
|
||||
log("use params from .env")
|
||||
|
||||
# Build params from environment variables
|
||||
params = {
|
||||
"title": "Blog",
|
||||
"subtitle": "Lorem Ipsum",
|
||||
"author": "Admin",
|
||||
"site_url": "http://localhost:8000",
|
||||
"title": os.getenv("TITLE", "Blog"),
|
||||
"subtitle": os.getenv("SUBTITLE", "Lorem Ipsum"),
|
||||
"author": os.getenv("AUTHOR", "Admin"),
|
||||
"site_url": os.getenv("SITE_URL", "http://localhost:8000"),
|
||||
"current_year": datetime.datetime.now().year,
|
||||
"stacosys_url": "",
|
||||
"stacosys_url": os.getenv("STACOSYS_URL", ""),
|
||||
"external_check": os.getenv("EXTERNAL_CHECK", ""),
|
||||
}
|
||||
|
||||
log("use params from " + param_file)
|
||||
if os.path.isfile(param_file):
|
||||
params.update(json.loads(fread(param_file)))
|
||||
return params
|
||||
|
||||
|
||||
|
|
@ -733,14 +737,14 @@ def rebuild_site_directory():
|
|||
shutil.copytree("static", "_site")
|
||||
|
||||
|
||||
def main(param_file):
|
||||
def main(env_file=None):
|
||||
"""Main entry point for static site generation.
|
||||
|
||||
Args:
|
||||
param_file: Path to JSON parameters file
|
||||
env_file: Optional .env file to override .env values
|
||||
"""
|
||||
|
||||
params = get_params(param_file)
|
||||
params = get_params(env_file)
|
||||
|
||||
# Create a new _site directory from scratch.
|
||||
rebuild_site_directory()
|
||||
|
|
@ -769,8 +773,10 @@ def main(param_file):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description='Makesite')
|
||||
parser.add_argument('--params', dest='param_file', type=str,
|
||||
default="params.json", help='Custom param file')
|
||||
args = parser.parse_args()
|
||||
main(args.param_file)
|
||||
# Determine which env file to use
|
||||
env_file = None
|
||||
if "--local-stacosys" in sys.argv:
|
||||
env_file = ".env.local-stacosys"
|
||||
elif "--local" in sys.argv:
|
||||
env_file = ".env.local"
|
||||
main(env_file)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue