a
    ˆÏ?em  ã                   @   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   úS/home/yuhw/wc/wire-cell-python/wcpy/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 )z›Create 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|ƒs˜J ‚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"   
