Source code for cadrumo.adapters.persistence.storage.operator_scope
"""Persistence adapters for the application operator-scope ports."""from__future__importannotationsfrompathlibimportPathfrom....application.auth.operator_scope_portsimport(OperatorScopeBucketPaths,OperatorScopePorts,OperatorScopeSession,OperatorScopeStorageError,)from.bucket.directory_layoutimportbucket_pathsfrom.bucket.errorsimportBucketErrorfrom.bucket.lockfileimportacquire_lock,release_lockfrom.master_key.active_sessionimportcurrent_active_bucket_sessionclass_PersistenceOperatorScopeStorage:"""Translate bucket layout and lock primitives into the app port."""defresolve(self,root:Path,bucket_id:str)->OperatorScopeBucketPaths:try:paths=bucket_paths(root,bucket_id)exceptBucketErrorasexc:raiseOperatorScopeStorageError(operation="resolve",bucket_id=bucket_id,failure_kind=type(exc).__name__,)fromexcreturnOperatorScopeBucketPaths(bucket_id=paths.bucket_id,bucket_dir=paths.bucket_dir)defacquire_lock(self,paths:OperatorScopeBucketPaths,*,wait_seconds:float)->None:try:acquire_lock(paths,wait_seconds=wait_seconds)exceptBucketErrorasexc:raiseOperatorScopeStorageError(operation="acquire_lock",bucket_id=paths.bucket_id,failure_kind=type(exc).__name__,)fromexcdefrelease_lock(self,paths:OperatorScopeBucketPaths)->None:try:release_lock(paths)exceptBucketErrorasexc:raiseOperatorScopeStorageError(operation="release_lock",bucket_id=paths.bucket_id,failure_kind=type(exc).__name__,)fromexcclass_PersistenceOperatorScopeSession:"""Translate the live persistence session into application-safe facts."""defcurrent(self)->OperatorScopeSession|None:session=current_active_bucket_session()ifsessionisNone:returnNonereturnOperatorScopeSession(bucket_id=session.bucket_id,storage_root=session.storage_root,)defserves_bucket(self,session:OperatorScopeSession|None,bucket_id:str)->bool:returnsessionisnotNoneandsession.bucket_id==bucket_id
[docs]defbuild_operator_scope_ports()->OperatorScopePorts:"""Build the translated operator-scope capability bundle."""returnOperatorScopePorts(storage=_PersistenceOperatorScopeStorage(),session=_PersistenceOperatorScopeSession(),)