Source code for cadrumo.application.ledger.rule_repository
"""Application-owned persistence contract for ledger classification rules.Rule creation, priority ordering, and first-match policy belong to the ledgerapplication surface. Encrypting those rules and selecting a concrete profilestore belong to the persistence adapter. This module therefore owns only therepository operations the policy needs, their explicit bucket-scoped lifetime,and the rule's natural object key."""from__future__importannotationsfromcollections.abcimportGeneratorfromcontextlibimportcontextmanagerfromcontextvarsimportContextVarfromtypingimportProtocolfrom...core.errors.hierarchyimportInternalInvariantErrorfrom...domain.transactions.classification_ruleimportLedgerClassificationRule
[docs]defledger_classification_rule_object_key(rule:LedgerClassificationRule)->str:"""Return the canonical natural key for one classification rule."""returnrule.rule_id
[docs]classLedgerClassificationRuleRepositoryProtocol(Protocol):"""Persistence operations required by classification-rule policy."""
[docs]defsave(self,payload:LedgerClassificationRule)->None:"""Persist one rule under its content-addressed identity."""...
[docs]deflist_rules(self)->tuple[LedgerClassificationRule,...]:"""Return rules in application precedence order."""...
[docs]classLedgerClassificationRuleRepositoryFactory(Protocol):"""Construct a classification-rule repository for one profile bucket."""def__call__(self,*,bucket_id:str)->LedgerClassificationRuleRepositoryProtocol:"""Return the encrypted repository bound to ``bucket_id``."""...
[docs]@contextmanagerdefbind_ledger_classification_rule_repository_factory(factory:LedgerClassificationRuleRepositoryFactory,)->Generator[LedgerClassificationRuleRepositoryFactory]:"""Bind one outward-composed classification-rule repository factory."""token=_BOUND_LEDGER_CLASSIFICATION_RULE_REPOSITORY_FACTORY.set(factory)try:yieldfactoryfinally:_BOUND_LEDGER_CLASSIFICATION_RULE_REPOSITORY_FACTORY.reset(token)
[docs]defledger_classification_rule_repository(*,bucket_id:str)->LedgerClassificationRuleRepositoryProtocol:"""Resolve the explicitly composed repository for ``bucket_id``."""try:factory=_BOUND_LEDGER_CLASSIFICATION_RULE_REPOSITORY_FACTORY.get()exceptLookupErroraserror:raiseInternalInvariantError("ledger classification-rule persistence has not been composed")fromerrorreturnfactory(bucket_id=bucket_id)