a
    nÝEb<.  ã                   @   s„  d Z ddlZddlZddl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 ddlm  mZ ddlZddlmZ ejjeƒ dd�ZG dd„ dejjƒ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"G dd„ deƒZ#G dd„ deƒZ$G dd„ dejjƒZ%dd„ Z&dd„ Z'ej(dd „ ƒZ)ejj*d!eej+dfeej,dfe!ej-d"fej.e"ej-d#ej/gd$�e#ej0dfe$ej1dfeej+dfe ej,dfgd gd%�d&d'„ ƒZ2ejj*d!eej+dfeej,dfe!ej-d"fej.e"ej-d#ej/gd$�e#ej0dfe$ej1dfeej+dfe ej,dfgd gd%�d(d)„ ƒZ3ejj*d*e%ej+fgd gd%�d+d,„ ƒZ4ej *d-d"d#g¡d.d/„ ƒZ5dS )0z9
Tests for the pandas custom headers in http(s) requests
é    N)ÚBytesIO)Úis_ci_environmentz…This test can hang in our CI min_versions build and leads to '##[error]The runner has received a shutdown signal...' in GHA. GH 45651)Úreasonc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBaseUserAgentResponderzü
    Base class for setting up a server that can be set up to respond
    with a particular file format with accompanying content-type headers.
    The interfaces on the different io methods are different enough
    that this seemed logical to do.
    c                 C   s,   |   d¡ | jd | _t d| jgi¡}|S )z<
        shared logic at the start of a GET request
        éÈ   ú
User-AgentÚheader)Úsend_responseÚheadersZrequested_from_user_agentÚpdÚ	DataFrame)ÚselfÚresponse_df© r   ú_/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/pandas/tests/io/test_user_agent.pyÚstart_processing_headers$   s    
ÿÿz/BaseUserAgentResponder.start_processing_headersc              	   C   sj   t ƒ �P}tj|dd��}| |¡ W d  ƒ n1 s60    Y  | ¡ }W d  ƒ n1 s\0    Y  |S )zQ
        some web servers will send back gzipped files to save bandwidth
        Úw)ÚfileobjÚmodeN)r   ÚgzipÚGzipFileÚwriteÚgetvalue)r   Úresponse_bytesÚbioZzipperr   r   r   Ú
gzip_bytes1   s
    (&z!BaseUserAgentResponder.gzip_bytesc                 C   s   | j  |¡ dS )z:
        shared logic at the end of a GET request
        N)Úwfiler   )r   r   r   r   r   Úwrite_back_bytes;   s    z'BaseUserAgentResponder.write_back_bytesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   
r   c                   @   s   e Zd Zdd„ ZdS )ÚCSVUserAgentResponderc                 C   s<   |   ¡ }|  dd¡ |  ¡  |jdd� d¡}|  |¡ d S )NúContent-Typeútext/csvF©Úindexúutf-8)r   Úsend_headerÚend_headersÚto_csvÚencoder   ©r   r   r   r   r   r   Údo_GETC   s
    zCSVUserAgentResponder.do_GETN©r   r   r    r-   r   r   r   r   r"   B   s   r"   c                   @   s   e Zd Zdd„ ZdS )ÚGzippedCSVUserAgentResponderc                 C   sR   |   ¡ }|  dd¡ |  dd¡ |  ¡  |jdd� d¡}|  |¡}|  |¡ d S )Nr#   r$   úContent-Encodingr   Fr%   r'   )r   r(   r)   r*   r+   r   r   r,   r   r   r   r-   N   s    
z#GzippedCSVUserAgentResponder.do_GETNr.   r   r   r   r   r/   M   s   r/   c                   @   s   e Zd Zdd„ ZdS )ÚJSONUserAgentResponderc                 C   s8   |   ¡ }|  dd¡ |  ¡  | ¡  d¡}|  |¡ d S )Nr#   úapplication/jsonr'   )r   r(   r)   Úto_jsonr+   r   r,   r   r   r   r-   [   s
    zJSONUserAgentResponder.do_GETNr.   r   r   r   r   r1   Z   s   r1   c                   @   s   e Zd Zdd„ ZdS )ÚGzippedJSONUserAgentResponderc                 C   sN   |   ¡ }|  dd¡ |  dd¡ |  ¡  | ¡  d¡}|  |¡}|  |¡ d S )Nr#   r2   r0   r   r'   )r   r(   r)   r3   r+   r   r   r,   r   r   r   r-   f   s    
