a
    ¦ˆcÿ´  ã                   @   s  d Z ddlZddl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mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZm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%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z>m?Z? dZ@ee4ƒ dƒZAdZBdZCdZDdZEdZFdZGdd„ ZHdd„ ZIdd„ ZJdd„ ZKd d!„ ZLd"d#„ ZMd$d%„ ZNG d&d'„ d'e$ƒZOG d(d)„ d)e$ƒZPG d*d+„ d+e$ƒZQG d,d-„ d-e$ƒZRG d.d/„ d/e;ƒZSG d0d1„ d1e$ƒZTeTƒ ZTdS )2zH Defines the ImageLibrary object used to manage Pyface image libraries.
é    N)
ÚenvironÚlistdirÚremoveÚstatÚmakedirsÚrenameÚaccessÚR_OKÚW_OKÚX_OK)ÚjoinÚisdirÚisfileÚsplitextÚabspathÚdirnameÚbasenameÚexists)ÚST_MTIME)Úsystem)Ú
is_zipfileÚZipFileÚZIP_DEFLATED©Úallocate_lock)ÚThread)ÚHasPrivateTraitsÚPropertyÚStrÚIntÚListÚDictÚFileÚInstanceÚBoolÚ	UndefinedÚ
TraitErrorÚFloatÚAnyÚcached_property)Úget_resource_pathÚtraits_home)ÚImageResource)Úresource_manager)ÚImageReferenceÚResourceReference)Ú	HasMarginÚ	HasBorderÚ	Alignment)ú.pngz.gifz.jpgZjpegZimage_cache)úimage_volume.pyúimage_info.pyúlicense.txtz¹        ImageVolumeInfo(
            description=%(description)s,
            copyright=%(copyright)s,
            license=%(license)s,
            image_names=%(image_names)s
        )zNDescription:
    %s

Copyright:
    %s

License:
    %s

Applicable Images:
%szÜfrom pyface.image.image import ImageVolume, ImageVolumeInfo

volume = ImageVolume(
    category=%(category)s,
    keywords=%(keywords)s,
    aliases=%(aliases)s,
    time_stamp=%(time_stamp)s,
    info=[
%(info)s
    ]
)zefrom pyface.image.image import ImageInfo
from pyface.ui_traits import Margin, Border

images = [
%s
]aÔ      ImageInfo(
        name=%(name)s,
        image_name=%(image_name)s,
        description=%(description)s,
        category=%(category)s,
        keywords=%(keywords)s,
        width=%(width)d,
        height=%(height)d,
        border=Border(%(bleft)d, %(bright)d, %(btop)d, %(bbottom)d),
        content=Margin(%(cleft)d, %(cright)d, %(ctop)d, %(cbottom)d),
        label=Margin(%(lleft)d, %(lright)d, %(ltop)d, %(lbottom)d),
        alignment=%(alignment)s
    )c                 C   s6   t | dƒ�}| ¡ W  d  ƒ S 1 s(0    Y  dS )z8 Returns the contents of the specified *file_name*.
    ÚrbN)ÚopenÚread)Ú	file_nameÚfh© r<   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/image/image.pyÚ	read_file‘   s    r>   c                 C   sL   t |tƒr| d¡}t| dƒ�}| |¡ W d  ƒ n1 s>0    Y  dS )z6 Writes the specified data to the specified file.
    Úutf8ÚwbN)Ú
isinstanceÚstrÚencoder8   Úwrite)r:   Údatar;   r<   r<   r=   Ú
write_file˜   s    

