o
    $i,%                     @   sh   d dl Z d dlZd dlmZ G dd dZe ddd e D Zedd	 Z	e	e_
d
d Zee_dS )    N)urlparsec                   @   sr   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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abfssazurecondahttps clsr   r   ^/home/ubuntu/veenaModal/venv/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   r   get_remote_protocols&   r   z&ProtocolsProvider.get_remote_protocolsc                 C   s   zddl }ddlm} W n ty   td| j w | }| r)|d}nddlm	} ddl
m} |jd||d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. r   )UNSIGNED)Config)signature_version)configclient)boto3
smart_openr   ImportError_MISSING_DEPENDENCIES_WARNINGSessionget_credentialsr   botocorer   botocore.configr   )r   r   	open_filesession	s3_clientr   r   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_protocolM   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_namer1   r(   r   r   r   _handle_azure_protocolc   s,   
z(ProtocolsProvider._handle_azure_protocolc                    sR   zddl ddlm  W n ty   td| j w dd fdd
}|dfS )a&  Set up Azure Blob File System Secure (ABFSS) protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
            ValueError: If the ABFSS URI format is invalid.
        r   Nr-   z\You must `pip install adlfs azure-identity` to fetch URIs in Azure Blob File System Secure. r(   c                   s   t | }|jrd|jvrtd|  |jdd\}}|s%td|  |r,|ds3td|  |dd }|sCtd	|  j|  d
}|| |S )N@z6Invalid ABFSS URI format - missing container@account:    z1Invalid ABFSS URI format - empty container name: z.dfs.core.windows.netzSInvalid ABFSS URI format - invalid hostname (must end with .dfs.core.windows.net): .r   z/Invalid ABFSS URI format - empty account name: )account_namer2   )r   netlocr7   splitendswithAzureBlobFileSystemr   )urimoder(   parsedcontainer_parthostname_partr8   
filesystemr.   adlfsr   r   r%      s0   z;ProtocolsProvider._handle_abfss_protocol.<locals>.open_file)rJ   r3   r.   r   r    )r   r%   r   rI   r   _handle_abfss_protocol   s   &z(ProtocolsProvider._handle_abfss_protocolprotocol
source_uri	dest_filec              	   C   sN  ||   v sJ d}d}|dkr |tdd }dddd}nI|dkr+|  \}}n>|dkr6|  \}}n3|d	krA|  \}}n(|d
krL|  \}}nzddlm} W n tyh   td|	  d| j
 w ||d|d-}t|d}||  W d   n1 sw   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   )rC   rD   r(   r   r   r   r%      s   
z8ProtocolsProvider.download_remote_uri.<locals>.open_filer   r   r   r   r   r   z+You must `pip install smart_open` to fetch z URIs. rbwb)r   lenr)   r,   r9   rK   r   r   r   upperr    writeread)r   rL   rM   rN   tpr%   finfoutr   r   r   download_remote_uri   s@   "z%ProtocolsProvider.download_remote_uriN)__name__
__module____qualname__r    classmethodr   r   r)   r,   r9   rK   strrY   r   r   r   r   r      s"    


"

'
<r   Protocolc                 C   s   i | ]}|  |qS r   rS   .0rL   r   r   r   
<dictcomp>   s    rc   c                    s    fddt  D S )Nc                    s   g | ]} |   qS r   r`   ra   r   r   r   
<listcomp>   s    z%_remote_protocols.<locals>.<listcomp>)r   r   r   r   r   r   _remote_protocols   s   
re   c                 C   s   t | j||S rO   )r   rY   value)selfrM   rN   r   r   r   _download_remote_uri  s   rh   )enumr5   urllib.parser   r   Enumr   r_   r]   re   remote_protocolsrh   rY   r   r   r   r   <module>   s     q