z$GzippedJSONUserAgentResponder.do_GETNr.   r   r   r   r   r4   e   s   r4   c                   @   s   e Zd Zdd„ ZdS )Ú ParquetPyArrowUserAgentResponderc                 C   s8   |   ¡ }|  dd¡ |  ¡  |jddd�}|  |¡ d S )Nr#   úapplication/octet-streamFÚpyarrow)r&   Úengine)r   r(   r)   Ú
to_parquetr   r,   r   r   r   r-   s   s
    z'ParquetPyArrowUserAgentResponder.do_GETNr.   r   r   r   r   r5   r   s   r5   c                   @   s   e Zd Zdd„ ZdS )Ú$ParquetFastParquetUserAgentResponderc                 C   sx   |   ¡ }|  dd¡ |  ¡  dd l}|jdddd d� | dd¡�}| ¡ }W d   ƒ n1 s`0    Y  |  |¡ d S )	Nr#   r6   r   z'memory://fastparquet_user_agent.parquetFÚfastparquet)r&   r8   ÚcompressionÚrb)r   r(   r)   Úfsspecr9   ÚopenÚreadr   )r   r   r>   Úfr   r   r   r   r-   ~   s    ü&z+ParquetFastParquetUserAgentResponder.do_GETNr.   r   r   r   r   r:   }   s   r:   c                   @   s   e Zd Zdd„ ZdS )ÚPickleUserAgentResponderc                 C   sB   |   ¡ }|  dd¡ |  ¡  tƒ }| |¡ | ¡ }|  |¡ d S )Nr#   r6   )r   r(   r)   r   Z	to_pickler   r   ©r   r   r   r   r   r   r   r-   ˜   s    
zPickleUserAgentResponder.do_GETNr.   r   r   r   r   rB   —   s   rB   c                   @   s   e Zd Zdd„ ZdS )ÚStataUserAgentResponderc                 C   sF   |   ¡ }|  dd¡ |  ¡  tƒ }|j|dd� | ¡ }|  |¡ d S )Nr#   r6   F)Zwrite_index)r   r(   r)   r   Zto_statar   r   rC   r   r   r   r-   ¥   s    zStataUserAgentResponder.do_GETNr.   r   r   r   r   rD   ¤   s   rD   c                   @   s   e Zd ZdZdd„ ZdS )ÚAllHeaderCSVResponderz?
    Send all request headers back for checking round trip
    c                 C   sP   t  | j ¡ ¡}|  d¡ |  dd¡ |  ¡  |jdd� d¡}| j	 
|¡ d S )Nr   r#   r$   Fr%   r'   )r   r   r
   Úitemsr	   r(   r)   r*   r+   r   r   r,   r   r   r   r-   ¶   s    
zAllHeaderCSVResponder.do_GETN)r   r   r    r!   r-   r   r   r   r   rE   ±   s   rE   c                    s   ‡ fdd„}|S )Nc                     s8   zˆ | i |¤ŽW S  t jjy0   t d¡ Y q 0 q d S )Nçš™™™™™¹?)ÚurllibÚerrorÚURLErrorÚtimeÚsleep)ÚargsÚkwargs©Úfuncr   r   ÚinnerÀ   s    zwait_until_ready.<locals>.innerr   )rP   rM   rN   rQ   r   rO   r   Úwait_until_ready¿   s    rR   c                 C   sF   t j d|f| ¡�}| ¡  W d   ƒ n1 s00    Y  | ¡  d S )NÚ	localhost)ÚhttpÚserverZ
HTTPServerÚhandle_requestÚserver_close)Ú	responderÚportrU   r   r   r   Úprocess_serverË   s    &rZ   c                 c   s¶   t   ¡ �&}| d¡ | ¡ d }W d  ƒ n1 s40    Y  tjt| j|fd�}| ¡  |V  | d¡ | 	¡  d}d}| 
¡ rª||kr–| ¡  qªqz|d7 }t d¡ qz| ¡  dS )	zã
    Fixture that starts a local http server in a separate process on localhost
    and returns the port.

    Running in a separate process instead of a thread to allow termination/killing
    of http server upon cleanup.
    )rS   r   é   N)ÚtargetrM   é
   é   r   rG   )ÚsocketÚbindÚgetsocknameÚmultiprocessingÚProcessrZ   ÚparamÚstartÚjoinÚ	terminateÚis_aliveÚkillrK   rL   Úclose)ÚrequestÚsockrY   Zserver_processZ	kill_timeZ	wait_timer   r   r   rX   Ñ   s&    