rF   c                 C   s"   i }t |  dd¡tƒ |ƒ || S )z^ Returns the value of a Python symbol loaded from a specified source
        code string.
    ó   ó    )ÚexecÚreplaceÚglobals)ÚsourceÚnameÚtempr<   r<   r=   Úget_python_value¡   s    rO   c                 C   s   t j  | ¡ d¡S )z0 Returns a specified time as a text string.
    z%Y%m%d%H%M%S)ÚdatetimeÚutcfromtimestampÚstrftime)Útimer<   r<   r=   Útime_stamp_forª   s    rT   c                 C   s&   |  ¡  ¡ D ]\}}|| || < qdS )zc Adds all traits from a specified object to a dictionary with a specified
        name prefix.
    N)Ú	trait_getÚitems)ÚdictÚobjectÚprefixrM   Úvaluer<   r<   r=   Úadd_object_prefix°   s    r[   c                 C   sD   |   d¡}| d|… }| |d d… }|  d¡dk r<|d7 }||fS )z— Splits a specified **image_name** into its constituent volume and file
        names and returns a tuple of the form: ( volume_name, file_name ).
    ú:é   NÚ.r   r3   )Úfind)Ú
image_nameÚcolÚvolume_namer:   r<   r<   r=   Úsplit_image_name¸   s    
rc   c                 C   s2   t |ƒ\}}|dkr&| d¡dk r&|}d| |f S )zz Joins a specified **volume_name** and **file_name** into an image name,
        and return the resulting image name.
    r3   r^   r   z@%s:%s)r   r_   )rb   r:   ÚrootÚextr<   r<   r=   Újoin_image_nameÅ   s    rf   c                   @   sV   e Zd ZdZeƒ ZeZeƒ Z	e
ƒ Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚFastZipFilezm Provides fast access to zip files by keeping the underlying zip file
        open across multiple uses.
    c                 C   s2   | j  ¡  z| j ¡ W | j  ¡  S | j  ¡  0 dS )zM Returns the names of all files in the top-level zip file directory.
        N)r   ÚacquireÚzfÚnamelistÚrelease©Úselfr<   r<   r=   rj   ã   s    


þzFastZipFile.namelistc                 C   s4   | j  ¡  z| j |¡W | j  ¡  S | j  ¡  0 dS )z\ Returns the contents of the specified **file_name** from the zip
            file.
        N)r   rh   ri   r9   rk   )rm   r:   r<   r<   r=   r9   ì   s    

þzFastZipFile.readc                 C   sD   | j  ¡  z(| jdur&| j ¡  d| _W | j  ¡  n| j  ¡  0 dS )z| Temporarily closes the zip file (usually while the zip file is being
            replaced by a different version).
        N)r   rh   Ú_zfÚcloserk   rl   r<   r<   r=   ro   ö   s    


zFastZipFile.closec                 C   s   t ƒ S ©Nr   rl   r<   r<   r=   Ú_access_default  s    zFastZipFile._access_defaultc                 C   sH   t   ¡ | _| jd u rBt| jdƒ| _| jd u rBt| jd� ¡  d| _| jS )NÚr)ÚtargetT)	rS   Ú
time_stamprn   r   ÚpathÚ_runningr   Ú_processÚstartrl   r<   r<   r=   Ú_get_zf	  s    


zFastZipFile._get_zfc                 C   sb   t  d¡ | j ¡  t   ¡ | jd krR| jdur@| j ¡  d| _d| _| j ¡  q^| j ¡  q dS )zq Waits until the zip file has not been accessed for a while, then
            closes the file and exits.
        r]   g       @N)	rS   Úsleepr   rh   rt   rn   ro   rv   rk   rl   r<   r<   r=   rw     s    




zFastZipFile._processN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   ru   r   ri   r'   rt   r(   r   rj   r9   ro   rq   ry   rw   r<   r<   r<   r=   rg   Ð   s   	
rg   c                   @   sš   e Zd ZdZedƒZeƒ Zeƒ Zeƒ Z	edƒZ
eeƒZeƒ Zeƒ ZeZeZeZeZeZeZeZdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z dd„ Z!dS )Ú	ImageInfozY Defines a class that contains information about a specific Traits UI
        image.
    ÚImageVolumeÚGeneralc                 C   s   t | jƒd S )Nr]   )rc   r`   rl   r<   r<   r=   Ú_name_defaultd  s    zImageInfo._name_defaultc                 C   sF   | j d u rdS | j  | j¡}|d u r.d| _dS | | ¡ ¡\}| _|S ©Nr   )ÚvolumeÚimage_resourcer`   ÚheightÚ
