o
    ҷhn0                     @   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jje ddg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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"d$d% dfe#ej/d&fej0e$ej/d'ej1gd(e%ej2dfe&ej3dfeej-dfe!ej.dfg	d"gd)d*d+ Z4ejj,d#eej-dfe ej.dfe"d,d% dfe#ej/d&fej0e$ej/d'ej1gd(e%ej2dfe&ej3dfeej-dfe!ej.dfg	d"gd)d-d. Z5ejj,d/e'ej-fgd"gd)d0d1 Z6ej,d2d&d'gd3d4 Z7dS )5z9
Tests for the pandas custom headers in http(s) requests
    N)BytesIO)is_ci_environmentz9GH 45651: This test can hang in our CI min_versions build)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headersrequested_from_user_agentpd	DataFrame)selfresponse_df r   R/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/io/test_user_agent.pystart_processing_headers%   s   
z/BaseUserAgentResponder.start_processing_headersc              	   C   sl   t  )}tj|dd}|| W d   n1 sw   Y  | }W d   |S 1 s/w   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biozipperr   r   r   
gzip_bytes2   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                   @      e Zd Zdd ZdS )CSVUserAgentResponderc                 C   <   |   }| 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_GETD   s
   zCSVUserAgentResponder.do_GETNr    r!   r"   r1   r   r   r   r   r%   C       r%   c                   @   r$   )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   r0   r   r   r   r1   O   s   
z#GzippedCSVUserAgentResponder.do_GETNr2   r   r   r   r   r4   N   r3   r4   c                   @   r$   )JSONUserAgentResponderc                 C   s8   |   }| dd |   | d}| | d S )Nr'   application/jsonr+   )r   r,   r-   to_jsonr/   r   r0   r   r   r   r1   \   
   zJSONUserAgentResponder.do_GETNr2   r   r   r   r   r6   [   r3   r6   c                   @   r$   )GzippedJSONUserAgentResponderc                 C   sN   |   }| dd | dd |   | d}| |}| | d S )Nr'   r7   r5   r   r+   )r   r,   r-   r8   r/   r   r   r0   r   r   r   r1   g   s   
z$GzippedJSONUserAgentResponder.do_GETNr2   r   r   r   r   r:   f   r3   r:   c                   @   r$   )HTMLUserAgentResponderc                 C   r&   )Nr'   z	text/htmlFr)   r+   )r   r,   r-   to_htmlr/   r   r0   r   r   r   r1   t   s
   zHTMLUserAgentResponder.do_GETNr2   r   r   r   r   r;   s   r3   r;   c                   @   r$   ) ParquetPyArrowUserAgentResponderc                 C   s8   |   }| dd |   |jddd}| | d S )Nr'   application/octet-streamFpyarrow)r*   engine)r   r,   r-   
