B
    Wcm                 @   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modeZstorepastebinfailedallz3Send failed|all info to bpaste.net pastebin service)metavaractiondestdefaultchoiceshelp)ZgetgroupZ
_addoption)r
   group r   /lib/python3.7/site-packages/_pytest/pastebin.pypytest_addoption   s    
r   T)Ztrylast)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 )Nzutf-8)
isinstancestrencodestashpastebinfile_keywrite)skwargs)r   oldwriter   r   	tee_write+   s    

z#pytest_configure.<locals>.tee_write)
optionr   pluginmanager	getplugintempfileZTemporaryFiler   r    _twr!   )r   trr%   r   )r   r$   r   pytest_configure   s    r,   c             C   sr   t | jkrn| jt  }|d | }|  | jt = | jd}|jjd= |	dd t
|}|d|  d S )Nr   r   r!   =z$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    



r6   )contentsr   c       	   
   C   s   ddl }ddlm} ddlm} | ddd}d}y$||||d	d
 d}W n& tk
rz } zd| 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)urlopen)	urlencodetextZ1week)codeZlexerZexpiryzhttps://bpa.stascii)datazutf-8zbad response: %szhref="/raw/(\w+)"z/show/   zbad response: invalid format ('z'))reZurllib.requestr8   Zurllib.parser9   r   r/   decodeOSErrorsearchr   )	r7   r?   r8   r9   ZparamsZurlZresponseexc_infomr   r   r   r3   E   s    $r3   )r   r   c          	   C   s   | j jjdkrd S d| jkr| dd x| jd D ]}y|jjjd j}W n t	k
rj   | 
|}Y nX t }t| j |}|| | }t|stt|}| | d|  q4W d S )Nr   r-   z$Sending information to Paste Servicez --> )r   r&   r   Zstatsr2   ZlongreprZreprtracebackZreprentriesZreprfilelocAttributeErrorZ_getfailureheadliner   r   Z
toterminalgetvaluelenAssertionErrorr3   r4   )r   ZrepmsgfileZtwr"   r5   r   r   r   pytest_terminal_summary^   s     

rL   )__doc__r)   ior   typingr   r   ZpytestZ_pytest.configr   r   Z_pytest.config.argparsingr   Z_pytest.stashr   Z_pytest.terminalr	   bytesr    r   Zhookimplr,   r6   r   r3   rL   r   r   r   r   <module>   s"   
