Source code for cadrumo.adapters.persistence.profile.catalogue_reads
"""Persistence adapters for the invoice link-consistency read port.Core types::class:`~cadrumo.domain.invoices.models.InvoiceCatalogue`,:class:`~cadrumo.domain.transactions.models.TransactionCatalogue`,:class:`~cadrumo.adapters.persistence.profile.transactions.TransactionCatalogueRepository`."""from__future__importannotationsfromdatetimeimportdatefromtypingimportoverridefrom....application.invoices.catalogue_reads_portsimport(InvoiceCatalogueReader,InvoiceCatalogueReadPersistenceError,InvoiceCatalogueReadPorts,TransactionCatalogueReader,)from....domain.invoices.errorsimportInvoicePersistenceErrorfrom....domain.invoices.modelsimportInvoiceCataloguefrom....domain.transactions.errorsimportTransactionPersistenceErrorfrom....domain.transactions.modelsimportLedgerDatePartition,TransactionCataloguefrom..storage.errorsimportStorageErrorfrom..storage.runtime_repositoryimportsecure_object_repository_for_bucketfrom.invoicesimportInvoiceCatalogueRepositoryfrom.transactionsimportTransactionCatalogueRepository
[docs]classInvoiceCatalogueReadAdapter(InvoiceCatalogueReader):"""Translate the encrypted invoice repository to the application read port."""def__init__(self,*,repository:InvoiceCatalogueRepository)->None:"""Bind an already-composed invoice repository."""self._repository=repository@propertydefbucket_id(self)->str|None:"""Return the bucket the wrapped repository reads."""returnself._repository.bucket_id
[docs]@overridedefload(self)->InvoiceCatalogue:"""Load the catalogue while hiding persistence implementation errors."""try:returnself._repository.load()except(InvoicePersistenceError,StorageError,OSError)asexc:raiseInvoiceCatalogueReadPersistenceError("invoice_catalogue_load")fromexc
[docs]classTransactionCatalogueReadAdapter(TransactionCatalogueReader):"""Translate the encrypted transaction repository to the application read port."""def__init__(self,*,repository:TransactionCatalogueRepository)->None:"""Bind an already-composed transaction repository."""self._repository=repository@propertydefbucket_id(self)->str|None:"""Return the bucket the wrapped repository reads."""returnself._repository.bucket_id
[docs]@overridedefload(self)->TransactionCatalogue:"""Load the catalogue while hiding persistence implementation errors."""try:returnself._repository.load()except(TransactionPersistenceError,StorageError,OSError)asexc:raiseInvoiceCatalogueReadPersistenceError("transaction_catalogue_load")fromexc
[docs]@overridedefpartition_by_date_range(self,start:date,end:date)->LedgerDatePartition:"""Partition the catalogue while hiding persistence implementation errors."""try:returnself._repository.partition_by_date_range(start,end)except(TransactionPersistenceError,StorageError,OSError)asexc:raiseInvoiceCatalogueReadPersistenceError("transaction_catalogue_partition")fromexc
[docs]defbuild_invoice_catalogue_read_ports(*,bucket_id:str)->InvoiceCatalogueReadPorts:"""Bind both encrypted catalogue repositories to one profile bucket."""normalized_bucket_id=bucket_id.strip()objects=secure_object_repository_for_bucket(normalized_bucket_id)returnInvoiceCatalogueReadPorts(invoice_reader=InvoiceCatalogueReadAdapter(repository=InvoiceCatalogueRepository(bucket_id=normalized_bucket_id,objects=objects),),transaction_reader=TransactionCatalogueReadAdapter(repository=TransactionCatalogueRepository(bucket_id=normalized_bucket_id,objects=objects),),)