to_parquetr   r0   r   r   r   r1      r9   z'ParquetPyArrowUserAgentResponder.do_GETNr2   r   r   r   r   r=   ~   r3   r=   c                   @   r$   )$ParquetFastParquetUserAgentResponderc                 C   sx   |   }| dd |   dd l}|jdddd d |dd}| }W d    n1 s0w   Y  | | d S )	Nr'   r>   r   z'memory://fastparquet_user_agent.parquetFfastparquet)r*   r@   compressionrb)r   r,   r-   fsspecrA   openreadr   )r   r   rF   fr   r   r   r   r1      s   
z+ParquetFastParquetUserAgentResponder.do_GETNr2   r   r   r   r   rB      r3   rB   c                   @   r$   )PickleUserAgentResponderc                 C   sB   |   }| dd |   t }|| | }| | d S )Nr'   r>   )r   r,   r-   r   	to_pickler   r   r   r   r   r   r   r   r   r1      s   
zPickleUserAgentResponder.do_GETNr2   r   r   r   r   rJ      r3   rJ   c                   @   r$   )StataUserAgentResponderc                 C   sF   |   }| dd |   t }|j|dd | }| | d S )Nr'   r>   F)write_index)r   r,   r-   r   to_statar   r   rL   r   r   r   r1      s   zStataUserAgentResponder.do_GETNr2   r   r   r   r   rM      r3   rM   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   r0   r   r   r   r1      s   
zAllHeaderCSVResponder.do_GETN)r    r!   r"   r#   r1   r   r   r   r   rP      s    rP   c                    s    fdd}|S )Nc                     s6   	 z | i |W S  t jjy   td Y nw q)NT皙?)urlliberrorURLErrortimesleepargskwargsfuncr   r   inner   s   zwait_until_ready.<locals>.innerr   )r\   rY   rZ   r]   r   r[   r   wait_until_ready   s   r^   c                 C   sF   t jd|f| }|  W d    n1 sw   Y  |  d S )N	localhost)httpserver
HTTPServerhandle_requestserver_close)	responderportra   r   r   r   process_server   s   
rg   c                 c   s    t   }|d | d }W d   n1 sw   Y  tjt| j|fd}|  |V  |d |	  d}d}|
 rX||krK|  n|d7 }td |
 sB|  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.
    )r_   r      N)targetrY   
      r   rR   )socketbindgetsocknamemultiprocessingProcessrg   paramstartjoin	terminateis_alivekillrV   rW   close)requestsockrf   server_process	kill_time	wait_timer   r   r   re      s,   





re   z&responder, read_method, parquet_enginec                  O      t j| i |d S Nr   r   	read_htmlrX   r   r   r   <lambda>      r   r?   rC   )marks)indirectc                 C   sd   |d urt | |dkrt d t|}|d u r"|d|  }n	|d|  |d}|jr0J d S )NrC   rF   http://localhost:)r@   )pytestimportorskipr^   empty)re   read_methodparquet_enginedf_httpr   r   r   test_server_and_default_headers   s   

r   c                  O   r}   r~   r   rX   r   r   r   r   .  r   c                 C   s   |d urt | |dkrt d d}td|gi}t|}|d u r0|d|  d|id}n|d|  d|i|d}t|| d S )	NrC   rF   Super Cool Oner   r   r   storage_optionsr   r@   )r   r   r   r   r^   tmassert_frame_equal)re   r   r   custom_user_agentdf_truer   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 )Nr   zSuper Secret Oner   Authr   r   01c                    s   g | ]} | qS r   r   ).0kr   r   r   
<listcomp>t  r   z6test_server_and_all_custom_headers.<locals>.<listcomp>)r   r   r*   rh   )axis)r^   isinkeyssort_valuesreset_indexlistr   r   dropr   r   )re   r   r   custom_auth_tokenr   r   r   r   r   r   "test_server_and_all_custom_headers[  s$   r   r@   c                 C   sl   ddd}t |  tddgi}d}t jt|d |jd|| d	 W d    d S 1 s/w   Y  d S )
Ncustomother_customr   column_namecolumn_valuezxstorage_options passed with file object or non-fsspec file path|storage_options passed with buffer, or non-supported URL)matchz/tmp/junk.parquetr   )r   r   r   r   raises
ValueErrorrA   )r@   r
   true_dfmsgr   r   r   ,test_to_parquet_to_disk_with_storage_options{  s   	
"r   )8r#   r   http.serverr`   ior   ro   rl   rV   urllib.errorrS   r   pandas.compatr   pandas.util._test_decoratorsutil_test_decoratorstdpandasr   pandas._testing_testingr   mark
single_cpuskipif
pytestmarkra   BaseHTTPRequestHandlerr   r%   r4   r6   r:   r;   r=   rB   rJ   rM   rP   r^   rg   fixturere   parametrizeread_csv	read_jsonread_parquetrq   &skip_array_manager_not_yet_implementedread_pickle
read_statar   r   r   r   r   r   r   r   <module>   s    	&




	







	




