a
    1$eE                     @   s   d Z ddlZddlZddlmZmZ eeZzddl	Z	W n  e
yZ   ed dZ	Y n0 G dd dZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZdS )z+Abstraction for sessions in various clouds.    N)
parse_pathUnparsedPathz>Could not import boto3, continuing with reduced functionality.c                   @   sb   e Zd ZdZedd Zdd ZedddZed	d
 Z	edd Z
edd Zedd ZdS )SessionzBase for classes that configure access to secured resources.

    Attributes
    ----------
    credentials : dict
        Keys and values for session credentials.

    Notes
    -----
    This class is not intended to be instantiated.

    c                 C   s   t S )  Determine if the given configuration has proper credentials

        Parameters
        ----------
        cls : class
            A Session class.
        config : dict
            GDAL configuration as a dict.

        Returns
        -------
        bool

        NotImplementedclsconfig r   I/var/www/html/django/DPS/env/lib/python3.9/site-packages/fiona/session.pyhascreds   s    zSession.hascredsc                 C   s   t S eGet credentials as GDAL configuration options

        Returns
        -------
        dict

        r   selfr   r   r   get_credential_options1   s    zSession.get_credential_optionsNc                 C   s   |s
t  S || S dS )a  Create a session object matching the foreign `session`.

        Parameters
        ----------
        session : obj
            A foreign session object.
        cls : Session class, optional
            The class to return.

        Returns
        -------
        Session

        N)DummySession)sessionr	   r   r   r   from_foreign_session;   s    zSession.from_foreign_sessionc                 C   s   | st S t| } t| ts | jr$t S | jdks8d| jv r^d| jvr^tdurNtS t	
d t S n:| jdksrd| jv rvtS | jdrtS | jd	krtS t S dS )
zFind the session class suited to the data at `path`.

        Parameters
        ----------
        path : str
            A dataset path or identifier.

        Returns
        -------
        class

        Zs3zamazonaws.comzX-Amz-SignatureNz4boto3 not available, falling back to a DummySession.Zosszaliyuncs.comz
/vsiswift/az)r   r   
isinstancer   is_localschemepathboto3
AWSSessionloginfo
OSSSession
startswithSwiftSessionAzureSession)r   r   r   r   cls_from_pathP   s,    

zSession.cls_from_pathc                 O   s   t | |i |S )a  Create a session object suited to the data at `path`.

        Parameters
        ----------
        path : str
            A dataset path or identifier.
        args : sequence
            Positional arguments for the foreign session constructor.
        kwargs : dict
            Keyword arguments for the foreign session constructor.

        Returns
        -------
        Session

        )r   r#   )r   argskwargsr   r   r   	from_path   s    zSession.from_pathc                  O   s(   t durt| i |S t| i |S dS )a  Create an AWSSession if boto3 is available, else DummySession
        Parameters
        ----------
        path : str
            A dataset path or identifier.
        args : sequence
            Positional arguments for the foreign session constructor.
        kwargs : dict
            Keyword arguments for the foreign session constructor.
        Returns
        -------
        Session
        N)r   r   r   )r$   r%   r   r   r   aws_or_dummy   s    zSession.aws_or_dummyc                  O   sJ   zt j| i |}|j W n* tyD   td t| i |}Y n0 |S )a  Create a session object suited to the environment.
        Parameters
        ----------
        path : str
            A dataset path or identifier.
        args : sequence
            Positional arguments for the foreign session constructor.
        kwargs : dict
            Keyword arguments for the foreign session constructor.
        Returns
        -------
        Session
        zACredentials in environment have expired. Creating a DummySession.)r   r'   credentialsRuntimeErrorr   warningr   )r$   r%   r   r   r   r   from_environ   s    
zSession.from_environ)N)__name__
__module____qualname____doc__classmethodr   r   staticmethodr   r#   r&   r'   r+   r   r   r   r   r      s   


.

r   c                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	r   zmA dummy session.

    Attributes
    ----------
    credentials : dict
        The session credentials.

    c                 O   s   d | _ i | _d S )N)_sessionr(   )r   r$   r%   r   r   r   __init__   s    zDummySession.__init__c                 C   s   dS )r   Tr   r   r   r   r   r      s    zDummySession.hascredsc                 C   s   i S r   r   r   r   r   r   r      s    z#DummySession.get_credential_optionsN)r,   r-   r.   r/   r3   r0   r   r   r   r   r   r   r      s
   	
