Files
aigc-home/docs/hel-892/birefnet-document-evidence/build_definition.py
T

51 lines
1.9 KiB
Python

"""Bind reviewed editorial data to exact static source; never execute upstream code."""
from __future__ import annotations
import hashlib
import json
from pathlib import Path
REPO = Path(__file__).resolve().parents[3]
OUT = Path(__file__).resolve().parent
PATHS = [
"example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/"
"birefnet_图像移除背景.json"
]
def canonical(value):
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()
def pointer(obj, path):
for part in path.strip("/").split("/"):
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def build(index=0):
block = json.loads((OUT / "editorial.json").read_bytes())
raw = (REPO / PATHS[index]).read_bytes()
graph = json.loads(raw)
assert block["source_path"] == PATHS[index]
# Editorial meanings only apply to this original, including modes/UE/endpoints.
expected = json.loads((OUT / "original-binding.json").read_bytes())
assert hashlib.sha256(raw).hexdigest() == expected["raw_sha256"]
for source in block["sources"]:
data = (OUT / source["local_file"]).read_bytes()
assert hashlib.sha256(data).hexdigest() == source["sha256"]
assert len(source["commit"]) == 40 and source["commit"] in source["url"]
assert 1 <= source["lines"][0] <= source["lines"][1] <= len(data.decode().splitlines())
for field in block["fields"]:
field["value"] = pointer(graph, field["pointer"])
field["json_type"] = type(field["value"]).__name__
for note in block["comments"]:
note["text"] = pointer(graph, note["pointer"])
block.update(
source_sha1=hashlib.sha1(raw).hexdigest(),
source_sha256=hashlib.sha256(raw).hexdigest(),
canonical_sha256=hashlib.sha256(canonical(graph)).hexdigest(),
)
return block