3
<Ad]N¶  ã               @   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? d7Z@ee4ƒ dƒZAd8ZBd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/d0„ d0e$ƒZQG d1d2„ d2e$ƒZRG d3d4„ d4e;ƒZSG d5d6„ d6e$ƒZTeTƒ ZTdS )9zH 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ú.pngú.gifú.jpgÚ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   s   t | dƒ�
}|jƒ S Q R X dS )z8 Returns the contents of the specified *file_name*.
    ÚrbN)ÚopenÚread)Ú	file_nameÚfh© r>   úl/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/pyface-6.1.2-py3.6.egg/pyface/image/image.pyÚ	read_filew   s    r@   c          	   C   s$   t | dƒ�}|j|ƒ W dQ R X dS )z6 Writes the specified data to the specified file.
    ÚwbN)r:   Úwrite)r<   Údatar=   r>   r>   r?   Ú
write_file~   s    rD   c             C   s"   i }t | j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    rM   c             C   s   t j j| ƒ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    rR   c             C   s*   x$|j ƒ jƒ D ]\}}|| || < qW dS )zc Adds all traits from a specified object to a dictionary with a specified
        name prefix.
    N)Ú	trait_getÚitems)ÚdictÚobjectÚprefixrK   Úvaluer>   r>   r?   Úadd_object_prefix”   s    rY   c             C   sD   | j dƒ}| d|… }| |d d… }|j 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   z.png)Úfind)Ú
image_nameÚcolÚvolume_namer<   r>   r>   r?   Úsplit_image_nameœ   s    
ra   c             C   s2   t |ƒ\}}|dkr&|j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.
    z.pngr\   r   z@%s:%s)r   r]   )r`   r<   ÚrootÚextr>   r>   r?   Újoin_image_name©   s    rd   c               @   sP   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   s&   | j jƒ  z
| jjƒ S | j jƒ  X 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?   rh   Ç   s    

zFastZipFile.namelistc          
   C   s(   | j jƒ  z| jj|ƒS | j jƒ  X dS )z\ Returns the contents of the specified **file_name** from the zip
            file.
        N)r   rf   rg   r;   ri   )rj   r<   r>   r>   r?   r;   Ð   s    
zFastZipFile.readc          
   C   s:   | j jƒ  z| jdk	r&| jjƒ  d| _W d| j jƒ  X dS )z| Temporarily closes the zip file (usually while the zip file is being
            replaced by a different version).
        N)r   rf   Ú_zfÚcloseri   )rj   r>   r>   r?   rl   Ú   s    



zFastZipFile.closec             C   s   t ƒ S )N)r   )rj   r>   r>   r?   Ú_access_defaultè   s    zFastZipFile._access_defaultc             C   sH   t j ƒ | _| jd krBt| jdƒ| _| jd krBt| jd�jƒ  d| _| jS )NÚr)ÚtargetT)	rQ   Ú
time_stamprk   r   ÚpathÚ_runningr   Ú_processÚstart)rj   r>   r>   r?   Ú_get_zfí   s    


zFastZipFile._get_zfc             C   sf   x`t jdƒ | jjƒ  t j ƒ | jd krT| jdk	rB| jjƒ  d| _d| _| jjƒ  P | jjƒ  qW dS )zq Waits until the zip file has not been accessed for a while, then
            closes the file and exits.
        r[   g       @N)	rQ   Úsleepr   rf   rp   rk   rl   rr   ri   )rj   r>   r>   r?   rs   û   s    




