SPB Git forge

spb/wp22_uqo

Public
6commits 1branches 0releases
142.4 MBsize
maindefault branch
2 h agolast push
TeX 55.1% Python 44.2%
1.2 KB · 41 lines python
Raw Blame History
1# Author: Simon-Pierre Boucher — simon-pierre.boucher@uqo.ca2"""I/O helpers: read-only SQLite access, parquet layers, manifests."""3from __future__ import annotations45import hashlib6import json7import sqlite38from datetime import datetime, timezone9from pathlib import Path1011import polars as pl121314def sha256_file(path: Path, chunk: int = 1 << 24) -> str:15    h = hashlib.sha256()16    with open(path, "rb") as f:17        while True:18            b = f.read(chunk)19            if not b:20                break21            h.update(b)22    return h.hexdigest()232425def sqlite_ro(path: Path) -> sqlite3.Connection:26    """Open a SQLite file strictly read-only (URI mode)."""27    return sqlite3.connect(f"file:{path}?mode=ro", uri=True)282930def query(con: sqlite3.Connection, sql: str) -> pl.DataFrame:31    return pl.read_database(sql, con, infer_schema_length=None)323334def write_manifest(path: Path, payload: dict) -> None:35    payload = {"written_utc": datetime.now(timezone.utc).isoformat(timespec="seconds"), **payload}36    path.write_text(json.dumps(payload, indent=2, ensure_ascii=False, default=str))373839def read_manifest(path: Path) -> dict:40    return json.loads(path.read_text())41