o
    r|a!  ã                   @   sH   d Z ddlZddlZddlZddlmZ ddlmZ G dd„ deƒZ	dS )zC
This module provides a Locator class for finding template files.

é    N)ÚTemplateNotFoundError)Údefaultsc                   @   sZ   e Zd Zddd„Zdd„ Zdd„ Zddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
ddd„ZdS )ÚLocatorNc                 C   s   |du rt j}|| _dS )a  
        Construct a template locator.

        Arguments:

          extension: the template file extension, without the leading dot.
            Pass False for no extension (e.g. to use extensionless template
            files).  Defaults to the package default.

        N)r   ZTEMPLATE_EXTENSIONÚtemplate_extension)ÚselfÚ	extension© r   ú2/usr/lib/python3/dist-packages/pystache/locator.pyÚ__init__   s   
zLocator.__init__c                 C   s:   t |dƒsdS tj|j }t |dƒsdS |j}tj |¡S )a#  
        Return the directory containing an object's defining class.

        Returns None if there is no such directory, for example if the
        class was defined in an interactive Python session, or in a
        doctest that appears in a text file (rather than a Python file).

        Ú
__module__NÚ__file__)ÚhasattrÚsysÚmodulesr   r   ÚosÚpathÚdirname)r   ÚobjÚmoduler   r   r   r	   Úget_object_directory!   s   
	
zLocator.get_object_directoryc                 C   s&   |j j}dd„ }t d||¡dd… S )aÈ  
        Return the canonical template name for an object instance.

        This method converts Python-style class names (PEP 8's recommended
        CamelCase, aka CapWords) to lower_case_with_underscords.  Here
        is an example with code:

        >>> class HelloWorld(object):
        ...     pass
        >>> hi = HelloWorld()
        >>>
        >>> locator = Locator()
        >>> locator.make_template_name(hi)
        'hello_world'

        c                 S   s   d|   d¡ ¡  S )NÚ_r   )ÚgroupÚlower)Úmatchr   r   r	   ÚreplJ   s   z(Locator.make_template_name.<locals>.replz[A-Z]é   N)Ú	__class__Ú__name__ÚreÚsub)r   r   Útemplate_namer   r   r   r	   Úmake_template_name7   s   zLocator.make_template_namec                 C   s.   |}|du r	| j }|dur|tjj| 7 }|S )zª
        Generate and return the file name for the given template name.

        Arguments:

          template_extension: defaults to the instance's extension.

        NF)r   r   r   Úextsep)r   r    r   Ú	file_namer   r   r	   Úmake_file_nameO   s   	zLocator.make_file_namec                 C   s0   |D ]}t j ||¡}t j |¡r|  S qdS )zr
        Search for the given file, and return the path.

        Returns None if the file is not found.

        N)r   r   ÚjoinÚexists)r   Úsearch_dirsr#   Údir_pathZ	file_pathr   r   r	   Ú
_find_pathb   s   ÿzLocator._find_pathc                 C   s0   |   ||¡}|du rtdt|ƒt|ƒf ƒ‚|S )zJ
        Return the path to a template with the given file name.

        NzFile %s not found in dirs: %s)r)   r   Úrepr)r   r'   r#   r   r   r   r	   Ú_find_path_requiredp   s   ÿzLocator._find_path_requiredc                 C   s   |   ||¡S )z×
        Return the path to a template with the given file name.

        Arguments:

          file_name: the file name of the template.

          search_dirs: the list of directories in which to search.

        )r+   )r   r#   r'   r   r   r	   Ú	find_file~   s   zLocator.find_filec                 C   s   |   |¡}|  ||¡S )zÑ
        Return the path to a template with the given name.

        Arguments:

          template_name: the name of the template.

          search_dirs: the list of directories in which to search.

        )r$   r+   )r   r    r'   r#   r   r   r	   Ú	find_name‹   s   
zLocator.find_namec                 C   sH   |du r|   |¡}|  |¡}|  |¡}|dur|g| }|  ||¡}|S )zR
        Return the path to a template associated with the given object.

        N)r!   r$   r   r+   )r   r   r'   r#   r    r(   r   r   r   r	   Úfind_objectš   s   



zLocator.find_object)N)r   r   Ú__qualname__r
   r   r!   r$   r)   r+   r,   r-   r.   r   r   r   r	   r      s    

r   )
Ú__doc__r   r   r   Zpystache.commonr   Zpystacher   Úobjectr   r   r   r   r	   Ú<module>   s   