Added config

This commit is contained in:
alexander
2026-01-24 15:46:24 +01:00
parent d81cd88cab
commit fe20cb7bdc
7 changed files with 29 additions and 20 deletions

View File

@@ -9,12 +9,11 @@ Flask==3.1.2
h11==0.16.0
httpcore==1.0.9
httptools==0.7.1
httpx==0.28.1
httpx
idna==3.11
itsdangerous==2.2.0
janus==2.0.0
Jinja2==3.1.6
libsass==0.23.0
livereload==2.7.1
MarkupSafe==3.0.3
pydantic==2.12.5
@@ -22,7 +21,6 @@ pydantic-settings==2.12.0
pydantic_core==2.41.5
python-dotenv==1.2.1
PyYAML==6.0.3
requests==2.32.5
starlette==0.38.6
tornado==6.5.4
typing-inspection==0.4.2

View File

@@ -8,7 +8,7 @@ class PathsConfig(BaseSettings):
inventory_file: str = Field(default="cheatsheet_inventory.json", description="Cheatsheet inventory file")
templates: str = Field(default="templates", description="Templates directory")
static: str = Field(default="static", description="Static files directory")
output: str = Field(default="prod", description="Output directory")
output: str = Field(default="out", description="Output directory")
prod: str = Field(default="prod", description="Production directory")

View File

@@ -7,6 +7,8 @@ from sources.gitea import process_gitea
from logger import get_worker_thread_logger
def load_cheatsheet_inventory(file: str) -> CSInventoryConfig:
logger = get_worker_thread_logger()
logger.info(f"Loading cheatsheet inventory from {file}")
if not os.path.exists(file):
res = CSInventoryConfig(items=[])
else:

View File

@@ -39,7 +39,6 @@ def worker():
async def lifespan(app: FastAPI):
global build_queue, build_queue_sync, build_queue_async
settings = load_settings()
logger = getLogger("uvicorn").getChild("lifespan")
build_queue = janus.Queue()

View File

@@ -15,6 +15,7 @@ class CSSourceBase(BaseModel):
class CSSourceGitea(CSSourceBase):
type: Literal[CheatsheetSourceType.GITEA_SOURCE]
base_url: str
fetch_url: str | None = Field(default=None)
repo: str
owner: str
tag: str

View File

@@ -3,34 +3,41 @@ from sources import CSSourceGitea, CSItem, CSInventoryItem
from sources.util import cache_cheatsheet, get_datestring
import httpx
from pathlib import Path
from logger import get_worker_thread_logger
def process_gitea(item: CSInventoryItem, outdir: str) -> list[CSItem] | None:
logger = get_worker_thread_logger()
logger.info(f"Processing Gitea cheatsheet: {item.source.owner}/{item.source.repo}@{item.source.tag}")
source: CSSourceGitea = item.source
commit_hash = get_release_commit_sha(source.base_url, source.owner, source.repo, source.tag)
asserts = list_release_assets(source.base_url, source.owner, source.repo, source.tag)
commit_hash = get_release_commit_sha(source.fetch_url or source.base_url, source.owner, source.repo, source.tag)
assets = list_release_assets(source.fetch_url or source.base_url, source.owner, source.repo, source.tag)
asserts = list(filter(lambda a: a[1].endswith(".pdf"), asserts))
asserts = list(map(lambda a: (a[0], f"{source.base_url}/repos/{source.owner}/{source.repo}/releases/download/{source.tag}/{a[0]}"), asserts))
assets = list(filter(lambda a: a[1].endswith(".pdf"), assets))
print(assets)
assets_urls = list(map(lambda a: (
f"{source.fetch_url or source.base_url}/{source.owner}/{source.repo}/releases/download/{source.tag}/{a[1]}",
f"{source.base_url}/{source.owner}/{source.repo}/releases/download/{source.tag}/{a[1]}"
), assets),
)
print(f"Found {len(asserts)} PDF assets in Gitea release {source.owner}/{source.repo}@{source.tag}")
logger.info(f"Found {len(assets_urls)} PDF assets in Gitea release {source.owner}/{source.repo}@{source.tag}")
res = []
for a in asserts:
res_url = a[0]
for fetch_url, real_url in assets_urls:
if item.cache:
cache_url = cache_cheatsheet(a[0], outdir)
cache_url = cache_cheatsheet(fetch_url, outdir)
if cache_url:
res_url = cache_url
real_url = cache_url
else:
continue
name = Path(a[1]).stem
name = Path(real_url).stem
res.append(CSItem(
url = res_url,
url = real_url,
date=get_datestring(),
commit=commit_hash[:10] if commit_hash else "",
author=item.author if item.author else source.owner,

View File

@@ -2,6 +2,7 @@ import httpx
import datetime
import os
from pathlib import Path
from logger import get_worker_thread_logger
from urllib.parse import urlparse
def get_datestring() -> str:
@@ -9,16 +10,17 @@ def get_datestring() -> str:
def cache_cheatsheet(url, outdir: str) -> str | None:
print("Caching cheatsheet from", url)
logger = get_worker_thread_logger()
logger.info(f"Caching cheatsheet from {url}")
try:
with httpx.Client() as client:
r = client.get(url, timeout=5.0)
if not r.is_success and r.headers.get("Content-Type") != "application/pdf":
logger.error(f"Failed to fetch URL: {url} (status code: {r.status_code})")
return None
except httpx.TimeoutException:
print("Timeout fetching URL:", url)
logger.error(f"Timeout fetching URL: {url}")
return None
data = r.content
@@ -33,6 +35,6 @@ def cache_cheatsheet(url, outdir: str) -> str | None:
with open(os.path.join(outdir, filesname), "wb") as f:
f.write(data)
print("Saved file to", filesname)
logger.info(f"Saved file to {filesname}")
return filesname