a
    !fm                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ eee   ZeddddZejdde	ddddZe	ddddZeeef edddZeddddZdS )zASubmit failure or test session information to a pastebin service.    N)StringIO)IO)Union)Config)create_terminal_writer)Parser)StashKey)TerminalReporter)parserreturnc              	   C   s*   |  d}|jddddd ddgdd	 d S )
Nzterminal reportingz
--pastebinmodestorepastebinfailedallz3Send failed|all info to bpaste.net pastebin service)metavaractiondestdefaultchoiceshelp)ZgetgroupZ
_addoption)r
   group r   Q/var/www/html/python-backend/venv/lib/python3.9/site-packages/_pytest/pastebin.pypytest_addoption   s    
r   T)Ztrylast)configr   c                    sR    j jdkrN jd}|d urNtd jt< |jj	 fdd}||j_	d S )Nr   terminalreporterzw+bc                    s8   | fi | t | tr$| d}  jt |  d S )Nutf-8)
isinstancestrencodestashpastebinfile_keywrite)skwargsr   Zoldwriter   r   	tee_write+   s    

z#pytest_configure.<locals>.tee_write)
optionr   pluginmanager	getplugintempfileTemporaryFiler!   r"   _twr#   )r   trr'   r   r&   r   pytest_configure   s    r/   c                 C   sr   t | jv rn| jt  }|d | }|  | jt = | jd}|jjd= |	dd t
|}|d|  d S )Nr   r   r#   =$Sending information to Paste Servicezpastebin session-log: %s
)r"   r!   seekreadcloser)   r*   r-   __dict__	write_sepcreate_new_paste
write_line)r   ZpastebinfileZ
sessionlogr.   pastebinurlr   r   r   pytest_unconfigure4   s    



r:   )contentsr   c           	   
   C   s   ddl }ddlm} ddlm} | ddd}d}z$||||d	d
 d}W n, ty } zd| W  Y d}~S d}~0 0 |	d|}|r| d|
d S d| d S dS )zCreate a new paste using the bpaste.net service.

    :contents: Paste contents string.
    :returns: URL to the pasted contents, or an error message.
    r   N)urlopen)	urlencodetextZ1week)codelexerZexpiryzhttps://bpa.stascii)datar   zbad response: %szhref="/raw/(\w+)"z/show/   zbad response: invalid format ('z'))reurllib.requestr<   urllib.parser=   r    r3   decodeOSErrorsearchr   )	r;   rD   r<   r=   paramsurlresponseexc_infomr   r   r   r7   E   s    r7   )r   r   c              	   C   s   | j jjdkrd S d| jv r| dd | jd D ]}z|jjjd j}W n t	yf   | 
|}Y n0 t }t| j |}|| | }t|sJ t|}| | d|  q2d S )Nr   r0   r1   z --> )r   r(   r   statsr6   ZlongreprZreprtracebackZreprentriesZreprfilelocAttributeErrorZ_getfailureheadliner   r   Z
toterminalgetvaluelenr7   r8   )r   repmsgfileZtwr$   r9   r   r   r   pytest_terminal_summary^   s     

rW   )__doc__r+   ior   typingr   r   ZpytestZ_pytest.configr   r   Z_pytest.config.argparsingr   Z_pytest.stashr   Z_pytest.terminalr	   bytesr"   r   Zhookimplr/   r:   r   r7   rW   r   r   r   r   <module>   s"   
