a
    §ˆcõN  ã                   @   s8  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 dd	l5m6Z6 dd
l7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m=Z= dZ>G dd„ deƒZ?G dd„ deƒZ@G dd„ de?e@ƒZAe#e?ƒG dd„ deƒƒZBG dd„ deƒZCeCƒ ZDG dd„ deBeCƒZEG dd„ deBƒZFG dd„ deBƒZGG dd „ d eBƒZHG d!d"„ d"e+ƒZIG d#d$„ d$e+ƒZJG d%d&„ d&e+ƒZKd'd(„ ZLd)d*„ ZMeNd+k�r4eOeMeFƒ eGƒ eHƒ gd,d-�ƒ d.S )/zd Defines functions and classes used to create pop-up file dialogs for
    opening and saving files.
é    )ÚR_OKÚW_OKÚaccessÚmkdir)ÚbasenameÚdirnameÚexistsÚgetatimeÚgetctimeÚgetmtimeÚgetsizeÚisdirÚisfileÚjoinÚsplitÚsplitext)Ú	localtimeÚstrftime)ÚBoolÚButtonÚCListÚEventÚFileÚHasPrivateTraitsÚInstanceÚIntÚ	InterfaceÚPropertyÚStrÚcached_propertyÚprovides)Úuser_name_foré   )Ú
CodeEditorÚ
FileEditorÚHGroupÚHSplitÚHandlerÚHistoryEditorÚImageEditorÚInstanceEditorÚItemÚUIInfoÚVGroupÚVSplitÚViewÚspring)ÚAView)ÚImageResource)Údo_later)Ú	commatize)Útoolkiti   c                   @   s   e Zd ZdZeƒ ZdS )ÚIFileDialogModelz+Defines a model extension to a file dialog.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú	file_name© r<   r<   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/file_dialog.pyr6   Y   s   r6   c                   @   s   e Zd ZdZeZeƒ ZdS )ÚIFileDialogViewú,Defines a visual extension to a file dialog.N©r7   r8   r9   r:   r1   Úviewr   Úis_fixedr<   r<   r<   r=   r>   e   s   r>   c                   @   s   e Zd ZdZdS )ÚIFileDialogExtensionz^Defines a (convenience) union of the IFileDialogModel and
    IFileDialogView interfaces.
    N©r7   r8   r9   r:   r<   r<   r<   r=   rC   t   s   rC   c                   @   s   e Zd Zeƒ ZdS )ÚMFileDialogModelN)r7   r8   r9   r   r;   r<   r<   r<   r=   rE      s   rE   c                   @   s   e Zd ZdZeZedƒZdS )ÚMFileDialogViewr?   FNr@   r<   r<   r<   r=   rF   ‹   s   rF   c                   @   s   e Zd ZdZdS )ÚMFileDialogExtensionzbDefines a (convenience) union of the MFileDialogModel and
    MFileDialogView mix-in classes.
    NrD   r<   r<   r<   r=   rG   ™   s   rG   c                
   @   s¨   e Zd ZdZedd�Zedd�Zedd�Zedd�Ze	e
edddd�edd	dd�ed
ddd�edddd�ddd�ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdS )ÚFileInfozFDefines a file dialog extension that display various file information.r;   ©ZobserveÚsizez	File sizeÚreadonly)ÚlabelÚstyleÚatimezLast accessÚmtimezLast modifiedÚctimez
Created atzFile InformationT©rL   Úshow_borderc                 C   s(   zt t| jƒƒd W S    Y dS 0 d S )Nz bytesÚ )r4   r   r;   ©Úselfr<   r<   r=   Ú	_get_size½   s    zFileInfo._get_sizec                 C   s*   zt dtt| jƒƒƒW S    Y dS 0 d S ©Nz%m/%d/%Y %I:%M:%S %prS   )r   r   r	   r;   rT   r<   r<   r=   Ú
_get_atimeÄ   s    ÿzFileInfo._get_atimec                 C   s*   zt dtt| jƒƒƒW S    Y dS 0 d S rW   )r   r   r   r;   rT   r<   r<   r=   Ú
_get_mtimeÍ   s    ÿzFileInfo._get_mtimec                 C   s*   zt dtt| jƒƒƒW S    Y dS 0 d S rW   )r   r   r
   r;   rT   r<   r<   r=   Ú
