o
    r|a                     @   sL   d Z ddlZddlmZ ddlmZ ddlmZ dd ZG dd	 d	eZ	dS )
zJ
This module provides a Loader class for locating and reading templates.

    N)common)defaults)Locatorc                  C   s   ddd} | S )Nc                 S   s   |du rt j}t| |t jS )zW
        Raises a TypeError exception if the given string is already unicode.

        N)r   ZSTRING_ENCODINGstrZDECODE_ERRORS)sencoding r   1/usr/lib/python3/dist-packages/pystache/loader.py
to_unicode   s   z$_make_to_unicode.<locals>.to_unicodeNr   )r
   r   r   r	   _make_to_unicode   s   
	r   c                   @   sV   e Zd ZdZ				dddZdd ZdddZdd	d
Zdd Zdd Z	dd Z
dS )Loaderz
    Loads the template associated to a name or user-defined object.

    All load_*() methods return the template as a unicode string.

    Nc                 C   sT   |du rt j}|du rt j}|du rt j}|du rt }|| _|| _|| _|| _dS )a  
        Construct a template loader instance.

        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.

          file_encoding: the name of the encoding to use when converting file
            contents to unicode.  Defaults to the package default.

          search_dirs: the list of directories in which to search when loading
            a template by name or file name.  Defaults to the package default.

          to_unicode: the function to use when converting strings of type
            str to unicode.  The function should have the signature:

              to_unicode(s, encoding=None)

            It should accept a string of type str and an optional encoding
            name and return a string of type unicode.  Defaults to calling
            Python's built-in function unicode() using the package string
            encoding and decode errors defaults.

        N)	r   ZTEMPLATE_EXTENSIONZFILE_ENCODINGZSEARCH_DIRSr   	extensionfile_encodingsearch_dirsr
   )selfr   r   r
   r   r   r   r	   __init__)   s   !
zLoader.__init__c                 C   s   t | jdS )N)r   )r   r   )r   r   r   r	   _make_locator\   s   zLoader._make_locatorc                 C   s   t |tr	t|S | ||S )an  
        Convert a string to unicode using the given encoding, and return it.

        This function uses the underlying to_unicode attribute.

        Arguments:

          s: a basestring instance to convert to unicode.  Unlike Python's
            built-in unicode() function, it is okay to pass unicode strings
            to this function.  (Passing a unicode string to Python's unicode()
            with the encoding argument throws the error, "TypeError: decoding
            Unicode is not supported.")

          encoding: the encoding to pass to the to_unicode attribute.
            Defaults to None.

        )
isinstancer   r
   )r   r   r   r   r   r	   r   _   s   
z
Loader.strc                 C   sD   t |}|du r| j}t dkr| ||ddS | ||S )zZ
        Read the template at the given path, and return it as a unicode string.

        NZWindows )r   readr   platformsystemr   replace)r   pathr   br   r   r	   r   v   s   
zLoader.readc                 C       |   }||| j}| |S )z
        Find and return the template with the given file name.

        Arguments:

          file_name: the file name of the template.

        )r   Z	find_filer   r   )r   	file_namelocatorr   r   r   r	   	load_file      	
zLoader.load_filec                 C   r   )z
        Find and return the template with the given template name.

        Arguments:

          name: the name of the template.

        )r   Z	find_namer   r   )r   namer   r   r   r   r	   	load_name   r!   zLoader.load_namec                 C   r   )z
        Find and return the template associated to the given object.

        Arguments:

          obj: an instance of a user-defined class.

          search_dirs: the list of directories in which to search.

        )r   Zfind_objectr   r   )r   objr   r   r   r   r	   load_object   s   
zLoader.load_object)NNNNr   )__name__
__module____qualname____doc__r   r   r   r   r    r#   r%   r   r   r   r	   r       s    	
3

r   )
r)   r   Zpystacher   r   Zpystache.locatorr   r   objectr   r   r   r   r	   <module>   s   