image_sizeÚcreate_image)rm   ÚimageÚwidthr<   r<   r=   Ú_width_defaultg  s    
zImageInfo._width_defaultc                 C   sF   | j d u rdS | j  | j¡}|d u r.d| _dS | | ¡ ¡\| _}|S rƒ   )r„   r…   r`   rŠ   r‡   rˆ   )rm   r‰   r†   r<   r<   r=   Ú_height_defaultt  s    
zImageInfo._height_defaultc              
      s�   t dd„ ˆ  dddddd¡ ¡ D ƒƒ}| ˆ  d	d
¡¡ g d¢}| ‡ fdd„|D ƒ¡ | ‡ fdd„|D ƒ¡ | ‡ fdd„|D ƒ¡ t| S )Nc                 s   s   | ]\}}|t |ƒfV  qd S rp   ©Úrepr©Ú.0rM   rZ   r<   r<   r=   Ú	<genexpr>„  s   ÿz1ImageInfo._get_image_info_code.<locals>.<genexpr>rM   r`   ÚdescriptionÚcategoryÚkeywordsÚ	alignmentrŠ   r†   )ÚleftÚrightÚtopZbottomc                 3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚbN)ÚgetattrÚborder©r�   rM   rl   r<   r=   r‘   ‘  rH   c                 3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚcN)rš   Úcontentrœ   rl   r<   r=   r‘   ’  s   c                 3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚlN)rš   Úlabelrœ   rl   r<   r=   r‘   •  rH   )rW   rU   rV   ÚupdateÚImageInfoTemplate)rm   rE   Zsidesr<   rl   r=   Ú_get_image_info_codeƒ  s$    úþÿzImageInfo._get_image_info_codec                 C   s
   |   d¡S )NÚ	copyright©Ú_volume_inforl   r<   r<   r=   Ú_get_copyright˜  s    zImageInfo._get_copyrightc                 C   s
   |   d¡S )NÚlicenser¥   rl   r<   r<   r=   Ú_get_license›  s    zImageInfo._get_licensec                 C   s&   | j  | j¡}|dur"t||dƒS dS )zC Returns the VolumeInfo object that applies to this image.
        NÚUnknown)r„   Úvolume_infor`   rš   )rm   rM   Úinfor<   r<   r=   r¦      s    zImageInfo._volume_infoN)"r{   r|   r}   r~   r#   r„   r   rM   r`   r’   r“   r    r”   r   rŠ   r†   r1   r›   r0   rž   r    r2   r•   r   r¤   r¨   Úimage_info_coder‚   r‹   rŒ   r£   r§   r©   r¦   r<   r<   r<   r=   r   /  s.   r   c                   @   sT   e Zd ZedƒZedƒZedƒZeeƒZe	Z
