a
    ay                     @   st   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d dlmZ G dd deZdS )    )BaseAdapter)urlparseunquote)ResponsecodesN)BytesIOc                       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__ ,lib/python3.9/site-packages/requests_file.pyr      s    zFileAdapter.__init__c              
   K   s@  |j dvrtd|j  t|j}|jr:|jdkr:tdt }zdd |jdD }|rr|d sr|d qZt	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|  }|r>tj|s>tjtjj|g|R   }t|d|_|jj|j_W n t
y } 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 Y d}~nLd}~0 0 tj&|_|j|_t'|j( }
t)*|
j+r<| j#r<|
j,|j%d< |S )z Wraps a file, described in request, in a Response object.

            :param request: The PreparedRequest` being "sent".
            :returns: a Response object containing the file
        )ZGETZHEADzInvalid request method %sZ	localhostz5file: URLs with hostname components are not permittedc                 S   s   g | ]}t |qS r   )r   .0pr   r   r   
<listcomp>,       z$FileAdapter.send.<locals>.<listcomp>/r   c                 s   s   | ]}t j|v V  qd S r	   )ossepr   r   r   r   	<genexpr>4   r   z#FileAdapter.send.<locals>.<genexpr>|:N rbFzContent-Length)-method
ValueErrorr   urlnetlocr   pathsplitpopanyIOErrorerrnoENOENTr   strerrorendswithr   join
splitdriveioopenrawcloseZrelease_connZEACCESr   Z	forbiddenZstatus_codeZ	not_foundZbad_requeststrencodelocalegetpreferredencodingr   r   lenZheadersokfstatfilenostatS_ISREGst_modest_size)r   ZrequestkwargsZ	url_partsZresp
path_parts
path_driver$   eZresp_strZ	resp_statr   r   r   send   sZ    






"zFileAdapter.sendc                 C   s   d S r	   r   )r   r   r   r   r2   t   s    zFileAdapter.close)T)__name__
__module____qualname__r   rC   r2   __classcell__r   r   r   r   r      s   br   )Zrequests.adaptersr   Zrequests.compatr   r   Zrequestsr   r   r)   r   r;   r5   r/   Zsixr   r   r   r   r   r   <module>   s   