ids.py 900 B

12345678910111213141516171819202122232425262728293031323334
  1. """Stable ID helpers for Atlas.
  2. These functions keep entity and claim identifiers deterministic across the app.
  3. The same stable hash should be used for:
  4. - atlas_id / entity IRI fragments
  5. - claim IDs
  6. - claim IRIs in Turtle exports
  7. Semantics live in triples; the IDs themselves stay opaque.
  8. """
  9. from __future__ import annotations
  10. import hashlib
  11. def stable_hash(*parts: str, length: int = 16) -> str:
  12. material = "|".join((part or "").strip() for part in parts)
  13. return hashlib.sha1(material.encode("utf-8")).hexdigest()[:length]
  14. def claim_hash(
  15. subject: str,
  16. predicate: str,
  17. object_value: str,
  18. layer: str,
  19. status: str = "active",
  20. created_at: str | None = None,
  21. ) -> str:
  22. return stable_hash(subject, predicate, object_value, layer, status, created_at or "")
  23. def entity_hash(*parts: str, length: int = 16) -> str:
  24. return stable_hash(*parts, length=length)