61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
|
|
"""Render a snapshot with whatever labels the manifest supplies.
|
||
|
|
|
||
|
|
The text below is the shared explorer. It must not name a dataspace, a kind,
|
||
|
|
or a predicate. Those strings come from the snapshot and the manifest.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from content_explorer.contract import resolve_claims
|
||
|
|
|
||
|
|
|
||
|
|
def render_explorer(snapshot: dict, manifest: dict) -> str:
|
||
|
|
kinds = {kind["id"]: kind for kind in snapshot.get("kinds", [])}
|
||
|
|
entities = {entity["id"]: entity for entity in snapshot.get("entities", [])}
|
||
|
|
claims = snapshot.get("claims", [])
|
||
|
|
policy = manifest.get("factPolicy", "show-all")
|
||
|
|
lines: list[str] = [manifest["home"]["heading"], ""]
|
||
|
|
|
||
|
|
for section in manifest.get("navigation", []):
|
||
|
|
names = [kinds[kind_id]["plural"] for kind_id in section["kinds"] if kind_id in kinds]
|
||
|
|
lines.append(f"{section['label']}: {', '.join(names)}")
|
||
|
|
lines.append("")
|
||
|
|
|
||
|
|
for kind_id in manifest["home"]["featuredKinds"]:
|
||
|
|
kind = kinds.get(kind_id)
|
||
|
|
if kind is None:
|
||
|
|
continue
|
||
|
|
lines.append(f"## {kind['plural']}")
|
||
|
|
members = [entity for entity in snapshot.get("entities", []) if entity.get("kindId") == kind_id]
|
||
|
|
if not members:
|
||
|
|
lines.append("(none)")
|
||
|
|
for entity in members:
|
||
|
|
field = resolve_claims(
|
||
|
|
[claim for claim in claims if claim.get("subjectId") == entity["id"]],
|
||
|
|
"tldr",
|
||
|
|
policy,
|
||
|
|
)
|
||
|
|
if field["state"] == "unknown":
|
||
|
|
shown = "unknown"
|
||
|
|
elif field["state"] == "contradicted":
|
||
|
|
shown = "contested: " + " | ".join(str(value) for value in field["values"])
|
||
|
|
elif field["state"] == "withheld":
|
||
|
|
shown = "withheld"
|
||
|
|
elif field["values"]:
|
||
|
|
shown = str(field["values"][0])
|
||
|
|
else:
|
||
|
|
shown = entity.get("tldr", "")
|
||
|
|
lines.append(f"- {entity['title']}: {shown}")
|
||
|
|
lines.append("")
|
||
|
|
|
||
|
|
lines.append("## Relations")
|
||
|
|
labels = manifest.get("relationLabels") or {}
|
||
|
|
for relation in snapshot.get("relations", []):
|
||
|
|
predicate = relation["predicate"]
|
||
|
|
wording = labels.get(predicate, {}).get("forward", predicate)
|
||
|
|
origin = entities.get(relation["from"], {}).get("title", relation["from"])
|
||
|
|
destination = entities.get(relation["to"], {}).get("title", relation["to"])
|
||
|
|
lines.append(f"- {origin} — {wording} → {destination}")
|
||
|
|
lines.append("")
|
||
|
|
return "\n".join(lines).rstrip() + "\n"
|