e	Zedd„ ƒZedd„ ƒZdd	„ Zd
S )ÚImageVolumeInfoz No volume description specified.z#No copyright information specified.z!No license information specified.c                    s   t ‡ fdd„dD ƒƒ}t| S )Nc                 3   s    | ]}|t tˆ |ƒƒfV  qd S rp   )rŽ   rš   rœ   rl   r<   r=   r‘   Ê  s   ÿz>ImageVolumeInfo._get_image_volume_info_code.<locals>.<genexpr>)r’   r¤   r¨   Úimage_names)rW   ÚImageVolumeInfoCodeTemplate©rm   rE   r<   rl   r=   Ú_get_image_volume_info_codeÈ  s    þz+ImageVolumeInfo._get_image_volume_info_codec                 C   sf   | j  dd¡}| j dd¡ ¡ }| j}| ¡  t|ƒdkr@dg}d dd„ |D ƒ¡}t|| j	||f S )NÚ
z
    r   ZAllc                 S   s   g | ]}d | ‘qS )z  - r<   )r�   r`   r<   r<   r=   Ú
<listcomp>Ù  rH   z?ImageVolumeInfo._get_image_volume_info_text.<locals>.<listcomp>)
r’   rJ   r¨   Ústripr¯   ÚsortÚlenr   ÚImageVolumeInfoTextTemplater¤   )rm   r’   r¨   r¯   Úimagesr<   r<   r=   Ú_get_image_volume_info_textÑ  s    üz+ImageVolumeInfo._get_image_volume_info_textc                 C   s   |   g d¢¡S )z7 Returns a copy of the ImageVolumeInfo object.
        )r’   r¤   r¨   )Úclonerl   r<   r<   r=   r»   ä  s    zImageVolumeInfo.cloneN)r{   r|   r}   r   r’   r¤   r¨   r    r¯   r   Úimage_volume_info_codeÚimage_volume_info_textr)   r²   rº   r»   r<   r<   r<   r=   r®   ¯  s   

r®   c                   @   sØ   e Zd Zeƒ ZeeƒZedƒZeeƒZ	eeƒZ
eƒ ZedƒZeeƒZeeƒZedd�Zeƒ ZeZeZeZdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z e!dd„ ƒZ"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dS ) r€   r�   Tr¹   ©Zobservec                 C   s*   | j D ]
}d|_q|  dg¡ |  ¡  dS )zv Updates the contents of the image volume from the underlying
            image store, and saves the results.
        Nr¹   )r¹   r„   Zreset_traitsÚsave)rm   r‰   r<   r<   r=   r¡   !  s    
zImageVolume.updatec           	   	   C   sò  | j }| js$t|ttB tB ƒsPdS n,tt|ƒttB tB ƒrLt|ƒrPt|tƒsPdS | j}| js¦t	t
 
¡ d ƒ| _tt|dƒ| jƒ tt|dƒ|ƒ tt|dƒ| jƒ dS |d }t|dtƒ}�z| j}| ¡ D ]}|tvrÌ| || |¡¡ qÌ| ¡  t	t
 
¡ d	 ƒ| _| d| j¡ | d|¡ | d| j¡ | ¡  d
}|d }tdƒD ]<}zt||ƒ W  �qˆW n t�y‚   t
 d¡ Y n0 �qJzt||ƒ |}W n   t||ƒ ‚ Y n0 W |d
u�rÈ| ¡  t|ƒ n|d
u�rä| ¡  t|ƒ 0 dS )zo Saves the contents of the image volume using the current contents
            of the **ImageVolume**.
        Fg      @r4   r5   r6   Tz.###Úwg      $@Nz.$$$é2   gš™™™™™¹?)ru   Úis_zip_filer   r	   r
   r   r   r   Úimages_coderT   rS   rt   rF   r   Úimage_volume_codeÚlicense_textr   r   Úzip_filerj   Údont_copy_listÚwritestrr9   ro   Úranger   Ú	Exceptionrz   r   )	rm   ru   rÃ   r:   Znew_zfZcur_zfrM   Z	temp_nameÚir<   r<   r=   r¿   /  sf    ÿÿ





ý

zImageVolume.savec                 C   sx   t |ƒ\}}| jrP|  |¡}|du r@ttj| j| j| j|d�}qft	tj|d�}nt	tjt
| j|ƒd�}t|ƒ}||_|S )zL Returns the ImageResource object for the specified **image_name**.
        N)Úresource_factoryrÆ   ru   rb   r:   )Úfilename)rc   rÂ   Ú_check_cacheÚZipFileReferencer-   rÌ   rÆ   ru   rM   r.   r   r,   Z_ref)rm   r`   rb   r:   Ú
