a
    §ˆc~/  ã                   @   sÔ   d 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Zddl	m
Z
mZ zddlmZ W n eyv   ddlmZ Y n0 ddlmZmZmZ ddlmZ ddlmZ dd	lmZ G d
d„ deƒZdd„ Zdd„ 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)Úfiles)Ú	HasTraitsÚInstanceÚList)Úget_path)ÚResourceFactory)ÚImageReferencec                   @   sR   e Zd ZdZg d¢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   sx   t |tjjƒs|g}g }t|ƒ| j D ]B}t |tƒr@| |¡ q&t |tj	ƒrX| |¡ q&| 
|  |¡¡ q&|  |||¡S )aG   Locates an image.

        Parameters
        ----------
        image_name : str
            Name of the image file.
        path : list of (str or ModuleType)
            Paths from which image files will be searched. Note that for each
            path, a subdirectory named 'images' will be search first.
            The first match will be returned.
        size : tuple of (m: int, n: int), optional
            Specific size of the image requested. If provided, then
            the subdirectory ``images/{m}x{n}`` will be searched first,
            followed by the ``images`` subdirectory and its containing folder.
            Default is None.

        Returns
        -------
        image_ref : ImageReference or None
            ImageReference to the image found, or None if no matching images
            are found.
        )Ú
isinstanceÚcollectionsÚabcÚSequenceÚlistÚextra_pathsÚstrÚappendÚtypesÚ
ModuleTypeÚextendÚ_get_resource_pathÚ_locate_image)ÚselfÚ
image_nameÚpathÚsizeÚresource_pathÚitem© r    úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/resource/resource_manager.pyÚlocate_image>   s    
zResourceManager.locate_imagec                 C   s(   |   |||¡}|dur | ¡ }nd}|S )z Loads an image. N)r"   Úload)r   r   r   r   Ú	referenceÚimager    r    r!   Ú
load_imaged   s
    
zResourceManager.load_imagec              	      s˜  t j |¡\‰ }t|ƒdkr(|g}|}n| j}|d }|du rHddg}nd|d |d f ddg}‡ fdd	„|D ƒ}|D �]}	t|	tjƒrÄzt|	||ƒ}
W n t	y°   Y qxY n0 t
| j|
d
�  S |D ]V}t t|	||ƒ¡}|D ]:}t j |¡\}}||v rât
| j|d�}|      S qâqÈt|	dƒ}t j |¡�r†t|dƒ}|D ]>}z*| ˆ | ¡}t
| j|d
�}|W     S    Y n0 �qF|	}d}t|ƒ�søt j |¡d  d¡�røt j |¡d  d¡�røt j |¡\}}|dk�rð|d | }n|}�qŽt|ƒrxt|ƒ}dD ]„}|D ]x}zd|dk�r.|d }nd}|dk�rH|| d }|ˆ  | }| |¡}t
| j|d
�}|W       S    Y n0 �q�qqxd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é   c                    s   g | ]}ˆ | ‘qS r    r    )Ú.0Ú	extension©Úbasenamer    r!   Ú
<listcomp>‘   ó    z1ResourceManager._locate_image.<locals>.<listcomp>)Údata)Úfilenamez
images.zipÚrú\ú/)r'   r(   )Úosr   ÚsplitextÚlenÚIMAGE_EXTENSIONSr   r   r   Ú_find_resource_dataÚOSErrorr   Úresource_factoryÚglobr   Úisfiler   Úreadr   Ú
splitdriveÚ
startswithÚsplit)r   r   r   r   r+   Ú
extensionsÚpatternÚsubdirsZimage_filenamesÚdirnamer0   r   Ú	filenamesr1   Znot_usedr$   Úzip_filenameÚzip_fileZ
image_dataÚfilepathÚzippathÚtailÚsubpathr    r,   r!   r   t   s’    


ÿ
ÿ
ÿ

ÿ
ÿþý




ÿzResourceManager._locate_imagec                 C   s    t |dƒr|j}n
|  |¡}|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   sF   g }t  |j¡D ]0}z| t|ƒ¡ W q ty>   Y  qBY q0 q|S )z2 Returns the default resource path for an object. )ÚinspectÚgetmroÚ	__class__r   r	   ÚAttributeError)r   rO   r   Úklassr    r    r!   rN   þ   s    z*ResourceManager._get_default_resource_path)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   r   r   r   r
   r;   r"   r&   r   r   rN   r    r    r    r!   r   '   s   
&
r   c                 C   s‚   | j du s| j jdu rptj | j¡}tjj|g| d¡¢R Ž }t|dƒ�}| 	¡ W  d  ƒ S 1 sf0    Y  t
| ƒ |¡ ¡ S )a�   Return package data in bytes for the given module and resource path.

    Parameters
    ----------
    module : ModuleType
        A module from which package data will be discovered.
        If the module name does not conform to the package requirement, then
        its "__file__" attribute is used for locating the directory to search
        for resource files.
    rel_path : str
        "/"-separated path for loading data file.

    Returns
    -------
    data : bytes
        Loaded data in bytes.

    Raises
    ------
    OSError
        If the path referenced does not resolve to an existing file or the
        file cannot be read.
    Nr4   Úrb)Ú__spec__Úsubmodule_search_locationsr5   r   rE   Ú__file__r   rA   Úopenr>   r   ÚjoinpathÚ
read_bytes)ÚmoduleÚrel_pathZmodule_dir_pathr   Úfpr    r    r!   Ú_get_package_data  s    

ÿ&ÿrc   c              
   C   sT   |D ]B}|D ]8}d||f }zt | |ƒW     S  tyB   Y q0 qqtdƒ‚dS )aL   For the given module, search directories and names, find a matching
    resource file and return its content as bytes.

    Parameters
    ----------
    module : ModuleType
        A module from which package data will be discovered.
        If the module name does not conform to the package requirement, then
        its "__file__" attribute is used for locating the directory to search
        for resource files.
    subdirs : list of str
        Name of subdirectories to try. Each value can be a "/"-separated
        string to represent more nested subdirectories.
    filenames : list of str
        File names to try.

    Returns
    -------
    data : bytes
        Loaded data in bytes.

    Raises
    ------
    OSError
        If the path referenced does not resolve to an existing file or the
        file cannot be read.
    z%s/%sz:Unable to load data for the given module and search paths.N)rc   r:   )r`   rD   rF   r   r1   Z
searchpathr    r    r!   r9   4  s    
ÿr9   )rX   Úcollections.abcr   r<   rP   r5   Úos.pathr   r   Úzipfiler   r   Úimportlib.resourcesr   ÚImportErrorZimportlib_resourcesZ
traits.apir   r   r   Ztraits.util.resourcer	   Z pyface.resource.resource_factoryr
   Z"pyface.resource.resource_referencer   r   rc   r9   r    r    r    r!   Ú<module>
   s&    i%