a
    ¨ˆcª  ã                   @   sb   d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 e e¡ZG dd„ de
ƒZdd	„ ZdS )
a   Attach relevant project files.

FIXME: there are no public project plugins for Envisage 3, yet. In any case,
this stuff should not be hard-coded, but extensible via extension points. The
code remains here because we can reuse the zip utility code in that extensible
rewrite.
é    N)Úencoders)ÚMIMEBase)ÚAnyÚ	HasTraitsc                       sH   e Zd Zeƒ Zeƒ Z‡ fdd„Zdd„ Zdd„ Zdd„ Z	d	d
„ Z
‡  ZS )ÚAttachmentsc                    s*   |  ¡ }||d< tt| ƒjf i |¤Ž d S )NÚmessage)ÚcopyÚsuperr   Ú__init__)Úselfr   Ztraits©Ú	__class__© úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/logger/agent/attachments.pyr
   "   s    zAttachments.__init__c                 C   s2   | j d u r
| j  d¡}|d ur.|j}|  |¡ d S )Nzenvisage.project.IWorkspace)ÚapplicationÚget_serviceÚpathÚ_attach_directory)r   Z	workspaceÚdirr   r   r   Úpackage_workspace)   s
    
zAttachments.package_workspacec                 C   s2   | j d u r
| j  d¡}|d ur.|j}|  |¡ d S )Nz$envisage.single_project.ModelService)r   r   Úlocationr   )r   Zsingle_projectr   r   r   r   Úpackage_single_project2   s    
ÿz"Attachments.package_single_projectc                 C   s   |   ¡  |  ¡  d S )N)r   r   )r   r   r   r   Úpackage_any_relevant_files=   s    z&Attachments.package_any_relevant_filesc                 C   s¤   t j |¡}dd l}ddlm} d}| dd¡\}}t||ƒ}|ƒ }	| |	d¡}
t	|
||ƒ |
 
¡  | |	 ¡ ¡ t |¡ |jddd	d
� | j |¡ |	 
¡  d S )Nr   )ÚBytesIOzapplication/octet-streamú/é   ÚwzContent-DispositionÚ
attachmentzproject.zip)Úfilename)Úosr   ÚbasenameÚzipfileÚior   Úsplitr   ÚZipFileÚ_append_to_zip_archiveÚcloseÚset_payloadÚgetvaluer   Úencode_base64Ú
add_headerr   Úattach)r   r   Úrelpathr!   r   ÚctypeÚmaintypeÚsubtypeÚmsgZfile_objectÚzipr   r   r   r   A   s"    

ÿzAttachments._attach_directory)Ú__name__Ú
__module__Ú__qualname__r   r   r   r
   r   r   r   r   Ú__classcell__r   r   r   r   r      s   	r   c                 C   s„   t  |¡D ]t}t j ||¡}t j |¡rRt j ||¡}|  ||¡ t d| ¡ q
|dkr
t j ||¡}t| |t j ||¡ƒ q
dS )z: Add all files in and below directory dir into zip archivezadding %s to error reportz.svnN)	r   Úlistdirr   ÚjoinÚisfileÚwriteÚloggerÚdebugr%   )r1   r   r,   r   r   ÚnameÚsubdirr   r   r   r%   \   s    ÿr%   )Ú__doc__ÚloggingÚos.pathr   Úemailr   Zemail.mime.baser   Z
traits.apir   r   Ú	getLoggerr2   r:   r   r%   r   r   r   r   Ú<module>
   s   
?