B
    ܇\]                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZdd Z	ej
dd	d
d Zdd Zdd Zdd ZdS )zC submit failure or test session information to a pastebin service.     )absolute_import)division)print_functionNc          	   C   s*   |  d}|jddddd ddgdd	 d S )
Nzterminal reportingz
--pastebinmodeZstorepastebinfailedallz4send failed|all info to bpaste.net pastebin service.)metavaractiondestdefaultchoiceshelp)ZgetgroupZ
_addoption)parsergroup r   /lib/python3.7/site-packages/_pytest/pastebin.pypytest_addoption   s    
r   T)Ztrylastc                sN    j jdkrJ jd}|d k	rJtd _|jj fdd}||j_d S )Nr   terminalreporterzw+bc                s2   | f| t | tjr"| d}  j|  d S )Nzutf-8)
isinstancesixZ	text_typeencode_pastebinfilewrite)skwargs)configoldwriter   r   	tee_write'   s    
z#pytest_configure.<locals>.tee_write)	optionr   pluginmanager	getplugintempfileZTemporaryFiler   _twr   )r   trr   r   )r   r   r   pytest_configure   s    r%   c             C   sj   t | drf| jd | j }| j  | `| jd}|jjd= |	dd t
|}|d|  d S )Nr   r   r   r   =z$Sending information to Paste Servicezpastebin session-log: %s
)hasattrr   seekreadcloser    r!   r#   __dict__	write_sepcreate_new_paste
write_line)r   Z
sessionlogr$   pastebinurlr   r   r   pytest_unconfigure0   s    



r0   c             C   s   ddl }tjdk r$ddlm}m} nddlm} ddlm} | tjd dkrPdnd	d
d}d}||||dd	 }|
d|d}|rd||df S d| S dS )z
    Creates a new paste using bpaste.net service.

    :contents: paste contents as utf-8 encoded bytes
    :returns: url to the pasted contents
    r   N)   r   )urlopen	urlencode)r2   )r3   r1   Zpython3ZpythonZ1week)codeZlexerZexpiryzhttps://bpaste.netascii)datazhref="/raw/(\w+)"zutf-8z
%s/show/%s   zbad response: )resysversion_infoZurllibr2   r3   Zurllib.requestZurllib.parser   r)   searchdecoder   )contentsr8   r2   r3   ZparamsZurlZresponsemr   r   r   r-   @   s    
r-   c          	   C   s   dd l }| 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
rx   ||}Y nX |jj| jdd}|| |j }t|stt|}|d||f  qBW d S )	Nr   r   r&   z$Sending information to Paste ServiceT)stringioz	%s --> %s)Z_pytest.configr   r   r   Zstatsr,   getZlongreprZreprtracebackZreprentriesZreprfilelocAttributeErrorZ_getfailureheadlineZcreate_terminal_writerZ
toterminalr@   getvaluelenAssertionErrorr-   r.   )r   Z_pytestr$   ZrepmsgZtwr   r/   r   r   r   pytest_terminal_summary]   s$    


rG   )__doc__Z
__future__r   r   r   r9   r"   r   Zpytestr   Zhookimplr%   r0   r-   rG   r   r   r   r   <module>   s   