3
<Ad]²#  ã               @   s¤   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ ddlZG d	d
„ d
eƒZdS )zz The default resource manager.

A resource manager locates and loads application resources such as images and
sounds etc.
é    N)Újoin)Ú
is_zipfileÚZipFile)Ú	HasTraitsÚInstanceÚList)Úget_path)ÚResourceFactory)ÚImageReferencec               @   sV   e Zd ZdZdddddgZeZeeƒZ	ddd	„Z
dd
d„Zdd„ Zdd„ Zdd„ ZdS )ÚResourceManagerz† The default resource manager.

    A resource manager locates and loads application resources such as images
    and sounds etc.
    z.pngz.jpgz.bmpz.gifz.icoNc             C   s|   t |tjƒs|g}g }xVt|ƒ| j D ]D}t |tjƒrB|j|ƒ q&t |tj	ƒrZ|j|ƒ q&|j
| j|ƒƒ q&W | j|||ƒS )z Locates an image. )Ú
isinstanceÚcollectionsÚSequenceÚlistÚextra_pathsÚsixÚstring_typesÚappendÚtypesÚ
ModuleTypeÚextendÚ_get_resource_pathÚ_locate_image)ÚselfÚ
image_nameÚpathÚsizeÚresource_pathÚitem© r   úz/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/pyface-6.1.2-py3.6.egg/pyface/resource/resource_manager.pyÚlocate_image7   s    zResourceManager.locate_imagec             C   s(   | j |||ƒ}|dk	r |jƒ }nd}|S )z Loads an image. N)r!   Úload)r   r   r   r   Ú	referenceÚimager   r   r    Ú
load_imageH   s
    
zResourceManager.load_imagec             C   s´  t jj|ƒ\}}t|ƒdkr(|g}|}n| j}|d }|dkrHddg}nd|d |d f ddg}�xJ|D �]@}	t|	tjƒräddlm	}
 xZ|D ]P}xJ|D ]B}d	|||f }y|
|	j
|ƒ}t| j|d
�S  tk
rØ   Y q˜X q˜W qŽW qjxZ|D ]R}tjt|	||ƒƒ}x:|D ]2}t jj|ƒ\}}||k�rt| j|d�}|S �qW qêW t|	dƒ}t jj|ƒ�r t|dƒ}x<|D ]4}y |j|| ƒ}t| j|d
�}|S    Y nX �qhW |	}d}xnt|ƒ �rt jj|ƒd jdƒ�rt jj|ƒd jdƒ�rt jj|ƒ\}}|dk�r|d | }n|}�qªW t|ƒrjt|ƒ}x‚dD ]z}xr|D ]j}yV|dk�rR|d }nd}|dk�rl|| d }|| | }|j|ƒ}t| j|d
�}|S    Y nX �q8W �q.W qjW dS )z­ Attempts to locate an image resource.

        If the image is found, an image resource reference is returned.
        If the image is NOT found None is returned.

        r   z.*NÚimagesÚ zimages/%dx%dé   )Úresource_stringz%s/%s%s)Údata)Úfilenamez
images.zipÚrú\ú/)r&   r'   )Úosr   ÚsplitextÚlenÚIMAGE_EXTENSIONSr   r   r   Úpkg_resourcesr)   Ú__name__r
   Úresource_factoryÚIOErrorÚglobr   Úisfiler   Úreadr   Ú
splitdriveÚ
startswithÚsplit)r   r   r   r   ÚbasenameÚ	extensionÚ
extensionsÚpatternZsubdirsÚdirnamer)   r   Z
searchpathr*   Ú	filenamesr+   Únot_usedr#   Úzip_filenameZzip_fileZ
image_dataÚfilepathÚzippathÚtailÚsubpathr   r   r    r   X   sˆ    

















zResourceManager._locate_imagec             C   s    t |dƒr|j}n
| j|ƒ}|S )z* Returns the resource path for an object. r   )Úhasattrr   Ú_get_default_resource_path)r   Úobjectr   r   r   r    r   Ö   s    

z"ResourceManager._get_resource_pathc             C   sH   g }x>t j|jƒD ].}y|jt|ƒƒ W q tk
r>   P Y qX qW |S )z2 Returns the default resource path for an object. )ÚinspectÚgetmroÚ	__class__r   r   ÚAttributeError)r   rK   r   Úklassr   r   r    rJ   á   s    z*ResourceManager._get_default_resource_path)N)N)r4   Ú
__module__Ú__qualname__Ú__doc__r2   r   r   r   r	   r5   r!   r%   r   r   rJ   r   r   r   r    r       s   

~r   )rS   r   r7   rL   r/   Úsysr   Úos.pathr   Úzipfiler   r   Z
traits.apir   r   r   Ztraits.util.resourcer   Z pyface.resource.resource_factoryr	   Z"pyface.resource.resource_referencer
   r   r   r   r   r   r    Ú<module>   s   0