3
d                 @   sT   d Z ddlZddlZddlZddlZddlZdd ZdddZdd	d
Zdd Z	dS )a   Utility functions for managing and finding resources (e.g. images, files).

    get_path :           Returns the absolute path of a class or instance

    create_unique_name : Creates a name with a given prefix that is not in a
                         given list of existing names. The separator between
                         the prefix and the rest of the name can also be
                         specified (default is a '_')

    find_resource:       Given a setuptools project specification string
                         ('MyProject>=2.1') and a partial path leading from the
                         projects base directory to the desired resource, will
                         return either an opened file object or, if specified,
                         a full path to the resource.
    Nc             C   s   t | tk	rtj| r| }n| j}|j}tj| }|dkrvtj	j
tjd tj g}x.|D ]}tj	j|rZ|} P qZW ntj	j
|j} | S )zg Returns an absolute path for the specified path.

    'path' can be a string, class or instance.

    __main__r   )typestrinspectisclass	__class__
__module__sysmodulesospathdirnameargvgetcwdexists__file__)r   klassmodule_namemoduledirsd r   6/tmp/pip-build-7vycvbft/traits/traits/util/resource.pyget_path$   s    


r   _c             C   s2   d}| }x$||kr,| | t | }|d7 }q
W |S )z? Creates a name starting with 'prefix' that is not in 'names'.    )r   )prefixnames	separatorinamer   r   r   create_unique_nameF   s    
r!   Fc       
      C   s   t jdtdd y^ddlm}m}m} |j| }|rb|j|}t	j
j|j|}	t	j
j|	r^|	S  n
|||S W n   t	j
jtjd|}	t	j
j|	r|r|	S t|	dS |dkrdS |rt	j
jtj
d |S ytt	j
jtj
d |dS    dS Y nX dS )	a   Returns a file object or file path pointing to the desired resource.

    This function will find a desired resource file and return an opened file
    object. The main method of finding the resource uses the pkg_resources
    resource_stream method, which searches your working set for the installed
    project specified and appends the resource_path given to the project
    path, leading it to the file. If setuptools is not installed or it cannot
    find/open the resource, find_resource will use the sys.path[0] to find the
    resource if alt_path is defined.

    .. deprecated:: 6.3.0

    Parameters
    ----------
    project : str
        The name of the project to look for the resource in. Can be the name or
        a requirement string. Ex: 'MyProject', 'MyProject>1.0',
        'MyProject==1.1'
    resource_path : str
        The path to the file from inside the package. If the file desired is
        MyProject/data/image.jpg, resource_path would be 'data/image.jpg'.
    alt_path : str
        The path to the resource relative to the location of the application's
        top-level script (the one with __main__). If this function is called in
        code/scripts/myscript.py and the resource is code/data/image.jpg, the
        alt_path would be '../data/image.jpg'. This path is only used if the
        resource cannot be found using setuptools.
    return_path : bool
        Determines whether the function should return a file object or a full
        path to the resource.

    Returns
    -------
    file : file object or file path
        A file object containing the resource. If return_path is True, 'file'
        will be the full path to the resource. If the file is not found or
        cannot be opened, None is returned.
    z=find_resource is deprecated. Use importlib.resources instead.   )
stacklevelr   )resource_streamworking_setRequirementpurelibrbN)warningswarnDeprecationWarningpkg_resourcesr$   r%   r&   parsefindr   r   joinlocationr   	sysconfigr   openr	   )
projectresource_pathZalt_pathZreturn_pathr$   r%   r&   requirementdist	full_pathr   r   r   find_resourceS   s8    '


r8   c          	   C   sb   t jdtdd t| |}|dkr2td| |f t|d}|j|j  W dQ R X |j  dS )a   Store the content of a resource, given by the name of the project
        and the path (relative to the root of the project), into a newly
        created file.

        The first two arguments (project and resource_path) are the same
        as for the function find_resource in this module.  The third
        argument (filename) is the name of the file which will be created,
        or overwritten if it already exists.
        The return value in always None.

        .. deprecated:: 6.3.0
    z>store_resource is deprecated. Use importlib.resources instead.r"   )r#   Nz'Resource not found for project "%s": %swb)	r)   r*   r+   r8   RuntimeErrorr2   writereadclose)r3   r4   filenamefifor   r   r   store_resource   s    
rA   )r   )NF)
__doc__r   r   r	   r1   r)   r   r!   r8   rA   r   r   r   r   <module>   s   "

`