Source code for cadrumo.core.redaction.tax_identity_admission
"""Host-composed admission gate for tax identities found by redaction.Redaction scans wide and admits narrowly: a span shaped like a Spanish NIF/CIFor a prefixed NIF-IVA is only hashed when it really is one, because the sameshapes cover ordinary operator output (``SE-2026-000412``, ``F-2026-0142``, thegroups of a printed IBAN). Deciding that needs the Spanish control-charactertables and the dated per-Member-State NIF-IVA formats, which are publishedregistry authority this core module cannot import.The process host binds the authority-backed gate. Without one -- or when thebound gate reports that its authority is unavailable -- redaction admits onlexical shape alone, which over-redacts ordinary output rather than letting areal identity through."""from__future__importannotationsfromcollections.abcimportGeneratorfromcontextlibimportcontextmanagerfromtypingimportProtocolfrom..process_bindingimportProcessScopedBinding
[docs]classTaxIdentityAdmission(Protocol):"""Decide whether a normalised span is a real tax identity. Each method returns ``None`` when its authority cannot be consulted, so the caller applies its own fail-safe instead of treating absence as refusal. """
[docs]defadmits_spanish_identity(self,normalised:str)->bool|None:"""Return whether ``normalised`` is a valid Spanish NIF, NIE, or CIF."""...
[docs]defadmits_nif_iva(self,normalised:str)->bool|None:"""Return whether ``normalised`` matches its Member State's NIF-IVA format."""...
[docs]@contextmanagerdefbind_tax_identity_admission(admission:TaxIdentityAdmission|None)->Generator[TaxIdentityAdmission|None]:"""Publish ``admission`` process-wide for the scope, restoring the previous gate after. ``None`` suspends the gate for the scope, leaving redaction on its fallback. Process-wide rather than context-local: log records are redacted on worker threads and fresh event-loop contexts that never inherit a context variable. """previous=_TAX_IDENTITY_ADMISSION.get()_TAX_IDENTITY_ADMISSION.bind(admission)try:yieldadmissionfinally:_TAX_IDENTITY_ADMISSION.bind(previous)
[docs]deftax_identity_admission()->TaxIdentityAdmission|None:"""Return the host-bound admission gate, or ``None`` when none is bound."""return_TAX_IDENTITY_ADMISSION.get()