This commit is contained in:
alexander
2026-01-18 23:36:55 +01:00
parent b16009f62a
commit 3eac461f7a
16 changed files with 506 additions and 7 deletions

Binary file not shown.

54
src/build.py Normal file
View File

@@ -0,0 +1,54 @@
from jinja2 import Environment, FileSystemLoader, select_autoescape
from inventory import load_cheatsheet_inventory, download_cheatsheet, CheatsheetItem
import shutil
import datetime
INVENTORY_FILE = "cheatsheet_inventory.json"
STATIC_DIR = "static"
TEMPLATES_DIR = "templates"
OUTPUT_DIR = "out"
inv_raw = load_cheatsheet_inventory(INVENTORY_FILE)
inv: list[CheatsheetItem] = []
# Clear output directory
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
shutil.copytree(STATIC_DIR, OUTPUT_DIR)
for i in inv_raw.items:
a = None
if i.cache:
print("Downloading", i.url)
url = download_cheatsheet(i, OUTPUT_DIR)
if url is not None:
i.url = url
a = i
else:
a = i
if a is not None:
inv.append(a)
env = Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
autoescape=select_autoescape()
)
index = env.get_template("index.html.j2")
for i in inv:
print("-", i)
thisYear = datetime.datetime.now().year
with open(f"{OUTPUT_DIR}/index.html", "w", encoding="utf-8") as f:
f.write(index.render(items=inv, thisYear=thisYear))
with open(f"{OUTPUT_DIR}/impressum.html", "w", encoding="utf-8") as f:
f.write(env.get_template("impressum.html.j2").render(thisYear=thisYear))
with open(f"{OUTPUT_DIR}/license.html", "w", encoding="utf-8") as f:
f.write(env.get_template("license.html.j2").render(thisYear=thisYear))

58
src/inventory.py Normal file
View File

@@ -0,0 +1,58 @@
from pydantic import BaseModel, Field
from uuid import uuid4
import os
import hashlib
import requests
class CheatsheetItem(BaseModel):
url: str
cache: bool
id: str = Field(default_factory=lambda: uuid4().__str__())
title: str
author: str | None
git_commit: str | None
git_repo: str | None
git_repo_type: str
date: str
class CheatSheetExternalInfo(BaseModel):
tilte: str
author: str | None
class CheatsheetInvertoryConfig(BaseModel):
items: list[CheatsheetItem]
def load_cheatsheet_inventory(file: str) -> CheatsheetInvertoryConfig:
if not os.path.exists(file):
res = CheatsheetInvertoryConfig(items=[])
else:
res = CheatsheetInvertoryConfig.model_validate_json(
open(file, "r", encoding="utf-8").read()
)
with open(file, "w", encoding="utf-8") as f:
f.write(res.model_dump_json(indent=4))
return res
def download_cheatsheet(inv: CheatsheetItem, outdir: str) -> str | None:
r = requests.get(inv.url)
if not r.ok and r.headers.get("Content-Type") != "application/pdf":
return None
data = r.content
hashdata = hashlib.sha256(data)
filesname = os.path.join("cache", f"{hashdata.hexdigest()}.pdf")
if not os.path.exists(os.path.join(outdir, "cache")):
os.mkdir(os.path.join(outdir, "cache"))
with open(os.path.join(outdir, filesname), "wb") as f:
f.write(data)
print("Saved file to", filesname)
return filesname