cache_fileÚrefÚresourcer<   r<   r=   r…   š  s*    
û	ÿ
þzImageVolume.image_resourcec                 C   s2   t |ƒ\}}| jr| j |¡S tt| j|ƒƒS dS )z_ Returns the image data (i.e. file contents) for the specified image
            name.
        N)rc   rÂ   rÆ   r9   r>   r   ru   )rm   r`   rb   r:   r<   r<   r=   Ú
image_dataÁ  s    zImageVolume.image_datac                 C   sB   | j D ]$}t|jƒdks"||jv r|  S qtd t|ƒ¡ƒ‚dS )zs Returns the ImageVolumeInfo object that corresponds to the
            image specified by **image_name**.
        r   z(Volume info for image name {} not found.N)r¬   r·   r¯   Ú
ValueErrorÚformatrŽ   )rm   r`   r¬   r<   r<   r=   r«   Ì  s    
ÿ
ÿzImageVolume.volume_infoc                 C   s   t ƒ gS rp   )r®   rl   r<   r<   r=   Ú_info_defaultÜ  s    zImageVolume._info_defaultc                 C   s   |   ¡ S rp   )Ú_load_image_inforl   r<   r<   r=   Ú_images_defaultß  s    zImageVolume._images_defaultc                 C   s   t dd„ | jD ƒƒS )Nc                 s   s   | ]}|j |fV  qd S rp   ©r`   ©r�   r‰   r<   r<   r=   r‘   æ  rH   z+ImageVolume._get_catalog.<locals>.<genexpr>)rW   r¹   rl   r<   r<   r=   Ú_get_catalogä  s    zImageVolume._get_catalogc              	   C   sF   t dd„ |  ddddd¡ ¡ D ƒƒ}d d	d„ | jD ƒ¡|d
< t| S )Nc                 s   s   | ]\}}|t |ƒfV  qd S rp   r�   r�   r<   r<   r=   r‘   é  s   ÿz5ImageVolume._get_image_volume_code.<locals>.<genexpr>r’   r“   r”   Úaliasesrt   ú,
c                 s   s   | ]}|j V  qd S rp   )r¼   ©r�   r¬   r<   r<   r=   r‘   ï  s   r¬   )rW   rU   rV   r   r¬   ÚImageVolumeTemplater±   r<   r<   r=   Ú_get_image_volume_codeè  s    
ÿþ
ÿz"ImageVolume._get_image_volume_codec                 C   s   d  dd„ | jD ƒ¡}t| S )NrÝ   c                 s   s   | ]}|j V  qd S rp   )r­   rÞ   r<   r<   r=   r‘   õ  rH   z/ImageVolume._get_images_code.<locals>.<genexpr>)r   r¹   ÚImageVolumeImagesTemplate)rm   r¹   r<   r<   r=   Ú_get_images_codeô  s    zImageVolume._get_images_codec                 C   s   dd   dd„ | jD ƒ¡S )Nz

%s
zO-------------------------------------------------------------------------------c                 S   s   g | ]
}|j ‘qS r<   )r½   rÞ   r<   r<   r=   r´   û  rH   z1ImageVolume._get_license_text.<locals>.<listcomp>)r   r¬   rl   r<   r<   r=   Ú_get_license_textù  s    ÿzImageVolume._get_license_textc                 C   s¬  | j dkrg S tt| j ƒt ƒ}| j}g }g }| jrœ| j}| ¡ }d|v rZt| 	d¡dƒ}| j
|k rš|D ]0}t|ƒ\}}	|	tv rh| t|t||ƒd�¡ qhnjt| j dƒ}
t|
ƒr¾tt|
ƒdƒ}| j
|k �rt| j ƒD ]0}t|ƒ\}}	|	tv rÔ| t|t||ƒd�¡ qÔt|ƒdk�r|}nftdd„ |D ƒƒ}|D ]B}| |j¡}|du�r0|||j< | |_|j|_|j|_d|_�q0t| ¡ ƒ}|| _
|jd	d
„ d� |D ]}| |_�qš|S )zM Returns the list of ImageInfo objects for the images in the volume.
        Ú r5   r¹   )rM   r`   r   c                 S   s   g | ]}|j |f‘qS r<   rÙ   rÚ   r<   r<   r=   r´   D  rH   z0ImageVolume._load_image_info.<locals>.<listcomp>Nc                 S   s   | j S rp   rÙ   ©Úitemr<   r<   r=   Ú<lambda>U  rH   z.ImageVolume._load_image_info.<locals>.<lambda>©Úkey)ru   rT   r   r   rM   rÂ   rÆ   rj   rO   r9   rt   r   ÚImageFileExtsÚappendr   rf   r   r   r>   r   r·   rW   Úgetr`   r„   rŠ   r†   ÚlistÚvaluesr¶   )rm   rt   rb   Z
old_imagesZ
cur_imagesri   ÚnamesrM   rd   re   Zimage_info_pathr¹   Zcur_image_setZ	old_imageZ	cur_imager‰   r<   r<   r=   r×      sr    

ÿ
þÿÿþÿÿ



zImageVolume._load_image_infoc                 C   s4   t t| j|ƒ}t|ƒr0tt|ƒt ƒ| jkr0|S dS )zÎ Checks to see if the specified zip file name has been saved in the
            image cache. If it has, it returns the fully-qualified cache file
            name to use; otherwise it returns None.
        N)r   Úimage_cache_pathrM   r   rT   r   r   rt   )rm   r:   rÐ   r<   r<   r=   rÎ   ]  s    ÿzImageVolume._check_cacheN)(r{   r|   r}   r   rM   r    r®   r¬   r“   r”   rÜ   r"   ru   r$   rÂ   r#   rg   rÆ   r   r¹   r   Úcatalogrt   rÄ   rÃ   rÅ   r¡   r¿   r…   rÓ   r«   rÖ   rØ   r)   rÛ   rà   râ   rã   r×   rÎ   r<   r<   r<   r=   r€   ï  s8   
k'
]r€   c                   @   s:   e Zd ZeeƒZeƒ Zeƒ Ze	ƒ Z
eZdd„ Zdd„ ZdS )rÏ   c                 C   sª   | j }|dkrž| j | j¡}| j |t¡}|dur6|S tt| j	ƒ}t
|ƒsRt|ƒ t|| jƒ}t|dƒ�}| |¡ W d  ƒ n1 sˆ0    Y  || _ d| _| j |¡S )z Loads the resource.
        rä   Nr@   )rÐ   rÆ   r9   r:   rÌ   Zimage_from_datar%   r   rð   rb   r   r   r8   rD   Zimage_from_file)rm   rÐ   rE   r‰   Ú	cache_dirr;   r<   r<   r=   Úload…  s    (zZipFileReference.loadc                 C   s   | j dkr|  ¡  | j S )Nrä   )rÐ   ró   rl   r<   r<   r=   Ú_get_filename©  s    
zZipFileReference._get_filenameN)r{   r|   r}   r#   rg   rÆ   r   rb   r:   r"   rÐ   r   rÍ   ró   rô   r<   r<   r<   r=   rÏ   p  s   $rÏ   c                   @   s¬   e Zd ZdZeeƒZeeeƒZ	e
edd�Zeƒ Zdd„ Zdd„ Zdd	„ Zd!dd„Zd"dd„Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd
S )#ÚImageLibraryz( Manages Traits UI image libraries.
    zvolumes.items.imagesr¾   c                 C   s"   |   |¡}|dur|j |¡S dS )z_ Returns the ImageInfo object corresponding to a specified
            **image_name**.
        N)Úfind_volumerñ   rì   ©rm   r`   r„   r<   r<   r=   Ú
