B
    `                 @   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)StoreKey)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allz4send failed|all info to bpaste.net pastebin service.)metavaractiondestdefaultchoiceshelp)ZgetgroupZ
_addoption)r
   group r   X/home/kop/projects/devel/pgwui/test_venv/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_storepastebinfile_keywrite)skwargs)r   oldwriter   r   	tee_write+   s    

z#pytest_configure.<locals>.tee_write)
optionr   pluginmanager	getplugintempfile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    



r8   )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|}|rd
||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://bpaste.netascii)datazutf-8zbad response: %szhref="/raw/(\w+)"z
{}/show/{}   zbad response: invalid format ('z'))reurllib.requestr:   urllib.parser;   r   r1   decodeOSErrorsearchformatr   )	r9   rB   r:   r;   paramsurlresponseexc_infomr   r   r   r5   E   s    $r5   )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   statsr4   ZlongreprZreprtracebackZreprentriesZreprfilelocAttributeErrorZ_getfailureheadliner   r   Z
toterminalgetvaluelenAssertionErrorr5   r6   )r   repmsgfileZtwr#   r7   r   r   r   pytest_terminal_summary^   s     

rW   )__doc__r*   ior   typingr   r   ZpytestZ_pytest.configr   r   Z_pytest.config.argparsingr   Z_pytest.storer   Z_pytest.terminalr	   bytesr!   r   Zhookimplr.   r8   r   r5   rW   r   r   r   r   <module>   s"   
