U
    42i                     @  s   d 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 dd
lmZ ddlZeee   ZdddddZejdddddddZdddddZdddddZddddd ZdS )!zASubmit failure or test session information to a pastebin service.    )annotations)StringION)IO)Config)create_terminal_writer)Parser)StashKey)TerminalReporterr   None)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   4/tmp/pip-unpacked-wheel-7n2p7kht/_pytest/pastebin.pypytest_addoption   s    
r   T)Ztrylastr   )configr   c                   sR    j jdkrN jd}|d k	rNtd jt< |jj	 fdd}||j_	d S )Nr   terminalreporterzw+bc                   s4   | f| 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    r0   c                 C  sv   t | jkrr| jt  }|d | }|  | jt = | jd}|jjd= |	dd t
|}|d| d d S )Nr   r   r$   =$Sending information to Paste Servicezpastebin session-log: 
)r#   r"   seekreadcloser*   r+   r.   __dict__	write_sepcreate_new_paste
write_line)r   ZpastebinfileZ
sessionlogr/   pastebinurlr   r   r   pytest_unconfigure7   s    



r<   zstr | bytesr    )contentsr   c           	   
   C  s   ddl }ddlm} ddlm} | ddd}d}z$||||d	d
 d}W n0 tk
r } zd|  W Y S d}~X Y nX |	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)	urlencode)urlopentextZ1week)codelexerZexpiryzhttps://bpa.stascii)datar   zbad response: zhref="/raw/(\w+)"z/show/   zbad response: invalid format ('z'))reurllib.parser>   urllib.requestr?   r!   r5   decodeOSErrorsearchr   )	r=   rF   r>   r?   paramsurlresponseexc_infomr   r   r   r9   H   s     r9   r	   )r   r   c              	   C  s   | j jjdkrd S d| jkr| dd | jd D ]}z|jjjd j}W n t	k
rh   | 
|}Y nX t }t| j |}|| | }t|stt|}| | d|  q2d S )Nr   r1   r2   z --> )r   r)   r   statsr8   ZlongreprZreprtracebackZreprentriesZreprfilelocAttributeErrorZ_getfailureheadliner   r   Z
toterminalgetvaluelenAssertionErrorr9   r:   )r   repmsgfileZtwr%   r;   r   r   r   pytest_terminal_summarya   s     

rZ   )__doc__
__future__r   ior   r,   typingr   Z_pytest.configr   r   Z_pytest.config.argparsingr   Z_pytest.stashr   Z_pytest.terminalr	   Zpytestbytesr#   r   Zhookimplr0   r<   r9   rZ   r   r   r   r   <module>   s"   
