Source code for aeat.adapters.persistence.storage.runtime_repository
"""Runtime-owned :class:`SecureObjectRepository` factories.Builds :class:`SecureObjectRepository` instances bound to the active profile'sunlocked storage route, so callers obtain a repository without resolving thestorage namespace or key material themselves."""from__future__importannotationsfrom....core.configimportSettings,StorageRouteKind,classify_storage_route,load_settingsfrom._namespace_registryimportSTORAGE_NAMESPACE_REGISTRYfrom.errorsimportStorageValidationErrorfrom.runtimeimportinspect_bucket_storage_runtime,runtime_not_ready_errorfrom.sqlimportSecureObjectRepositorydef_active_bucket_id_for_source(source:Settings,*,include_process_pointer:bool,)->str|None:"""Resolve the active bucket for ``source`` without ignoring scoped settings."""override=(source.aeat_active_profileor"").strip()ifoverride:returnoverrideroute=classify_storage_route(source)ifroute.kindisStorageRouteKind.ACTIVE_BUCKET_DATABASE:returnroute.bucket_idifinclude_process_pointer:from....coreimportresolve_active_bucket_idreturnresolve_active_bucket_id()returnNone
[docs]defsecure_object_repository_for_bucket(bucket_id:str,settings:Settings|None=None,)->SecureObjectRepository:"""Return a bucket-attached :class:`SecureObjectRepository` through storage runtime."""returninspect_bucket_storage_runtime(bucket_id,settingsorload_settings()).secure_object_repository()
[docs]defsecure_object_repository_for_active_bucket()->SecureObjectRepository:"""Return a :class:`SecureObjectRepository` attached to the selected active profile bucket."""from....coreimportresolve_active_bucket_idbucket_id=resolve_active_bucket_id()ifbucket_idisNone:raiseruntime_not_ready_error("storage runtime is not ready for profile-bound storage: no active profile bucket is selected.",message_key="errors.storage.runtime.no_active_session",)returnsecure_object_repository_for_bucket(bucket_id)
[docs]defsecure_object_repository_for_active_bucket_or_default_route(settings:Settings|None=None,)->SecureObjectRepository:"""Return a :class:`SecureObjectRepository` for the active bucket, or the process default. This lower-level storage helper is for repository base classes that still support explicit injected/default SQL engines in tests and bootstrap-adjacent code. It does not catch active-bucket runtime errors: once a bucket is selected, route/session failures surface from ``secure_object_repository_for_bucket`` instead of falling back to a bare repository. """source=settingsorload_settings()bucket_id=_active_bucket_id_for_source(source,include_process_pointer=settingsisNone,)ifbucket_idisNone:from.sql.engineimportget_enginereturnSecureObjectRepository(engine=get_engine(source),namespace_registry=STORAGE_NAMESPACE_REGISTRY)returnsecure_object_repository_for_bucket(bucket_id,source)
[docs]defsecure_object_repository_for_cold_bootstrap_state(settings:Settings|None=None,)->SecureObjectRepository:"""Return a :class:`SecureObjectRepository` for cold-root recovery reads. This is the narrow bootstrap exception used before any active profile bucket pointer exists. Normal profile-bound code must use ``secure_object_repository_for_active_bucket`` or ``secure_object_repository_for_bucket`` so route/session mismatches fail closed at the storage runtime boundary. """from....coreimportresolve_active_bucket_idsource=settingsorload_settings()route=classify_storage_route(source)ifroute.kindisStorageRouteKind.EXPLICIT_DATABASE_URL:raiseStorageValidationError(translated_message="errors.storage.runtime.cold_bootstrap_explicit_database_refused",)ifroute.kindisStorageRouteKind.ACTIVE_BUCKET_DATABASEor(settingsisNoneandresolve_active_bucket_id()isnotNone):raiseStorageValidationError(translated_message="errors.storage.runtime.cold_bootstrap_active_profile_refused",)from.sql.engineimportget_enginereturnSecureObjectRepository(engine=get_engine(source),namespace_registry=STORAGE_NAMESPACE_REGISTRY)