*
ÿ
rX   z&responder, read_method, parquet_enginer7   r;   )Zmarks)Zindirectc                 C   sd   |d ur$t  |¡ |dkr$t  d¡ t|ƒ}|d u rD|d| › �ƒ}n|d| › �|d�}|jr`J ‚d S )Nr;   r>   úhttp://localhost:)r8   )ÚpytestÚimportorskiprR   Úempty)rX   Úread_methodÚparquet_engineÚdf_httpr   r   r   Útest_server_and_default_headersò   s    

rt   c                 C   sˆ   |d ur$t  |¡ |dkr$t  d¡ d}t d|gi¡}t|ƒ}|d u r`|d| › �d|id�}n|d| › �d|i|d�}t ||¡ d S )	Nr;   r>   úSuper Cool Oner   rm   r   ©Ústorage_options©rw   r8   )rn   ro   r   r   rR   ÚtmÚassert_frame_equal)rX   rq   rr   Úcustom_user_agentÚdf_truers   r   r   r   Útest_server_and_custom_headers  s$    

þýr}   zresponder, read_methodc                    s¸   d}d}||dœ‰ t |ƒ}|d| › �ˆ d�}||d  ˆ  ¡ ¡ }| dg¡ ¡ }|ddg }tˆ  ¡ ƒ}t |‡ fdd	„|D ƒd
œ¡}| dg¡}| ¡ jdgdd�}t	 
||¡ d S )Nru   zSuper Secret One©r   ZAuthrm   rv   Ú0Ú1c                    s   g | ]}ˆ | ‘qS r   r   )Ú.0Úkrv   r   r   Ú
<listcomp>_  ó    z6test_server_and_all_custom_headers.<locals>.<listcomp>)r   r€   r&   r[   )Zaxis)rR   ÚisinÚkeysZsort_valuesZreset_indexÚlistr   r   Zdropry   rz   )rX   rq   r{   Zcustom_auth_tokenrs   r†   r|   r   rv   r   Ú"test_server_and_all_custom_headersF  s$    þþrˆ   r8   c                 C   sj   dddœ}t  | ¡ t ddgi¡}d}t jt|d��  |jd|| d	� W d   ƒ n1 s\0    Y  d S )
NZcustomZother_customr~   Zcolumn_nameZcolumn_valuezxstorage_options passed with file object or non-fsspec file path|storage_options passed with buffer, or non-supported URL)Úmatchz/tmp/junk.parquetrx   )rn   ro   r   r   ZraisesÚ
ValueErrorr9   )r8   r
   Ztrue_dfÚmsgr   r   r   Ú,test_to_parquet_to_disk_with_storage_optionsf  s    	þ
ÿrŒ   )6r!   r   Zhttp.serverrT   Úior   rb   r_   rK   Úurllib.errorrH   rn   Zpandas.compatr   Zpandas.util._test_decoratorsÚutilZ_test_decoratorsÚtdZpandasr   Zpandas._testingZ_testingry   ÚmarkZskipifZ
pytestmarkrU   ZBaseHTTPRequestHandlerr   r"   r/   r1   r4   r5   r:   rB   rD   rE   rR   rZ   ZfixturerX   ZparametrizeZread_csvÚ	read_jsonZread_parquetrd   Z&skip_array_manager_not_yet_implementedZread_pickleZ
read_statart   r}   rˆ   rŒ   r   r   r   r   Ú<module>   s¨   þ&
 


ÿû	



ðì



ÿû	



ðì
ÿû
þþ