U
    H4f                     @   s   d dl mZ d dlmZmZ d dlmZmZ d dlZd dl	Z	d dl
Z
d dlZd dlZzd dlmZ W n  ek
r   d dlmZ Y nX G dd deZdS )	    )BaseAdapter)urlparseunquote)ResponsecodesN)BytesIO)StringIOc                       s.   e Zd Zd fdd	Zdd Zdd Z  ZS )	FileAdapterTc                    s   t t|   || _d S N)superr	   __init___set_content_length)selfZset_content_length	__class__ 1/tmp/pip-unpacked-wheel-jslftc1l/requests_file.pyr      s    zFileAdapter.__init__c              
   K   sB  |j dkrtd|j  t|j}|jr:|jdkr:tdt }||_zdd |jdD }|rx|d sx|	d q`t
d	d
 |D rttjttj|r|d ds|d dr|	d}|dr|dd d }|r|d s|	d qnd}|tj tjj|  }|rBtj|sBtjtjj|f|  }t|d|_|jj|j_W n tk
r } z||jtjkrtj|_n |jtjkrtj|_ntj|_t| t!"d}	t#|	|_| j$rt%|	|j&d< |jj|j_W 5 d}~X Y nDX tj'|_|j|_t(|j) }
t*+|
j,r>| j$r>|
j-|j&d< |S )zWraps a file, described in request, in a Response object.

        :param request: The PreparedRequest` being "sent".
        :returns: a Response object containing the file
        )GETHEADzInvalid request method %s	localhostz5file: URLs with hostname components are not permittedc                 S   s   g | ]}t |qS r   )r   .0pr   r   r   
<listcomp>0   s     z$FileAdapter.send.<locals>.<listcomp>/r   c                 s   s   | ]}t j|kV  qd S r
   )ossepr   r   r   r   	<genexpr>8   s     z#FileAdapter.send.<locals>.<genexpr>|:N rbFzContent-Length).method
ValueErrorr   urlnetlocr   requestpathsplitpopanyIOErrorerrnoENOENTr   strerrorendswithr   join
splitdriveioopenrawcloserelease_connEACCESr   	forbiddenstatus_code	not_foundbad_requeststrencodelocalegetpreferredencodingr   r   lenheadersokfstatfilenostatS_ISREGst_modest_size)r   r'   kwargsZ	url_partsresp
path_parts
path_driver(   eZresp_strZ	resp_statr   r   r   send   s\    






zFileAdapter.sendc                 C   s   d S r
   r   )r   r   r   r   r6   x   s    zFileAdapter.close)T)__name__
__module____qualname__r   rO   r6   __classcell__r   r   r   r   r	      s   cr	   )Zrequests.adaptersr   Zrequests.compatr   r   requestsr   r   r-   r   rF   r?   r3   r   ImportErrorr   r	   r   r   r   r   <module>   s   