zFastZipFile._processN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   rq   r   rg   r&   rp   r'   r   rh   r;   rl   rm   ru   rs   r>   r>   r>   r?   re   ´   s   	
re   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[   )ra   r^   )rj   r>   r>   r?   Ú_name_defaultF  s    zImageInfo._name_defaultc             C   sF   | j d krdS | j j| jƒ}|d kr.d| _dS |j|jƒ ƒ\}| _|S )Nr   )ÚvolumeÚimage_resourcer^   ÚheightÚ
image_sizeÚcreate_image)rj   ÚimageÚwidthr>   r>   r?   Ú_width_defaultI  s    
zImageInfo._width_defaultc             C   sF   | j d krdS | j j| jƒ}|d kr.d| _dS |j|jƒ ƒ\| _}|S )Nr   )r   r€   r^   r…   r‚   rƒ   )rj   r„   r�   r>   r>   r?   Ú_height_defaultV  s    
zImageInfo._height_defaultc          	      s”   t dd„ ˆ jddddddƒjƒ D ƒƒ}|jˆ jd	d
ƒƒ ddddg}|j‡ fdd„|D ƒƒ |j‡ fdd„|D ƒƒ |j‡ fdd„|D ƒƒ t| S )Nc             s   s   | ]\}}|t |ƒfV  qd S )N)Úrepr)Ú.0rK   rX   r>   r>   r?   ú	<genexpr>f  s   z1ImageInfo._get_image_info_code.<locals>.<genexpr>rK   r^   ÚdescriptionÚcategoryÚkeywordsÚ	alignmentr…   r�   ÚleftÚrightÚtopÚbottomc             3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚbN)ÚgetattrÚborder)r‰   rK   )rj   r>   r?   rŠ   m  s    c             3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚcN)r”   Úcontent)r‰   rK   )rj   r>   r?   rŠ   n  s    c             3   s"   | ]}d | t ˆ j|ƒfV  qdS )ÚlN)r”   Úlabel)r‰   rK   )rj   r>   r?   rŠ   o  s    )rU   rS   rT   ÚupdateÚImageInfoTemplate)rj   rC   Zsidesr>   )rj   r?   Ú_get_image_info_codee  s    zImageInfo._get_image_info_codec             C   s
   | j dƒS )NÚ	copyright)Ú_volume_info)rj   r>   r>   r?   Ú_get_copyrightr  s    zImageInfo._get_copyrightc             C   s
   | j dƒS )NÚlicense)rž   )rj   r>   r>   r?   Ú_get_licenseu  s    zImageInfo._get_licensec             C   s&   | j j| jƒ}|dk	r"t||dƒS dS )zC Returns the VolumeInfo object that applies to this image.
        NÚUnknown)r   Úvolume_infor^   r”   )rj   rK   Úinfor>   r>   r?   rž   z  s    zImageInfo._volume_infoN)"rw   rx   ry   rz   r"   r   r   rK   r^   r‹   rŒ   r   r�   r   r…   r�   r0   r•   r/   r—   r™   r1   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 )N)rˆ   r”   )r‰   rK   )rj   r>   r?   rŠ   ¢  s   z>ImageVolumeInfo._get_image_volume_info_code.<locals>.<genexpr>r‹   r�   r    Úimage_names)r‹   r�   r    r§   )rU   ÚImageVolumeInfoCodeTemplate)rj   rC   r>   )rj   r?   Ú_get_image_volume_info_code   s     
z+ImageVolumeInfo._get_image_volume_info_codec             C   sf   | j jddƒ}| jjddƒjƒ }| j}|jƒ  t|ƒdkr@dg}djdd„ |D ƒƒ}t|| j	||f S )NÚ
z
    r   ÚAllc             S   s   g | ]}d | ‘qS )z  - r>   )r‰   r^   r>   r>   r?   ú
<listcomp>°  s   z?ImageVolumeInfo._get_image_volume_info_text.<locals>.<listcomp>)
r‹   rH   r    Ústripr§   ÚsortÚlenr   ÚImageVolumeInfoTextTemplater�   )rj   r‹   r    r§   Úimagesr>   r>   r?   Ú_get_image_volume_info_text¨  s    

z+ImageVolumeInfo._get_image_volume_info_textc             C   s   | j dddgƒS )z7 Returns a copy of the ImageVolumeInfo object.
        r‹   r�   r    )Úclone)rj   r>   r>   r?   r³   ¸  s    zImageVolumeInfo.cloneN)rw   rx   ry   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±   )Ú
depends_onc             C   s.   x| j D ]
}d|_qW | jdgƒ | jƒ  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)rj   r„   r>   r>   r?   rš   ó  s    
zImageVolume.updatec       	      C   sÞ  | j }| js$t|ttB tB ƒsTdS n0tt|ƒttB tB ƒ sPt|ƒrTt|tƒ rTdS | j}| jsªt	t
j
ƒ d ƒ| _tt|dƒ| jƒ tt|dƒ|ƒ tt|dƒ| jƒ dS |d }t|dtƒ}zþ| j}x*|jƒ D ]}|tkrÐ|j||j|ƒƒ qÐW |jƒ  t	t
j
ƒ d	 ƒ| _|jd| jƒ |jd|ƒ |jd| jƒ |jƒ  d
}|d }xDtdƒD ]8}yt||ƒ P W n  tk
�r†   t
jdƒ Y nX �qRW yt||ƒ |}W n   t||ƒ ‚ Y nX W d
|d
k	�rÐ|jƒ  t|ƒ X dS )zo Saves the contents of the image volume using the current contents
            of the **ImageVolume**.
        Fg      @zimage_volume.pyzimage_info.pyzlicense.txtTz.###Úwg      $@Nz.$$$é2   gš™™™™™¹?)rq   Úis_zip_filer   r	   r
   r   r   r   Úimages_coderR   rQ   rp   rD   r   Úimage_volume_codeÚlicense_textr   r   Úzip_filerh   Údont_copy_listÚwritestrr;   rl   Úranger   Ú	Exceptionrv   r   )	rj   rq   r»   r<   Znew_zfZcur_zfrK   Z	temp_nameÚir>   r>   r?   r·     s\    





