B
    ÔùÓ`­  ã               @   sP  U 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 ej	ej
eejf  Zeje Zejejeejf  Zejejej  ZdddgZej	e ed< ed	g Zeejd
œdd„Zdejeej	e dœdd„Zdeeeeedœdd„Zdejeeeedœdd„Zd eeeeeedœdd„Zd!eje eje eje dœdd„Z dS )"zeanyconfig.template.jinja2_ module

Template rendering module for jinja2-based template config files.
é    Né   )ÚutilsÚctxÚpathsÚfiltersÚRENDER_S_OPTSÚask)r   Úreturnc             C   s(   | dkrg } t jt  dd„ | D ƒ¡d�S )z7
    :param paths: A list of template search paths
    Nc             S   s   g | ]}t |ƒ‘qS © )Ústr)Ú.0Úpr
   r
   ú=/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/template/jinja2.pyú
<listcomp>)   s    ztmpl_env.<locals>.<listcomp>)Úloader)Újinja2ÚEnvironmentZFileSystemLoader)r   r
   r
   r   Útmpl_env!   s    r   )Útemplate_filer   r	   c             C   s,   t | j ¡ ƒ}|r&|gdd„ |D ƒ S |gS )aK  
    Make up a list of template search paths from given 'template_file'
    (absolute or relative path to the template file) and/or 'paths' (a list of
    template search paths given by user).

    NOTE: User-given 'paths' will take higher priority over a dir of
    template_file.

    :param template_file: Absolute or relative path to the template file
    :param paths: A list of template search paths
    :return: List of template paths ([str])

    >>> p = pathlib.Path
    >>> make_template_paths(p('/path/to/a/template'))
    ['/path/to/a']
    >>> make_template_paths(p('/path/to/a/template'), ['/tmp'])
    ['/path/to/a', '/tmp']
    >>> os.chdir('/tmp')
    >>> make_template_paths(p('./path/to/a/template'))
    ['/tmp/path/to/a']
    >>> make_template_paths(p('./path/to/a/template'), ['/tmp'])
    ['/tmp/path/to/a', '/tmp']
    c             S   s   g | ]}t |ƒ‘qS r
   )r   )r   r   r
   r
   r   r   I   s    z'make_template_paths.<locals>.<listcomp>)r   ÚparentÚresolve)r   r   Ztmpldirr
   r
   r   Úmake_template_paths-   s    r   )Útmpl_sr   r   r   r	   c          
   C   s–   |dkrt jg}yt|ƒ}W n8 tk
rT } zt d|›d|›�¡ | S d}~X Y nX |dk	rj|j |¡ |dkrvi }t 	t
jt|ƒ¡ | ¡jf |ŽS )aÙ  
    Compile and render given template string 'tmpl_s' with context 'context'.

    :param tmpl_s: Template string
    :param ctx: Context dict needed to instantiate templates
    :param paths: Template search paths
    :param filters: Custom filters to add into template engine
    :return: Compiled result (str)

    >>> render_s('aaa') == 'aaa'
    True
    >>> s = render_s('a = {{ a }}, b = "{{ b }}"', {'a': 1, 'b': 'bbb'})
    >>> assert s == 'a = 1, b = "bbb"'
    Nz!Something went wrong with: paths=z, exc=)ÚosÚcurdirr   ÚAssertionErrorÚwarningsÚwarnr   ÚupdateÚtypingÚcastr   r   Zfrom_stringÚrender)r   r   r   r   ÚenvÚexcr
   r
   r   Úrender_sN   s    r$   )r   r   r   r   r	   c             C   s\   t t| |ƒƒ}|dkr"t| ƒ ¡ S |dk	r6|j |¡ |dkrBi }| t | ¡j	¡j
f |ŽS )zí
    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    :param filters: Custom filters to add into template engine
    :return: Compiled result (str)
    N)r   r   ÚopenÚreadr   r   Zget_templateÚpathlibÚPathÚnamer!   )r   r   r   r   r"   r
   r
   r   Úrender_implv   s    	r*   F)Úfilepathr   r   r   r   r	   c       
   
   C   s†   t  | ¡}yt||||ƒS  tjjk
r€ } zF|s4‚ ttj› d|› d�ƒ}t  | 	¡ ¡ 
¡ }t||ƒ}	t|||	|ƒS d}~X Y nX dS )a˜  
    Compile and render template and return the result as a string.

    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    :param paths: Template search paths
    :param ask: Ask user for missing template location if True
    :param filters: Custom filters to add into template engine
    :return: Compiled result (str)
    z*** Missing template 'zP'. Please enter absolute or relative path starts from '.' to the template file: N)r'   r(   r*   r   Ú
exceptionsZTemplateNotFoundÚinputr   ÚlinesepÚstripr   r   )
r+   r   r   r   r   ZfpathZmtmplZusr_tmplZ
usr_tmpl_2Zpaths_2r
   r
   r   r!   �   s    

r!   )r+   Úcontentr	   c             K   sÀ   | dkr|dkrt dƒ‚y@|dkrBt t|¡}tt t| ¡f|ŽS t t|¡}t	|f|ŽS  t
k
rº } zD| rt| }nt t|¡dd… d }t d|›dtj› d|›�¡ dS d}~X Y nX dS )a/  
    Compile and render template and return the result as a string.

    :param filepath: Absolute or relative path to the template file
    :param content: Template content (str)
    :param options: Keyword options passed to :func:`render` defined above.
    :return: Compiled result (str) or None
    Nz.Either 'path' or 'content' must be some value!é
   z ...zFailed to compile 'z'. It may not be a template.z, exc=)Ú
ValueErrorr   Zfilter_optionsÚRENDER_OPTSr!   r   r    r   r   r$   Ú	Exceptionr   r   r   r.   )r+   r0   ÚoptionsZrender_optsZrender_s_optsr#   r   r
   r
   r   Ú
try_render¬   s    r6   )N)NNN)NNN)NNFN)NN)!Ú__doc__r'   r   r   r   r   Zjinja2.exceptionsÚ r   ZListÚUnionr   r(   ZPathsTÚOptionalZMaybePathsTÚDictZAnyZMaybeContextTÚIterableÚCallableZMaybeFiltersTr   Ú__annotations__r3   r   r   r   r$   r*   Úboolr!   r6   r
   r
   r
   r   Ú<module>   s<   

  
%     