Source code for cadrumo.adapters.persistence.profile.relation_binding_join
"""Frozen relation-id to binding-id join captured from the pre-absorption corpus.Relations were a separate identified registry family whose declarations havesince been absorbed into the binding providers that replaced them. The authoredcorpus therefore no longer carries a relation id at all, so the correspondencebetween a retired relation id and the binding that inherited its join pointcannot be recovered from the live registry: it exists only as the frozen tablecaptured while both families were still declared.Persisted calculation revisions written before the absorption carry overridekeys in the retired vocabulary, so the table has to ship with the product forthe stored-data migration that rekeys them. It lives beside the migration thatconsumes it, as a packaged resource read through ``importlib.resources``, ratherthan in the development tree the capture tool writes to -- a migration thatcould only run from a source checkout would silently leave an installed user'sstored overrides in the retired vocabulary forever.The table is frozen by construction: it describes a corpus state that no longerexists, so nothing can legitimately add a row to it. It is many-to-one -- tworetired relation ids that differed only on a temporal axis the binding nowcarries as typed data map onto the same binding -- and consumers must handlethat collapse rather than assume injectivity."""from__future__importannotationsfromfunctoolsimportcachefromimportlib.resourcesimportfiles# nosemgrepfromtypesimportMappingProxyTypefromtypingimportFinalfrompydanticimportTypeAdapter,ValidationErrorfrom....core.errors.hierarchyimportCadrumoErrorfrom....core.external_constantsimportUTF_8_ENCODINGfrom....domain.calculations.registry.idsimportBindingId,RelationId_JOIN_RESOURCE_NAME:Final[str]="relation_binding_join.json"_JOIN_DOCUMENT_KEY:Final[str]="relation_binding_join"_JOIN_DOCUMENT_ADAPTER:Final[TypeAdapter[dict[str,dict[RelationId,BindingId]]]]=TypeAdapter(dict[str,dict[RelationId,BindingId]],)
[docs]classRelationBindingJoinError(CadrumoError):"""Raised when the bundled relation-to-binding join cannot be read as declared. A packaging fault, not a data condition: the table ships inside the wheel, so an absent resource or a payload that is not a flat string-to-string map means the distribution itself is broken. It refuses rather than degrading to an empty join, because an empty join would make the migration report every stored override as unmappable and destroy exactly the evidence it exists to preserve. """
[docs]@cachedefbundled_relation_binding_join()->MappingProxyType[RelationId,BindingId]:"""Return the frozen retired-relation-id to binding-id join. Cached because the table is immutable product data rather than profile state, and read lazily so importing this module costs nothing in a process that never migrates. Returns: A read-only mapping from each retired relation id to the binding id that inherited its join point. Many-to-one: distinct keys may share a value. Raises: RelationBindingJoinError: The bundled resource is absent, is not a valid join table, or carries no map under its declared document key. """resource=files(__package__).joinpath(_JOIN_RESOURCE_NAME)try:raw=resource.read_text(encoding=UTF_8_ENCODING)except(FileNotFoundError,OSError)asexc:raiseRelationBindingJoinError(f"bundled relation-to-binding join resource {_JOIN_RESOURCE_NAME!r} is not readable",)fromexctry:document=_JOIN_DOCUMENT_ADAPTER.validate_json(raw)exceptValidationErrorasexc:# The ids are validated against their own registry grammar rather than# merely being checked for stringhood, so a table row that could never# name a real binding is a packaging failure here instead of an# unresolvable override key at migration time.raiseRelationBindingJoinError(f"bundled relation-to-binding join resource {_JOIN_RESOURCE_NAME!r} is not a valid join table",)fromexcjoin=document.get(_JOIN_DOCUMENT_KEY)ifjoinisNone:raiseRelationBindingJoinError(f"bundled relation-to-binding join carries no {_JOIN_DOCUMENT_KEY!r} object",)returnMappingProxyType(dict(join))
[docs]defbundled_relation_binding_join_targets()->frozenset[BindingId]:"""Return every binding id the frozen join resolves onto. The migration needs this set to tell an override key that is ALREADY a binding id (a post-cut write, which must be left exactly as it is so the migration stays idempotent) from one that is neither a known relation id nor a known binding id (an orphan, which must be refused rather than dropped). """returnfrozenset(bundled_relation_binding_join().values())