image_infoÊ  s    
zImageLibrary.image_infoc                 C   s>   |  d¡dk rd|dd…  }|  |¡}|dur:| |¡S dS )zG Returns an ImageResource object for the specified image name.
        r\   r   z
@images:%sr]   N)r_   rö   r…   r÷   r<   r<   r=   r…   Ô  s    

zImageLibrary.image_resourcec                 C   s@   t |ƒ\}}| j}| j}||vr8| |¡}|du rdS q|| S )z‰ Returns the ImageVolume object corresponding to the specified
            **image_name** or None if the volume cannot be found.
        N)rc   rñ   rÜ   rì   )rm   r`   rb   r:   rñ   rÜ   r<   r<   r=   rö   å  s    
zImageLibrary.find_volumeNc                 C   sÎ   |du rt tdƒdƒ}t|ƒrn|  |¡}|du r<td| ƒ‚|j| jv rT|  |j¡ || j|j< | j 	|¡ n\t
|ƒr¾| j}|  |¡}|D ]$}|j|v r¤|  |j¡ |||j< qŠ| j |¡ ntd| ƒ‚dS )a¤   If **file_name** is a file, it adds an image volume specified by
            **file_name** to the image library. If **file_name** is a
            directory, it adds all image libraries contained in the directory
            to the image library. If **file_name** is omitted, all image
            libraries located in the *images* directory contained in the same
            directory as the caller are added.
        Né   r¹   z!'%s' is not a valid image volume.zYThe add method argument must be None or a file or directory path, but '%s' was specified.)r   r*   r   Ú_add_volumer&   rM   rñ   Ú_duplicate_volumeÚvolumesrë   r   Ú	_add_pathÚextend)rm   r:   r„   rñ   rü   r<   r<   r=   Ú
add_volumeö  s2    

ÿ

ÿÿzImageLibrary.add_volumec                 C   s°   || j v rtd| ƒ‚|du r,ttdƒdƒ}t|ƒs@td| ƒ‚t|dƒ}t|ƒrbtt|ƒdƒ}ntƒ }|j	||dd	� |j
tt|ƒt ƒk r–| ¡  || j |< | j |¡ dS )
a…   Adds the directory specified by **path** as a *virtual* volume
            called **volume_name**. All image files contained within path
            define the contents of the volume. If **path** is None, the
            *images* contained in the 'images' subdirectory of the same
            directory as the caller are is used as the path for the *virtual*
            volume..
        z5The volume name '%s' is already in the image library.Nrù   r¹   z*The image volume path '%s' does not exist.r4   r„   F)rM   ru   rÂ   )rñ   r&   r   r*   r   r   rO   r>   r€   Z	trait_setrt   rT   r   r   r¿   rü   rë   )rm   rb   ru   Zimage_volume_pathr„   r<   r<   r=   Úadd_path#  s*    	
ÿÿÿ

zImageLibrary.add_pathc                 C   s  t t|ƒƒ\}}|dkr |d7 }t|d�}t|ƒr>td| ƒ‚t|dtƒ}d}tƒ }tƒ }	g }
i }�z�t|ƒD �]}|dd… d	ks’| d
¡dk ržtd| ƒ‚t	|ƒ\}}|  
|¡}|du rÈtd| ƒ‚|j |¡}|du rètd| ƒ‚|
 |¡ | || |¡¡ ||k�r:||v�r:| |¡ |jD ]}|	 |¡ �q(| |¡}| |¡}|du �rh| ¡  ||< }|j |¡ qn|
jdd„ d� |
|_t|ƒ|_t|	ƒ|_t| ¡ ƒ|_| d|j¡ | d|j¡ | d|j¡ d}W | ¡  |�rt|ƒ n| ¡  |�rt|ƒ 0 dS )zÂ Builds a new image volume called **file_name** from the list of
            image names specified by **image_names**. Each image name should be
            of the form: '@volume:name'.
        rä   ú.zip©rM   zThe '%s' file already exists.rÀ   TNr]   ú@r\   r   zBThe image name specified by '%s' is not of the form: @volume:name.z2Could not find the image volume specified by '%s'.z+Could not find the image specified by '%s'.c                 S   s   | j S rp   rÙ   rå   r<   r<   r=   rç   ­  rH   z&ImageLibrary.extract.<locals>.<lambda>rè   r4   r5   r6   F) r   r   r€   r   r&   r   r   Úsetr_   rc   rö   rñ   rì   rë   rÈ   rÓ   Úaddr”   r«   r»   r¯   r¶   r¹   rí   rÜ   rî   r¬   rÄ   rÃ   rÅ   ro   r   )rm   r:   r¯   rb   re   r„   ri   ÚerrorrÜ   r”   r¹   r¬   r`   Zimage_volume_nameZimage_file_nameZimage_volumerø   Úkeywordr«   Zvinfor<   r<   r=   ÚextractS  s„    