r   c                	   @   s:   e Zd ZdZdddZedd Zedd	 Zd
d Z	dS )r   z=Configures access to secured resources stored in AWS S3.
    NFc
           
      C   s`   |r|| _ ntj|||||d| _ |	| _ttd|| _|| _| jsV| j rV| j 	 nd| _
dS )a  Create a new AWS session

        Parameters
        ----------
        session : optional
            A boto3 session object.
        aws_unsigned : bool, optional (default: False)
            If True, requests will be unsigned.
        aws_access_key_id : str, optional
            An access key id, as per boto3.
        aws_secret_access_key : str, optional
            A secret access key, as per boto3.
        aws_session_token : str, optional
            A session token, as per boto3.
        region_name : str, optional
            A region name, as per boto3.
        profile_name : str, optional
            A shared credentials profile name, as per boto3.
        endpoint_url: str, optional
            An endpoint_url, as per GDAL's AWS_S3_ENPOINT
        requester_pays : bool, optional
            True if the requester agrees to pay transfer costs (default:
            False)

        )aws_access_key_idaws_secret_access_keyaws_session_tokenregion_nameprofile_nameAWS_NO_SIGN_REQUESTN)r2   r   r   requester_paysboolosgetenvunsignedendpoint_urlZget_credentials_creds)
r   r   Zaws_unsignedr4   r5   r6   r7   r8   r?   r:   r   r   r   r3      s$    %zAWSSession.__init__c                 C   s   ddh | S )r   ZAWS_ACCESS_KEY_IDZAWS_SECRET_ACCESS_KEYissubsetkeysr   r   r   r   r   (  s    zAWSSession.hascredsc                 C   sz   i }| j rD| j  }|jr$|j|d< |jr4|j|d< |jrD|j|d< | jjrX| jj|d< | jrfd|d< | jrv| j|d< |S )!The session credentials as a dictr4   r5   r6   
aws_regionZ	requesterZaws_request_payerZaws_s3_endpoint)	r@   Zget_frozen_credentialsZ
access_keyZ
secret_keytokenr2   r7   r:   r?   )r   resZfrozen_credsr   r   r   r(   :  s     




zAWSSession.credentialsc                 C   sB   | j r*ddi}d| jv r&| jd |d< |S dd | j D S dS )r   r9   YESrE   Z
AWS_REGIONc                 S   s   i | ]\}}|  |qS r   upper.0kvr   r   r   
<dictcomp>\      z5AWSSession.get_credential_options.<locals>.<dictcomp>N)r>   r(   items)r   optsr   r   r   r   N  s    
z!AWSSession.get_credential_options)	NFNNNNNNF
r,   r-   r.   r/   r3   r0   r   propertyr(   r   r   r   r   r   r      s             
8

r   c                   @   s:   e Zd ZdZdddZedd Zedd Zd	d
 Z	dS )	GSSessionzJConfigures access to secured resources stored in Google Cloud Storage
    Nc                 C   s   i | _ |dur|| j d< dS )zCreate new Google Cloude Storage session

        Parameters
        ----------
        google_application_credentials: string
            Path to the google application credentials JSON file.

        Ngoogle_application_credentialsr@   )r   rV   r   r   r   r3   b  s    	zGSSession.__init__c                 C   s   d|v S )r   ZGOOGLE_APPLICATION_CREDENTIALSr   r   r   r   r   r   o  s    zGSSession.hascredsc                 C   s   | j S rD   rW   r   r   r   r   r(     s    zGSSession.credentialsc                 C   s   dd | j  D S )r   c                 S   s   i | ]\}}|  |qS r   rI   rK   r   r   r   rO     rP   z4GSSession.get_credential_options.<locals>.<dictcomp>r(   rQ   r   r   r   r   r     s    z GSSession.get_credential_options)NrS   r   r   r   r   rU   _  s   


rU   c                   @   s:   e Zd ZdZdddZedd Zedd Zd	d
 Z	dS )r   zCConfigures access to secured resources stored in Alibaba Cloud OSS.Nc                 C   s   |||d| _ dS )at  Create new Alibaba Cloud OSS session

        Parameters
        ----------
        oss_access_key_id: string, optional (default: None)
            An access key id
        oss_secret_access_key: string, optional (default: None)
            An secret access key
        oss_endpoint: string, optional (default: None)
            the region attached to the bucket

        )oss_access_key_idoss_secret_access_keyoss_endpointNrW   )r   rZ   r[   r\   r   r   r   r3     s    zOSSSession.__init__c                 C   s   ddh | S )r   ZOSS_ACCESS_KEY_IDZOSS_SECRET_ACCESS_KEYrA   r   r   r   r   r     s    zOSSSession.hascredsc                 C   s   | j S rX   rW   r   r   r   r   r(     s    zOSSSession.credentialsc                 C   s   dd | j  D S )r   c                 S   s   i | ]\}}|  |qS r   rI   rK   r   r   r   rO     rP   z5OSSSession.get_credential_options.<locals>.<dictcomp>rY   r   r   r   r   r     s    z!OSSSession.get_credential_options)NNNrS   r   r   r   r   r     s    


r   c                   @   s:   e Zd ZdZdddZedd Zedd Zd	d
 Z	dS )r!   zPConfigures access to secured resources stored in OpenStack Swift Object Storage.Nc                 C   sb   |r|r||d| _ nHddlm} |r.|| _n||||d| _| j d | j d d| _ dS )aa  Create new OpenStack Swift Object Storage Session.

        Three methods are possible:
            1. Create session by the swiftclient library.
            2. The SWIFT_STORAGE_URL and SWIFT_AUTH_TOKEN (this method
               is recommended by GDAL docs).
            3. The SWIFT_AUTH_V1_URL, SWIFT_USER and SWIFT_KEY (This
               depends on the swiftclient library).

        Parameters
        ----------
        session: optional
            A swiftclient connection object
        swift_storage_url:
            the storage URL
        swift_auth_token:
            the value of the x-auth-token authorization token
        swift_storage_url: string, optional
            authentication URL
        swift_user: string, optional
            user name to authenticate as
        swift_key: string, optional
            key/password to authenticate with

        Examples
        --------
        >>> import rasterio
        >>> from rasterio.session import SwiftSession
        >>> fp = '/vsiswift/bucket/key.tif'
        >>> conn = Connection(
        ...     authurl='http://127.0.0.1:7777/auth/v1.0',
        ...     user='test:tester',
        ...     key='testing'
        ... )
        >>> session = SwiftSession(conn)
        >>> with rasterio.Env(session):
        >>>     with rasterio.open(fp) as src:
        >>>         print(src.profile)

        )swift_storage_urlswift_auth_tokenr   )
Connection)Zauthurluserkey   N)r@   Zswiftclient.clientr_   r2   Zget_auth)r   r   r]   r^   Zswift_auth_v1_urlZ
swift_userZ	swift_keyr_   r   r   r   r3     s    1
zSwiftSession.__init__c                 C   s   ddh | S )r   ZSWIFT_STORAGE_URLZSWIFT_AUTH_TOKENrA   r   r   r   r   r     s    zSwiftSession.hascredsc                 C   s   | j S rX   rW   r   r   r   r   r(   $  s    zSwiftSession.credentialsc                 C   s   dd | j  D S )r   c                 S   s   i | ]\}}|  |qS r   rI   rK   r   r   r   rO   1  rP   z7SwiftSession.get_credential_options.<locals>.<dictcomp>rY   r   r   r   r   r   )  s    z#SwiftSession.get_credential_options)NNNNNNrS   r   r   r   r   r!     s         
D

r!   c                   @   s:   e Zd ZdZdddZedd Zedd	 Zd
d Z	dS )r"   zNConfigures access to secured resources stored in Microsoft Azure Blob Storage.NFc                 C   sV   t td|| _td|| _|r0d|i| _n"| jsF| j|d| _nd| ji| _dS )a  Create new Microsoft Azure Blob Storage session

        Parameters
        ----------
        azure_storage_connection_string: string
            A connection string contains both an account name and a secret key.
        azure_storage_account: string
            An account name
        azure_storage_access_key: string
            A secret key
        azure_unsigned : bool, optional (default: False)
            If True, requests will be unsigned.

        AZURE_NO_SIGN_REQUESTAZURE_STORAGE_ACCOUNTazure_storage_connection_string)azure_storage_accountazure_storage_access_keyrf   N)r;   r<   r=   r>   storage_accountr@   )r   re   rf   rg   Zazure_unsignedr   r   r   r3   7  s    
zAzureSession.__init__c                 C   s,   d|v p*ddh | p*ddh | S )r   ZAZURE_STORAGE_CONNECTION_STRINGrd   ZAZURE_STORAGE_ACCESS_KEYrc   rA   r   r   r   r   r   [  s    zAzureSession.hascredsc                 C   s   | j S rX   rW   r   r   r   r   r(   u  s    zAzureSession.credentialsc                 C   s*   | j rd| jdS dd | j D S dS )r   rH   )rc   rd   c                 S   s   i | ]\}}|  |qS r   rI   rK   r   r   r   rO     rP   z7AzureSession.get_credential_options.<locals>.<dictcomp>N)r>   rh   r(   rQ   r   r   r   r   r   z  s
    z#AzureSession.get_credential_options)NNNFrS   r   r   r   r   r"   4  s       
$

r"   )r/   loggingr<   Z
fiona.pathr   r   	getLoggerr,   r   r   ImportErrordebugr   r   r   rU   r   r!   r"   r   r   r   r   <module>   s"   


 1+s2:i