zImageVolume.savec             C   sx   t |ƒ\}}| jrP| j|ƒ}|dk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¾   rq   r`   r<   )Úfilename)ra   rº   Ú_check_cacheÚZipFileReferencer,   rÄ   r¾   rq   rK   r-   r   r+   Z_ref)rj   r^   r`   r<   Ú
cache_fileÚrefÚresourcer>   r>   r?   r€   l  s"    


zImageVolume.image_resourcec             C   s2   t |ƒ\}}| jr| jj|ƒS tt| j|ƒƒS dS )z_ Returns the image data (i.e. file contents) for the specified image
            name.
        N)ra   rº   r¾   r;   r@   r   rq   )rj   r^   r`   r<   r>   r>   r?   Ú
image_data�  s    zImageVolume.image_datac             C   sB   x*| j D ] }t|jƒdks$||jkr|S qW tdj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ˆ   )rj   r^   r¤   r>   r>   r?   r£   š  s    
zImageVolume.volume_infoc             C   s   t ƒ gS )N)r¦   )rj   r>   r>   r?   Ú_info_default¨  s    zImageVolume._info_defaultc             C   s   | j ƒ S )N)Ú_load_image_info)rj   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 )N)r^   )r‰   r„   r>   r>   r?   rŠ   ²  s    z+ImageVolume._get_catalog.<locals>.<genexpr>)rU   r±   )rj   r>   r>   r?   Ú_get_catalog°  s    zImageVolume._get_catalogc             C   sF   t dd„ | jdddddƒjƒ D ƒƒ}djd	d„ | jD ƒƒ|d
< t| S )Nc             s   s   | ]\}}|t |ƒfV  qd S )N)rˆ   )r‰   rK   rX   r>   r>   r?   rŠ   µ  s   z5ImageVolume._get_image_volume_code.<locals>.<genexpr>r‹   rŒ   r�   Úaliasesrp   z,
c             s   s   | ]}|j V  qd S )N)r´   )r‰   r¤   r>   r>   r?   rŠ   º  s   r¤   )rU   rS   rT   r   r¤   ÚImageVolumeTemplate)rj   rC   r>   r>   r?   Ú_get_image_volume_code´  s    
z"ImageVolume._get_image_volume_codec             C   s   dj dd„ | jD ƒƒ}t| S )Nz,
c             s   s   | ]}|j V  qd S )N)r¥   )r‰   r¤   r>   r>   r?   rŠ   ¿  s    z/ImageVolume._get_images_code.<locals>.<genexpr>)r   r±   ÚImageVolumeImagesTemplate)rj   r±   r>   r>   r?   Ú_get_images_code¾  s    zImageVolume._get_images_codec             C   s   ddd  j dd„ | jD ƒƒS )Nz

%s
ú-éO   c             S   s   g | ]
}|j ‘qS r>   )rµ   )r‰   r¤   r>   r>   r?   r¬   Ä  s   z1ImageVolume._get_license_text.<locals>.<listcomp>)r   r¤   )rj   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}|jƒ }d|krZt|j	dƒdƒ}| j
|k ržx¨|D ]0}t|ƒ\}}	|	tkrj|jt|t||ƒd�ƒ qjW nnt| j dƒ}
t|
ƒrÂtt|
ƒdƒ}| j
|k �rx>t| j ƒD ]0}t|ƒ\}}	|	tkrÚ|jt|t||ƒd�ƒ qÚW t|ƒdk�r"|}njtdd„ |D ƒƒ}xJ|D ]B}|j|jƒ}|dk	�r:|||j< | |_|j|_|j|_d|_�q:W t|jƒ ƒ}|| _
|jd	d
„ d� x|D ]}| |_�q¨W |S )zM Returns the list of ImageInfo objects for the images in the volume.
        Ú zimage_info.pyr±   )rK   r^   r   c             S   s   g | ]}|j |f‘qS r>   )r^   )r‰   r„   r>   r>   r?   r¬     s   z0ImageVolume._load_image_info.<locals>.<listcomp>Nc             S   s   | j S )N)r^   )Úitemr>   r>   r?   Ú<lambda>  s    z.ImageVolume._load_image_info.<locals>.<lambda>)Úkey)rq   rR   r   r   rK   rº   r¾   rh   rM   r;   rp   r   ÚImageFileExtsÚappendr{   rd   r   r   r@   r   r¯   rU   Úgetr^   r   r…   r�   ÚlistÚvaluesr®   )rj   rp   r`   Z