üÿ	ÿ
üÿ	ÿÿ

ÿ









þzImageLibrary.extractc                 C   s–   g }t tttjd ƒƒdƒ}t|ƒr4| |  |¡¡ | |  t tdƒdƒ¡¡ t	 
d¡}|d ur’d}tƒ dkrrd}| |¡D ]}| |  |¡¡ q||S )Nr   Zlibraryr]   ZTRAITS_IMAGESú;ÚWindowsr\   )r   r   r   ÚsysÚargvr   rþ   rý   r*   r   rì   r   Úsplit)rm   ÚresultZapp_libraryÚpathsÚ	separatorru   r<   r<   r=   Ú_volumes_defaultË  s    

zImageLibrary._volumes_defaultc                 C   s   t dd„ | jD ƒƒS )Nc                 S   s   g | ]}|j |f‘qS r<   r  )r�   r„   r<   r<   r=   r´   è  rH   z1ImageLibrary._catalog_default.<locals>.<listcomp>)rW   rü   rl   r<   r<   r=   Ú_catalog_defaultç  s    zImageLibrary._catalog_defaultc                 C   s   |   ¡ S rp   )Ú_get_images_listrl   r<   r<   r=   Ú_get_imagesì  s    zImageLibrary._get_imagesc                 C   s0   g }| j D ]}| |j¡ q
|jdd„ d� |S )z1 Returns the list of all library images.
        c                 S   s   | j S rp   rÙ   )r‰   r<   r<   r=   rç   û  rH   z/ImageLibrary._get_images_list.<locals>.<lambda>rè   )rü   rþ   r¹   r¶   )rm   r¹   r„   r<   r<   r=   r  ò  s
    
