Source code for cadrumo.application.ledger.usage_ratio_repository
"""Explicit lifetime binding for usage-ratio profile persistence."""from__future__importannotationsfromcollections.abcimportGeneratorfromcontextlibimportcontextmanagerfromcontextvarsimportContextVarfromdecimalimportDecimalfromtypingimportTYPE_CHECKING,Protocolfrom...core.errors.hierarchyimportInternalInvariantErrorfrom...domain.usage_ratios.modelimportUsageRatioProfileifTYPE_CHECKING:from...domain.calculations.registry.authorityimportPinnedAuthorityOperation
[docs]classUsageRatioCensoGuardLoader(Protocol):"""Load one profile's persisted usage ratios under its censo guard."""def__call__(self,*,bucket_id:str,raw_afectacion_ratio:Decimal|None,year:int,operation:PinnedAuthorityOperation,)->UsageRatioProfile:"""Return the guarded ratio profile for ``bucket_id`` under ``year``'s rules."""...
[docs]classUsageRatioProfileLoader(Protocol):"""Load one profile's persisted usage ratios."""def__call__(self,*,bucket_id:str,operation:PinnedAuthorityOperation)->UsageRatioProfile:"""Return the persisted ratio profile for ``bucket_id``."""...
[docs]classUsageRatioProfileSaver(Protocol):"""Persist one profile's usage ratios."""def__call__(self,profile:UsageRatioProfile,*,bucket_id:str)->None:"""Persist ``profile`` for ``bucket_id``."""...
[docs]@contextmanagerdefbind_usage_ratio_profile_persistence(*,loader:UsageRatioProfileLoader,saver:UsageRatioProfileSaver,)->Generator[None]:"""Bind the load/save pair as one atomic usage-ratio persistence lifetime."""token=_BOUND_USAGE_RATIO_PROFILE_PERSISTENCE.set((loader,saver))try:yieldfinally:_BOUND_USAGE_RATIO_PROFILE_PERSISTENCE.reset(token)
[docs]@contextmanagerdefbind_usage_ratio_censo_guard_loader(loader:UsageRatioCensoGuardLoader,)->Generator[UsageRatioCensoGuardLoader]:"""Bind one outward-composed censo-guarded usage-ratio loader."""token=_BOUND_USAGE_RATIO_CENSO_GUARD_LOADER.set(loader)try:yieldloaderfinally:_BOUND_USAGE_RATIO_CENSO_GUARD_LOADER.reset(token)
[docs]defusage_ratio_profile_with_censo_guard(*,bucket_id:str,raw_afectacion_ratio:Decimal|None,year:int,operation:PinnedAuthorityOperation,)->UsageRatioProfile:"""Resolve and invoke the composed censo-guarded ratio loader for ``year``."""try:loader=_BOUND_USAGE_RATIO_CENSO_GUARD_LOADER.get()exceptLookupErroraserror:raiseInternalInvariantError("usage-ratio persistence has not been composed")fromerrorreturnloader(bucket_id=bucket_id,raw_afectacion_ratio=raw_afectacion_ratio,year=year,operation=operation,)
[docs]defload_usage_ratio_profile(*,bucket_id:str,operation:PinnedAuthorityOperation)->UsageRatioProfile:"""Load the usage-ratio profile through the explicitly composed authority."""try:loader,_saver=_BOUND_USAGE_RATIO_PROFILE_PERSISTENCE.get()exceptLookupErroraserror:raiseInternalInvariantError("usage-ratio persistence has not been composed")fromerrorreturnloader(bucket_id=bucket_id,operation=operation)
[docs]defsave_usage_ratio_profile(profile:UsageRatioProfile,*,bucket_id:str)->None:"""Persist the usage-ratio profile through the explicitly composed authority."""try:_loader,saver=_BOUND_USAGE_RATIO_PROFILE_PERSISTENCE.get()exceptLookupErroraserror:raiseInternalInvariantError("usage-ratio persistence has not been composed")fromerrorsaver(profile,bucket_id=bucket_id)