old_imagesZ
cur_imagesrg   ÚnamesrK   rb   rc   Zimage_info_pathr±   Zcur_image_setZ	old_imageZ	cur_imager„   r>   r>   r?   rÏ   É  s`    








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_pathrK   r   rR   r   r   rp   )rj   r<   rÈ   r>   r>   r?   rÆ     s    zImageVolume._check_cacheN)(rw   rx   ry   r   rK   r   r¦   r¤   rŒ   r�   rÒ   r!   rq   r#   rº   r"   re   r¾   r{   r±   r   Úcatalogrp   r¼   r»   r½   rš   r·   r€   rË   r£   rÎ   rÐ   r(   rÑ   rÔ   rÖ   rÙ   rÏ   rÆ   r>   r>   r>   r?   r|   Á  s6   
k#
Tr|   c               @   s4   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ƒ}| jj|tƒ}|dk	r6|S tt| j	ƒ}t
|ƒsRt|ƒ t|| jƒ}t|dƒ�}|j|ƒ W dQ R X || _ d| _| jj|ƒS )z Loads the resource.
        rÚ   NrA   )rÈ   r¾   r;   r<   rÄ   Zimage_from_datar$   r   rä   r`   r   r   r:   rB   Zimage_from_file)rj   rÈ   rC   r„   Z	cache_dirr=   r>   r>   r?   ÚloadC  s    zZipFileReference.loadc             C   s   | j dkr| jƒ  | j S )NrÚ   )rÈ   ræ   )rj   r>   r>   r?   Ú_get_filenameg  s    
zZipFileReference._get_filenameN)rw   rx   ry   r"   re   r¾   r   r`   r<   r!   rÈ   r   rÅ   ræ   rç   r>   r>   r>   r?   rÇ   .  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.images)r¶   c             C   s"   | j |ƒ}|dk	r|jj|ƒS dS )z_ Returns the ImageInfo object corresponding to a specified
            **image_name**.
        N)Úfind_volumerå   rà   )rj   r^   r   r>   r>   r?   Ú
image_info†  s    
zImageLibrary.image_infoc             C   s>   |j dƒdk rd|dd…  }| j|ƒ}|dk	r:|j|ƒS dS )zG Returns an ImageResource object for the specified image name.
        rZ   r   z
@images:%sr[   N)r]   ré   r€   )rj   r^   r   r>   r>   r?   r€   �  s    

zImageLibrary.image_resourcec             C   sD   t |ƒ\}}| j}| j}x"||kr:|j|ƒ}|dkrdS qW || S )z‰ Returns the ImageVolume object corresponding to the specified
            **image_name** or None if the volume cannot be found.
        N)ra   rå   rÒ   rà   )rj   r^   r`   r<   rå   rÒ   r>   r>   r?   ré   ¡  s    

zImageLibrary.find_volumeNc             C   sÒ   |dkrt tdƒdƒ}t|ƒrn| j|ƒ}|dkr<td| ƒ‚|j| jkrT| j|jƒ || j|j< | jj	|ƒ n`t
|ƒrÂ| j}| j|ƒ}x,|D ]$}|j|kr¦| j|jƒ |||j< qŒW | j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%   rK   rå   Ú_duplicate_volumeÚvolumesrß   r   Ú	_add_pathÚextend)rj   r<   r   rå   rî   r>   r>   r?   Ú
add_volume²  s*    