zImageLibrary._get_images_listc                 C   sP   g }t |ƒrLt|ƒD ]6}t|ƒd dkr|  t||ƒ¡}|dur| |¡ q|S )zz Returns a list of ImageVolume objects, one for each image library
            located in the specified **path**.
        r]   r  N)r   r   r   rú   r   rë   )rm   ru   r  Úbaser„   r<   r<   r=   rý      s    zImageLibrary._add_pathc                 C   sœ   t |ƒ}t|ƒr˜t|d�}tt|ƒƒd }| ¡ }d|v rht| d¡dƒ}||_|  	|¡ ||_
||_nt|||d�}|jtt|ƒt ƒk r”| ¡  |S dS )z° Returns an ImageVolume object for the image library specified by
            **path**. If **path** does not specify a valid ImageVolume, None is
            returned.
        )ru   r   r4   r„   )rM   ru   rÆ   N)r   r   rg   r   r   rj   rO   r9   rM   Ú_add_aliasesru   rÆ   r€   rt   rT   r   r   r¿   )rm   ru   ri   rb   rï   r„   r<   r<   r=   rú     s     

zImageLibrary._add_volumec                 C   sN   | j }|j}|j D ]6}||v r@||| kr@td|||| f ƒ‚|||< qdS )zb Try to add all of the external volume references as aliases for
            this volume.
        zUImage library error: Attempt to alias '%s' to '%s' when it is already aliased to '%s'N)rÜ   rM   r&   )rm   r„   rÜ   rb   Zvnamer<   r<   r=   r  M  s    
ûÿzImageLibrary._add_aliasesc                 C   s   t d| ƒ‚dS )z/ Raises a duplicate volume name error.
        z]Attempted to add an image volume called '%s' when a volume with that name is already defined.N)r&   )rm   rb   r<   r<   r=   rû   _  s
    üÿzImageLibrary._duplicate_volume)N)N)r{   r|   r}   r~   r    r€   rü   r!   r   rñ   r   r¹   rÜ   rø   r…   rö   rÿ   r   r  r  r  r)   r  r  rý   rú   r  rû   r<   r<   r<   r=   rõ   µ  s(   


-
0x
7rõ   )Ur~   r  Úosr   r   r   r   r   r   r   r	   r
   r   Úos.pathr   r   r   r   r   r   r   r   r   Úplatformr   Úzipfiler   r   r   rP   rS   Ú_threadr   Ú	threadingr   Z
traits.apir   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   Ztraits.trait_baser*   r+   Z
pyface.apir,   Zpyface.resource_managerr-   Z"pyface.resource.resource_referencer.   r/   Zpyface.ui_traitsr0   r1   r2   rê   rð   rÇ   r°   r¸   rß   rá   r¢   r>   rF   rO   rT   r[   rc   rf   rg   r   r®   r€   rÏ   rõ   r<   r<   r<   r=   Ú<module>   sZ   0(
@		_ @   E   :