a
    dn0                     @   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headersZrequested_from_user_agentpd	DataFrame)selfresponse_df r   h/var/www/html/stable-diffusion-webui/venv/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_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                   @   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_GETD   s
    zCSVUserAgentResponder.do_GETNr   r   r    r-   r   r   r   r   r"   C   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-   O   s    
z#GzippedCSVUserAgentResponder.do_GETNr.   r   r   r   r   r/   N   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   [   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-   g   s    
z$GzippedJSONUserAgentResponder.do_GETNr.   r   r   r   r   r4   f   s   r4   c                   @   s   e Zd Zdd ZdS )HTMLUserAgentResponderc                 C   s<   |   }| dd |   |jddd}| | d S )Nr#   z	text/htmlFr%   r'   )r   r(   r)   Zto_htmlr+   r   r,   r   r   r   r-   t   s
    zHTMLUserAgentResponder.do_GETNr.   r   r   r   r   r5   s   s   r5   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
    z'ParquetPyArrowUserAgentResponder.do_GETNr.   r   r   r   r   r6   ~   s   r6   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#   r7   r   z'memory://fastparquet_user_agent.parquetFfastparquet)r&   r9   compressionrb)r   r(   r)   fsspecr:   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#   r7   )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   rC      s   rC   c                   @   s   e Zd Zdd ZdS )StataUserAgentResponderc                 C   sF   |   }| dd |   t }|j|dd | }| | d S )Nr#   r7   F)Zwrite_index)r   r(   r)   r   Zto_statar   r   rD   r   r   r   r-      s    zStataUserAgentResponder.do_GETNr.   r   r   r   r   rE      s   rE   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   rF      s   rF   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   )rR   rO   rP   rS   r   rQ   r   wait_until_ready   s    rT   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portrW   r   r   r   process_server   s    &r\   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|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.
    )rU   r      N)targetrO   
      r   rH   )socketbindgetsocknamemultiprocessingProcessr\   paramstartjoin	terminateis_alivekillrL   rM   close)requestsockr[   Zserver_processZ	kill_timeZ	wait_timer   r   r   rZ      s&    


*

rZ   z&responder, read_method, parquet_enginec                  O   s   t j| i |d S Nr   r   Z	read_htmlrN   r   r   r   <lambda>      rq   r8   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:)r9   )pytestimportorskiprT   empty)rZ   read_methodparquet_enginedf_httpr   r   r   test_server_and_default_headers   s    

rz   c                  O   s   t j| i |d S ro   rp   rN   r   r   r   rq   .  rr   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   rs   r   storage_optionsr}   r9   )rt   ru   r   r   rT   tmassert_frame_equal)rZ   rw   rx   custom_user_agentdf_truery   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   ZAuthrs   r|   01c                    s   g | ]} | qS r   r   ).0kr|   r   r   
<listcomp>t  rr   z6test_server_and_all_custom_headers.<locals>.<listcomp>)r   r   r&   r]   )Zaxis)rT   isinkeysZsort_valuesZreset_indexlistr   r   Zdropr   r   )rZ   rw   r   Zcustom_auth_tokenry   r   r   r   r|   r   "test_server_and_all_custom_headers[  s$    r   r9   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.parquetr~   )rt   ru   r   r   Zraises
ValueErrorr:   )r9   r
   Ztrue_dfmsgr   r   r   ,test_to_parquet_to_disk_with_storage_options{  s    	
r   )8r!   r   Zhttp.serverrV   ior   rd   ra   rL   urllib.errorrI   rt   Zpandas.compatr   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandasr   Zpandas._testingZ_testingr   markZ
single_cpuZskipifZ
pytestmarkrW   ZBaseHTTPRequestHandlerr   r"   r/   r1   r4   r5   r6   r;   rC   rE   rF   rT   r\   ZfixturerZ   ZparametrizeZread_csv	read_jsonZread_parquetrf   Z&skip_array_manager_not_yet_implementedZread_pickleZ
read_statarz   r   r   r   r   r   r   r   <module>   s   	&



	







	