zImageLibrary.add_volumec             C   s°   || j krtd| ƒ‚|dk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 |< | 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.zimage_volume.pyr   F)rK   rq   rº   )rå   r%   r   r)   r   r   rM   r@   r|   Z	trait_setrp   rR   r   r   r·   rî   rß   )rj   r`   rq   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„�xt|ƒD �]}|dd… d	ks–|jd
ƒdk r¢td| ƒ‚t	|ƒ\}}| j
|ƒ}|dkrÌtd| ƒ‚|jj|ƒ}|dkrìtd| ƒ‚|
j|ƒ |j||j|ƒƒ ||k�rB||k�rB|j|ƒ x|jD ]}|	j|ƒ �q.W |j|ƒ}|j|ƒ}|dk�rp|jƒ  ||< }|jj|ƒ qrW |
jdd„ d� |
|_t|ƒ|_t|	ƒ|_t|jƒ ƒ|_|jd|jƒ |jd|jƒ |jd|jƒ d}W d|jƒ  |�r t|ƒ X 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Ú   z.zip)rK   zThe '%s' file already exists.r¸   TNr[   ú@rZ   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 )N)r^   )rÛ   r>   r>   r?   rÜ   X  s    z&ImageLibrary.extract.<locals>.<lambda>)rÝ   zimage_volume.pyzimage_info.pyzlicense.txtF) r   r   r|   r   r%   r   r   Úsetr]   ra   ré   rå   rà   rß   rÀ   rË   Úaddr�   r£   r³   r§   r®   r±   rá   rÒ   râ   r¤   r¼   r»   r½   rl   r   )rj   r<   r§   r`   rc   r   rg   ÚerrorrÒ   r�   r±   r¤   r^   Zimage_volume_nameZimage_file_nameZimage_volumerê   Úkeywordr£   Zvinfor>   r>   r?   Úextract  sf    










zImageLibrary.extractc             C   sš   g }t tttjd ƒƒdƒ}t|ƒr4|j| j|ƒƒ |j| jt tdƒdƒƒƒ t	j
dƒ}|d k	r–d}tƒ dkrrd}x"|j|ƒD ]}|j| j|ƒƒ q~W |S )Nr   Úlibraryr[   ZTRAITS_IMAGESú;ÚWindowsrZ   )r   r   r   ÚsysÚargvr   rð   rï   r)   r   rà   r   Úsplit)rj   ÚresultZapp_libraryÚpathsÚ	separatorrq   r>   r>   r?   Ú_volumes_defaultv  s    

zImageLibrary._volumes_defaultc             C   s   t dd„ | jD ƒƒS )Nc             S   s   g | ]}|j |f‘qS r>   )rK   )r‰   r   r>   r>   r?   r¬   ”  s    z1ImageLibrary._catalog_default.<locals>.<listcomp>)rU   rî   )rj   r>   r>   r?   Ú_catalog_default“  s    zImageLibrary._catalog_defaultc             C   s   | j ƒ S )N)Ú_get_images_list)rj   r>   r>   r?   Ú_get_images˜  s    zImageLibrary._get_imagesc             C   s4   g }x| j D ]}|j|jƒ qW |jdd„ d� |S )z1 Returns the list of all library images.
        c             S   s   | j S )N)r^   )r„   r>   r>   r?   rÜ   §  s    z/ImageLibrary._get_images_list.<locals>.<lambda>)rÝ   )rî   rð   r±   r®   )rj   r±   r   r>   r>   r?   r  ž  s
    zImageLibrary._get_images_listc             C   sT   g }t |ƒrPxBt|ƒD ]6}t|ƒd dkr| jt||ƒƒ}|dk	r|j|ƒ qW |S )zz Returns a list of ImageVolume objects, one for each image library
            located in the specified **path**.
        r[   z.zipN)r   r   r   rì   r   rß   )rj   rq   rÿ   Úbaser   r>   r>   r?   rï   ¬  s    zImageLibrary._add_pathc             C   sœ   t |ƒ}t|ƒr˜t|d�}tt|ƒƒd }|jƒ }d|krht|jdƒdƒ}||_| j	|ƒ ||_
||_nt|||d�}|jtt|ƒt ƒk r”|jƒ  |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.
        )rq   r   zimage_volume.pyr   )rK   rq   r¾   N)r   r   re   r   r   rh   rM   r;   rK   Ú_add_aliasesrq   r¾   r|   rp   rR   r   r   r·   )rj   rq   rg   r`   rã   r   r>   r>   r?   rì   Â  s&    


zImageLibrary._add_volumec             C   sR   | j }|j}x@|j D ]6}||krB||| krBtd|||| f ƒ‚|||< qW 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Ò   rK   r%   )rj   r   rÒ   r`   Úvnamer>   r>   r?   r  ü  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%   )rj   r`   r>   r>   r?   rí     s    zImageLibrary._duplicate_volume)N)N)rw   rx   ry   rz   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è   q  s&   


*
0j:rè   )r2   r3   r4   r5   )r6   r7   r8   )Urz   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   rN   rQ   Zsix.moves._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_traitsr/   r0   r1   rÞ   rä   r¿   r¨   r°   rÓ   rÕ   r›   r@   rD   rM   rR   rY   ra   rd   re   r{   r¦   r|   rÇ   rè   r>   r>   r>   r?   Ú<module>   sV   0(@		]v:  oC   $