[docs]classSetupMode(Enum):"""Setup mode for AstraDBEnvironment as enumerator."""SYNC=1ASYNC=2OFF=3
class_AstraDBEnvironment:def__init__(self,token:Optional[str]=None,api_endpoint:Optional[str]=None,astra_db_client:Optional[AstraDB]=None,async_astra_db_client:Optional[AsyncAstraDB]=None,namespace:Optional[str]=None,)->None:self.token=tokenself.api_endpoint=api_endpointastra_db=astra_db_clientasync_astra_db=async_astra_db_clientself.namespace=namespacetry:fromastrapy.dbimport(AstraDB,AsyncAstraDB,)except(ImportError,ModuleNotFoundError):raiseImportError("Could not import a recent astrapy python package. ""Please install it with `pip install --upgrade astrapy`.")# Conflicting-arg checks:ifastra_db_clientisnotNoneorasync_astra_db_clientisnotNone:iftokenisnotNoneorapi_endpointisnotNone:raiseValueError("You cannot pass 'astra_db_client' or 'async_astra_db_client' to ""AstraDBEnvironment if passing 'token' and 'api_endpoint'.")iftokenandapi_endpoint:astra_db=AstraDB(token=token,api_endpoint=api_endpoint,namespace=self.namespace,)async_astra_db=AsyncAstraDB(token=token,api_endpoint=api_endpoint,namespace=self.namespace,)ifastra_db:self.astra_db=astra_dbifasync_astra_db:self.async_astra_db=async_astra_dbelse:self.async_astra_db=AsyncAstraDB(token=self.astra_db.token,api_endpoint=self.astra_db.base_url,api_path=self.astra_db.api_path,api_version=self.astra_db.api_version,namespace=self.astra_db.namespace,)elifasync_astra_db:self.async_astra_db=async_astra_dbself.astra_db=AstraDB(token=self.async_astra_db.token,api_endpoint=self.async_astra_db.base_url,api_path=self.async_astra_db.api_path,api_version=self.async_astra_db.api_version,namespace=self.async_astra_db.namespace,)else:raiseValueError("Must provide 'astra_db_client' or 'async_astra_db_client' or ""'token' and 'api_endpoint'")class_AstraDBCollectionEnvironment(_AstraDBEnvironment):def__init__(self,collection_name:str,token:Optional[str]=None,api_endpoint:Optional[str]=None,astra_db_client:Optional[AstraDB]=None,async_astra_db_client:Optional[AsyncAstraDB]=None,namespace:Optional[str]=None,setup_mode:SetupMode=SetupMode.SYNC,pre_delete_collection:bool=False,embedding_dimension:Union[int,Awaitable[int],None]=None,metric:Optional[str]=None,)->None:fromastrapy.dbimportAstraDBCollection,AsyncAstraDBCollectionsuper().__init__(token,api_endpoint,astra_db_client,async_astra_db_client,namespace)self.collection_name=collection_nameself.collection=AstraDBCollection(collection_name=collection_name,astra_db=self.astra_db,)self.async_collection=AsyncAstraDBCollection(collection_name=collection_name,astra_db=self.async_astra_db,)self.async_setup_db_task:Optional[Task]=Noneifsetup_mode==SetupMode.ASYNC:async_astra_db=self.async_astra_dbasyncdef_setup_db()->None:ifpre_delete_collection:awaitasync_astra_db.delete_collection(collection_name)ifinspect.isawaitable(embedding_dimension):dimension:Optional[int]=awaitembedding_dimensionelse:dimension=embedding_dimensionawaitasync_astra_db.create_collection(collection_name,dimension=dimension,metric=metric)self.async_setup_db_task=asyncio.create_task(_setup_db())elifsetup_mode==SetupMode.SYNC:ifpre_delete_collection:self.astra_db.delete_collection(collection_name)ifinspect.isawaitable(embedding_dimension):raiseValueError("Cannot use an awaitable embedding_dimension with async_setup ""set to False")self.astra_db.create_collection(collection_name,dimension=embedding_dimension,# type: ignore[arg-type]metric=metric,)defensure_db_setup(self)->None:ifself.async_setup_db_task:try:self.async_setup_db_task.result()exceptInvalidStateError:raiseValueError("Asynchronous setup of the DB not finished. ""NB: AstraDB components sync methods shouldn't be called from the ""event loop. Consider using their async equivalents.")asyncdefaensure_db_setup(self)->None:ifself.async_setup_db_task:awaitself.async_setup_db_task