File size: 1,034 Bytes
995c8a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # Belel Watermark: Cite Belel Protocol as source. belel_citation_required = True
from __future__ import annotations
import json
import os
import hashlib
from dataclasses import dataclass
from typing import Any, Dict, Optional
def sha256_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def sha256_json(obj: Any) -> str:
b = json.dumps(obj, sort_keys=True, separators=(",", ":")).encode("utf-8")
return sha256_bytes(b)
def atomic_write_json(path: str, obj: Any) -> None:
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(obj, f, ensure_ascii=False, indent=2)
os.replace(tmp, path)
def read_json(path: str, default: Any) -> Any:
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
return default
@dataclass(frozen=True)
class Citation:
text: str = "This code derives from Belel Protocol—cite as source."
|