_get_ctimeÖ   s    ÿzFileInfo._get_ctimeN)r7   r8   r9   r:   r   rJ   rN   rO   rP   r/   r-   r+   rA   r   rV   rX   rY   rZ   r<   r<   r<   r=   rH   Ÿ   s.   



úÿ


rH   c                   @   s<   e Zd ZdZedd�Zeedddeƒ d�ƒZ	e
dd	„ ƒZd
S )ÚTextInfozHDefines a file dialog extension that displays a file's contents as text.r;   rI   ÚtextrK   F)rM   Ú
show_labelÚeditorc                 C   s†   zLt | jƒtkrW dS t| jdƒ�}| ¡ }W d   ƒ n1 s@0    Y  W n ty`   Y dS 0 | d¡dks~| d¡dkr‚dS |S )NzFile too big...ÚrbrS   ú r   õ   Ã¿zFile contains binary data...)r   r;   ÚMAX_SIZEÚopenÚreadÚ	ExceptionÚfind)rU   ÚfhÚdatar<   r<   r=   Ú	_get_textî   s    *zTextInfo._get_textN)r7   r8   r9   r:   r   r\   r/   r+   r#   rA   r   ri   r<   r<   r<   r=   r[   à   s   
ÿr[   c                	   @   s”   e Zd ZdZedd�Zedd�Zedd�Zee	e	e
ddd�e
ddd�d	d
d�e	e
ddeƒ d�dd
d
d�ƒƒZedd„ ƒZedd„ ƒZedd„ ƒZdS )Ú	ImageInfoz]Defines a file dialog extension that display an image file's dimensions
    and content.
    r;   rI   ÚimageÚwidthrK   ©rM   ÚheightzImage DimensionsTrQ   F)r]   r^   ZImage)rL   rR   Úspringyc                 C   sD   t | jƒ\}}t|ƒd dv r.t||gd�}ntdƒ}| ¡ | _|S )Nr"   )z.pngz.gifz.jpgz.jpeg)Úsearch_pathÚunknown)r   r;   r   r2   Zcreate_imageÚ
_cur_image)rU   ÚpathÚnamerk   r<   r<   r=   Ú
_get_image"  s    
zImageInfo._get_imagec                 C   s0   zt tƒ  | j¡d ƒd W S    Y dS 0 d S )Nr   ú pixelsú---©Ústrr5   Z
image_sizerr   rT   r<   r<   r=   Ú
_get_width,  s    zImageInfo._get_widthc                 C   s0   zt tƒ  | j¡d ƒd W S    Y dS 0 d S )Nr"   rv   rw   rx   rT   r<   r<   r=   Ú_get_height3  s    zImageInfo._get_heightN)r7   r8   r9   r:   r   rk   rl   rn   r/   r-   r+   r)   rA   r   ru   rz   r{   r<   r<   r<   r=   rj   ÿ   s4   




üüùÿ
	
rj   c                
   @   s„   e Zd ZdZeƒ Zeƒ ZedƒZedƒZ	e
eeeddd�eddd	d
�eddd�ƒeeddddd�dd�ƒdd�Zdd„ Zdd„ ZdS )ÚCreateDirHandlerz0Controller for the 'create new directory' popup.ÚOKÚCancelzhandler.dir_nameÚName)rL   ú
handler.okFzhandler.dir_name.strip() != '')r]   Úenabled_whenúhandler.cancel©r]   úhandler.messagerK   T)r]   rM   ro   )rR   Úpopup©Úkindc                 C   sj   |j j}t|ƒst|ƒ}t|| jƒ}z(t|ƒ d|j _||j _|j 	d¡ W n   d| j | _
Y n0 dS )ú(Handles the user clicking the OK button.Tz#Could not create the '%s' directoryN)Úobjectr;   r   r   r   Údir_namer   ÚreloadÚuiÚdisposeÚmessage)rU   ÚinfoÚdirrs   r<   r<   r=   Úhandler_ok_changedd  s    ÿz#CreateDirHandler.handler_ok_changedc                 C   s   |j  d¡ dS ©z,Handles the user clicking the Cancel button.FN©rŒ   r�   ©rU   r�   r<   r<   r=   Úhandler_cancel_changed|  s    z'CreateDirHandler.handler_cancel_changedN)r7   r8   r9   r:   r   rŠ   rŽ   r   ÚokÚcancelr/   r-   r%   r+   rA   r‘   r•   r<   r<   r<   r=   r|   ;  s<   
ý
ù	üùöër|   c                	   @   s|   e Zd ZdZeƒ ZedƒZedƒZe	e
eededd�d�eddd	�d
d�eeedƒedƒd
d�ƒdd�Zdd„ Zdd„ ZdS )ÚFileExistsHandlerz/Controller for the 'file already exists' popup.r}   r~   r„   z@icons:dialog-warning)rk   )r^   rK   rm   F)Zshow_labelsr€   r‚   r…   r†   c                 C   s"   |j j}|j  d¡ | d¡ dS ©rˆ   TN)rŒ   Úparentr�   )rU   r�   rš   r<   r<   r=   r‘   £  s    z$FileExistsHandler.handler_ok_changedc                 C   s   |j  d¡ dS r’   r“   r”   r<   r<   r=   r•   ©  s    z(FileExistsHandler.handler_cancel_changedN)r7   r8   r9   r:   r   rŽ   r   r–   r—   r/   r-   r%   r+   r)   r0   rA   r‘   r•   r<   r<   r<   r=   r˜   �  s2   þ
úü÷ïr˜   c                   @   sÚ   e Zd ZdZeƒ ZeeƒZe	dƒZ
edƒZedƒZeeƒZeeƒZeƒ Zeƒ ZeeƒZedƒZedd�Zedd�ZedƒZed	ƒZed
dd�Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dS ) ÚOpenFileDialogz7Defines the model and handler for the open file dialog.é
   z	Open Filez#traitsui.file_dialog.OpenFileDialogFr;   rI   r}   r~   z@icons:folder-newZtoolbar)rk   rM   c                 C   s
   || _ dS )zAHandles the UIInfo object being initialized during view start-up.N)r�   r”   r<   r<   r=   Ú	init_infoá  s    zOpenFileDialog.init_infoc                 C   s&   | j rt| jƒpt| jƒ S t| jƒS ©N)Úis_save_filer   r;   r   rT   r<   r<   r=   Ú_get_is_valid_fileç  s    z!OpenFileDialog._get_is_valid_filec                 C   s    t | jƒ}t|ƒot|ttB ƒS rž   )r   r;   r   r   r   r   )rU   r�   r<   r<   r=   Ú_get_can_create_dirí  s    
z"OpenFileDialog._get_can_create_dirc                 C   s,   | j rt| jƒrt| jƒ n|j d¡ dS r™   )rŸ   r   r;   r3   Ú_file_already_existsrŒ   r�   r”   r<   r<   r=   Úobject_ok_changedó  s    z OpenFileDialog.object_ok_changedc                 C   s   |j  d¡ dS r’   r“   r”   r<   r<   r=   Úobject_cancel_changedú  s    z$OpenFileDialog.object_cancel_changedc                 C   s.   t | jƒst| jƒ| _tƒ j| |jjd� dS )z6Handles the user clicking the create directory button.©Úcontextrš   N)r   r;   r   r|   Úedit_traitsÚcreateÚcontrolr”   r<   r<   r=   Úobject_create_changedþ  s
    
ÿz$OpenFileDialog.object_create_changedc                 C   s   | j r|  | j¡ dS )zCHandles the user double-clicking a file name in the file tree view.N)Úis_valid_filer£   r�   rT   r<   r<   r=   Ú_dclick_changed	  s    zOpenFileDialog._dclick_changedc                 C   s^  t dddddt| jdddd	�d
�}d }}t| jƒdkrò|d9 }t}g }t| jƒD ]„\}}d| }t| ||ƒ |}	| jd|dd� | 	t
¡s�t}	| |	j¡}
|	js¦t}| t |t|jjƒddddddt|
|d�d�	¡ qR||t|dddœŽdd�}ttt|ƒtt dddddddd�t ddt| jdd�dd�t dddd d!�t d"dd#�ƒƒ| j| jd$||ddd%�S )&z$Returns the file dialog view to use.r;   Z	file_treeZcustomFg      à?Tr‹   Údclick)ÚfilterZ	allow_dirZreload_nameZdclick_name)ÚidrM   r]   rl   r^   gš™™™™™É?r   g       @zextension_%d)ZmutualZ
horizontal)rA   r¯   )rL   r]   rM   rl   rn   ZdockÚ	resizabler^   Z	splitter2)r¯   ro   Zsplitter)r¯   r¨   rŸ   Úcan_create_dirzCreate a new directory)r¯   r]   rM   Zdefined_whenr�   ZtooltipÚhistory)ÚentriesZauto_set)r¯   r^   ro   r–   r«   )r¯   r]   r�   r—   rƒ   Z	livemodal)Útitler¯   r‡   rl   rn   Zclose_resultr°   )r+   r$   r®   ÚlenÚ
extensionsr%   Ú	enumerateÚsetattrZ
sync_traitZhas_traits_interfacer>   Údefault_viewZ
trait_viewrA   rB   r&   Úappendr!   Ú	__class__r7   r*   r.   r/   r-   r(   r³   r´   r¯   )rU   Úitemrl   rn   ÚklassÚitemsÚiÚ	extensionrt   Zextension_viewrA   r<   r<   r=   Úopen_file_view  s¨    üú


÷ÿýù	ÿúü
èþÜzOpenFileDialog.open_file_viewc                 C   s8   t dt| jƒ d�}|j| | jjjd�j| jjd� dS )zzHandles prompting the user when the selected file already exists,
        and the dialog is a 'save file' dialog.
        z:The file '%s' already exists.
Do you wish to overwrite it?)rŽ   r¥   )rš   N)	r˜   r   r;   r§   r�   r–   r©   Z	trait_setrŒ   )rU   Zfehr<   r<   r=   r¢   „  s    üÿÿz#OpenFileDialog._file_already_existsN)(r7   r8   r9   r:   r   r;   r   r   r®   r   r³   r´   r¯   r6   r¶   r   r,   r�   r   r‹   r­   Zextension__r   rŸ   r   r«   r±   r   r–   r—   r¨   r�   r    r¡   r£   r¤   rª   r¬   rÁ   r¢   r<   r<   r<   r=   r›   ®  s4   

tr›   c                  K   s&   t f i | ¤Ž}|jdd�jr"|jS dS )zZReturns a file name to open or an empty string if the user cancels the
    operation.
    rÁ   ©rA   rS   )r›   r§   Úresultr;   ©ZtraitsÚfdr<   r<   r=   Ú	open_fileš  s    rÆ   c                  K   s:   |   dd¡ d| d< tf i | ¤Ž}|jdd�jr6|jS dS )a  Returns a file name to save to or an empty string if the user cancels
    the operation. In the case where the file selected already exists, the
    user will be prompted if they want to overwrite the file before the
    selected file name is returned.
    r´   z	Save FileTrŸ   rÁ   rÂ   rS   )Ú
setdefaultr›   r§   rÃ   r;   rÄ   r<   r<   r=   Ú	save_file¥  s    rÈ   Ú__main__zPython file (*.py)|*.py)r¶   r®   N)Pr:   Úosr   r   r   r   Úos.pathr   r   r   r	   r
   r   r   r   r   r   r   r   Útimer   r   Z
traits.apir   r   r   r   r   r   r   r   r   r   r   r   r    Ztraits.trait_baser!   Úapir#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   Z	ui_traitsr1   Z
pyface.apir2   Zpyface.timer.apir3   Úhelperr4   r5   rb   r6   r>   rC   rE   rF   r¹   rG   rH   r[   rj   r|   r˜   r›   rÆ   rÈ   r7   Úprintr<   r<   r<   r=   Ú<module>   sH   8<@A<F- m
þÿ