Source code for cadrumo.application.ledger.column_roles
"""Inward port for semantic column-role resolution.Financial-source adapters consume the result of column-role inference, but theinference implementation is an optional outbound capability. This structuralport keeps the inbound provider independent of that concrete adapter whilepreserving the existing role tuple as the only required contract."""from__future__importannotationsfromcollections.abcimportCallable,GeneratorfromcontextlibimportcontextmanagerfromcontextvarsimportContextVarfromtypingimportProtocol,runtime_checkablefrom...core.field_roleimportFieldRolefrom...core.tabularimportNormalizedTable
[docs]@runtime_checkableclassColumnRoleMappingPort(Protocol):"""One accepted role for each normalized table column."""@propertydefroles(self)->tuple[FieldRole,...]:"""Return roles in the same positional order as table headers."""...
ColumnRoleMappingResolverPort=Callable[[NormalizedTable],ColumnRoleMappingPort|None]"""Resolve one normalized table into a role mapping, or decline it."""_BOUND_COLUMN_ROLE_MAPPING_RESOLVER:ContextVar[ColumnRoleMappingResolverPort]=ContextVar("cadrumo_column_role_mapping_resolver",)
[docs]@contextmanagerdefbind_column_role_mapping_resolver(resolver:ColumnRoleMappingResolverPort,)->Generator[ColumnRoleMappingResolverPort]:"""Bind the outward column-role implementation for one runtime scope."""token=_BOUND_COLUMN_ROLE_MAPPING_RESOLVER.set(resolver)try:yieldresolverfinally:_BOUND_COLUMN_ROLE_MAPPING_RESOLVER.reset(token)
[docs]defcolumn_role_mapping_resolver()->ColumnRoleMappingResolverPort|None:"""Return the resolver installed by an outer composition root, if any."""return_BOUND_COLUMN_ROLE_MAPPING_RESOLVER.get(None)
[docs]defresolve_column_roles(table:NormalizedTable)->ColumnRoleMappingPort|None:"""Resolve ``table`` through the currently composed column-role port."""resolver=column_role_mapping_resolver()returnNoneifresolverisNoneelseresolver(table)