o
    bi/                     @   sn   d dl Z d dlZd dlmZ G dd dZe Ze ddd e D Ze	dd	 Z
e
e_d
d Zee_dS )    N)get_protocols_providerc                   @   sf   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	ede
de
de
fddZdS )ProtocolsProviderz~Note that these must be preinstalled on all nodes in the Ray cluster; it is not sufficient to install them in the runtime_env.c                 C      h dS )N>	   gss3uvgcspipfileazurecondahttps clsr   r   U/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/runtime_env/protocol.pyget_protocols      zProtocolsProvider.get_protocolsc                 C   r   )N>   r   r   r
   r   r   r   r   r   r   r   get_remote_protocols%   r   z&ProtocolsProvider.get_remote_protocolsc                 C   sL   zddl }ddlm} W n ty   td| j w d|di}||fS )zSet up S3 protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
        r   NopenzBYou must `pip install smart_open[s3]` to fetch URIs in s3 bucket. clientr   )boto3
smart_openr   ImportError_MISSING_DEPENDENCIES_WARNINGr   )r   r   	open_filetransport_paramsr   r   r   _handle_s3_protocol)   s   
z%ProtocolsProvider._handle_s3_protocolc                 C   s@   zddl m} ddlm} W |dfS  ty   td| j w )zSet up Google Cloud Storage protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
        r   )storager   zTYou must `pip install smart_open[gcs]` to fetch URIs in Google Cloud Storage bucket.N)google.cloudr   r   r   r   r   )r   r   r   r   r   r   _handle_gs_protocol?   s   
z%ProtocolsProvider._handle_gs_protocolc                 C   s   zddl m} ddlm} ddlm} W n ty"   td| j w t	d}|s.t
dd| d	}d
||| di}||fS )a  Set up Azure blob storage protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
            ValueError: If required environment variables are not set.
        r   )DefaultAzureCredential)BlobServiceClientr   zpYou must `pip install azure-storage-blob azure-identity smart_open[azure]` to fetch URIs in Azure Blob Storage. AZURE_STORAGE_ACCOUNTz`Azure Blob Storage authentication requires AZURE_STORAGE_ACCOUNT environment variable to be set.zhttps://z.blob.core.windows.net/r   )account_url
credential)azure.identityr"   azure.storage.blobr#   r   r   r   r   osgetenv
ValueError)r   r"   r#   r   azure_storage_account_namer%   r   r   r   r   _handle_azure_protocolU   s,   
z(ProtocolsProvider._handle_azure_protocolprotocol
source_uri	dest_filec              	   C   s8  ||   v sJ d}d}|dkr |tdd }dddd}n>|dkr+|  \}}n3|dkr6|  \}}n(|d	krA|  \}}nzd
dlm} W n ty]   td|  d| j	 w ||d|d-}||d}|
|  W d   n1 s}w   Y  W d   dS W d   dS 1 sw   Y  dS )ax  Download file from remote URI to destination file.

        Args:
            protocol: The protocol to use for downloading (e.g., 's3', 'https').
            source_uri: The source URI to download from.
            dest_file: The destination file path to save to.

        Raises:
            ImportError: If required dependencies for the protocol are not installed.
        Nr
   zfile://)r   c                S   s
   t | |S Nr   )urimoder   r   r   r   r      s   
z8ProtocolsProvider.download_remote_uri.<locals>.open_filer   r   r   r   r   z+You must `pip install smart_open` to fetch z URIs. rbwb)r   lenr   r!   r-   r   r   r   upperr   writeread)r   r.   r/   r0   tpr   finfoutr   r   r   download_remote_uri}   s<   "z%ProtocolsProvider.download_remote_uriN)__name__
__module____qualname__r   classmethodr   r   r   r!   r-   strr=   r   r   r   r   r      s    




'r   Protocolc                 C   s   i | ]}|  |qS r   r7   .0r.   r   r   r   
<dictcomp>   s    rG   c                    s    fddt  D S )Nc                    s   g | ]} |   qS r   rD   rE   r   r   r   
<listcomp>   s    z%_remote_protocols.<locals>.<listcomp>)_protocols_providerr   r   r   r   r   _remote_protocols   s   
rJ   c                 C   s   t | j||S r1   )rI   r=   value)selfr/   r0   r   r   r   _download_remote_uri   s   rM   )enumr)   %ray._private.runtime_env.default_implr   r   rI   Enumr   rC   rA   rJ   remote_protocolsrM   r=   r   r   r   r   <module>   s     #

