o
    €Ï<	  ã                   @   sd   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	Z	G dd„ de	j
jƒZdS )	z=Protocol adapter class to allow requests to GET file:// URLs.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úfilesc                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )ÚLocalFileAdapterz7Protocol Adapter to allow Requests to GET file:// URLs.c                 C   sn   |   ¡ dvrtjjdfS tj |¡rtjjdfS tj |¡s$tjj	dfS t 
|tj¡s1tjjdfS tjjdfS )z4Return an HTTP status for the given filesystem path.)ÚgetÚheadzMethod Not AllowedzPath Not A FilezFile Not FoundzAccess DeniedÚOK)ÚlowerÚrequestsÚcodesÚnot_allowedÚosÚpathÚisdirÚbad_requestÚisfileÚ	not_foundÚaccessÚR_OKÚ	forbiddenÚok)Úmethodr   © r   úC/tmp/google-cloud-sdk/lib/googlecloudsdk/core/local_file_adapter.pyÚ_chkpath   s   zLocalFileAdapter._chkpathc              
   K   sª   t  |j¡}t ¡ }|  |j|¡\|_|_|jdkrI|j 	¡ dkrIzt
 t  |¡¡|_W n ttfyH } zd|_t|ƒ|_W Y d}~nd}~ww |j|_||_| |_|S )zÔReturn the file specified by the given request.

    Args:
      req: PreparedRequest
      **kwargs: kwargs can include values for headers, timeout, stream, etc.

    Returns:
      requests.Response object
    éÈ   r   iô  N)r   ÚNormalizePathFromURLÚpath_urlr   ÚResponser   r   Ústatus_codeÚreasonr
   ÚioÚBytesIOÚReadBinaryFileContentsÚrawÚOSErrorÚIOErrorÚstrÚurlÚrequestÚ
connection)ÚselfÚreqÚkwargsr   ÚresponseÚerrr   r   r   Úsend-   s   
€þzLocalFileAdapter.sendN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r1   r   r   r   r   r      s
    
r   )r5   Ú
__future__r   r   r   r"   r   Úgooglecloudsdk.core.utilr   r   ÚadaptersÚBaseAdapterr   r   r   r   r   Ú<module>   s   