"""Storage route derivation helpers for core settings.This module implements the public :func:`~core.config.classify_storage_route`and :func:`~core.config.settings_for_active_profile_bucket` facades. Itclassifies :class:`~core.config.Settings` into a:class:`~core.config.StorageRouteClassification` whose:class:`~core.config.StorageRouteKind` distinguishes explicit databaseURLs, root-fallback SQLite databases, and active-profile bucket databases.Application write guards such as:func:`~application.storage_write_policy.inspect_storage_write_policy`consume that route classification rather than re-parsing database URLs."""from__future__importannotationsfrompathlibimportPathfromtypingimportTYPE_CHECKINGfromurllib.parseimportunquotefrom.errorsimportCoreValidationErrorifTYPE_CHECKING:from.configimportSettings,StorageRouteClassification
[docs]defclassify_storage_route_for_settings(settings:Settings)->StorageRouteClassification:"""Classify the effective primary SQL route. Args: settings: The :class:`~core.config.Settings` instance whose ``aeat_database_url`` and ``aeat_local_storage_root`` define the route. Returns: A :class:`~core.config.StorageRouteClassification` carrying the effective :class:`~core.config.StorageRouteKind`, database URL, filesystem path when SQLite-backed, and active bucket id when present. """from.configimportStorageRouteClassification,StorageRouteKinddatabase_url=settings.aeat_database_urldatabase_path=_sqlite_database_path(database_url)if"aeat_database_url"insettings.model_fields_set:returnStorageRouteClassification(kind=StorageRouteKind.EXPLICIT_DATABASE_URL,database_url=database_url,database_path=database_path,)root_fallback=_normalized_path(settings.aeat_local_storage_root/"aeat.db")ifdatabase_pathisnotNoneand_normalized_path(database_path)==root_fallback:returnStorageRouteClassification(kind=StorageRouteKind.ROOT_FALLBACK_DATABASE,database_url=database_url,database_path=database_path,)bucket_id=_bucket_id_for_route(database_path=database_path,storage_root=settings.aeat_local_storage_root,)ifbucket_id:returnStorageRouteClassification(kind=StorageRouteKind.ACTIVE_BUCKET_DATABASE,database_url=database_url,database_path=database_path,bucket_id=bucket_id,)returnStorageRouteClassification(kind=StorageRouteKind.EXPLICIT_DATABASE_URL,database_url=database_url,database_path=database_path,)
[docs]defsettings_for_bucket_route(bucket_id:str,source:Settings)->Settings:"""Return settings routed to ``bucket_id``'s active-profile database. Args: bucket_id: Active-profile bucket id to embed in the returned :class:`~core.config.Settings`. source: Source :class:`~core.config.Settings` whose non-route fields are preserved. Returns: A :class:`~core.config.Settings` instance equivalent to ``source`` but deriving ``aeat_database_url`` from ``bucket_id``. Raises: CoreValidationError: When ``bucket_id`` is blank or ``source`` already carries an explicit database URL. """from.configimportSettingstrimmed=bucket_id.strip()ifnottrimmed:raiseCoreValidationError("bucket_id must not be blank")if"aeat_database_url"insource.model_fields_set:raiseCoreValidationError("cannot derive an active profile bucket route from an explicit database URL")values=source.model_dump()values.pop("aeat_database_url",None)values["aeat_active_profile"]=trimmedderived=Settings.model_validate(values)explicit_fields=(source.model_fields_set-{"aeat_database_url"})|{"aeat_active_profile"}object.__setattr__(derived,"__pydantic_fields_set__",explicit_fields)returnderived