a
    �Ï?e›  ã                   @   sR  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g d¢Zej	e ed< edg Zdeejdœd	d
„Zdejeej	ej 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 )!zfanyconfig.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u rg } t jt  dd„ | D ƒ¡d�S )zqGet the template environment object from given ``paths``.

    :param paths: A list of template search paths
    Nc                 S   s   g | ]}t |ƒ‘qS © )Ústr©Ú.0Úpr
   r
   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/template/jinja2.pyÚ
<listcomp>*   ó    ztmpl_env.<locals>.<listcomp>)Úloader)Újinja2ÚEnvironmentZFileSystemLoader)r   r
   r
   r   Útmpl_env!   s
    ÿr   )Útemplate_filer   r	   c                    s,   | j  ¡ ‰ |r&ˆ g‡ fdd„|D ƒ S ˆ gS )aE  Make a tempalte paths.

    Make up a list of template search paths from given ``template_file`` path
    (absolute or relative path to the template file) and/or ``paths``, a list
    of template search paths given by user or None.

    NOTE: User-given 'paths' will take higher priority over a dir of
    template_file.
    c                    s&   g | ]}t |ƒt ˆ ƒkrt |¡‘qS r
   )r   ÚpathlibÚPathr   ©Ztmpldirr
   r   r   <   s   ÿz'make_template_paths.<locals>.<listcomp>)ÚparentÚresolve)r   r   r
   r   r   Úmake_template_paths.   s    
r   )Útmpl_sr   r   r   r	   c              
   C   s    |du rt jg}zt|ƒ}W n> tyZ } z&t d|›d|›�¡ | W  Y d}~S d}~0 0 |durp|j |¡ |du r|i }t 	t
jt|ƒ¡ | ¡jf i |¤ŽS )aõ  Render a template as a str.

    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=ú, exc=)ÚosÚcurdirr   ÚAssertionErrorÚwarningsÚwarnr   ÚupdateÚtypingÚcastr   r   Zfrom_stringÚrender)r   r   r   r   ÚenvÚexcr
   r
   r   Úrender_sB   s$    ÿÿÿr*   )r   r   r   r   r	   c                 C   s`   t t| |ƒƒ}|du r"t| ƒ ¡ S |dur6|j |¡ |du rBi }| t | ¡j	¡j
f i |¤ŽS )a  Render implementation.

    :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_templater   r   Únamer'   )r   r   r   r   r(   r
   r
   r   Úrender_implk   s    
r.   F)Úfilepathr   r   r   r   r	   c           
   
   C   sŽ   t  | ¡}zt||||ƒW S  tjjyˆ } zR|s4‚ ttj› d|› d�ƒ}t  | 	¡ ¡ 
¡ }t||ƒ}	t|||	|ƒW  Y d}~S d}~0 0 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u r|du rt dƒ‚zL|du rHt t|¡}tt t| ¡fi |¤ŽW S t t|¡}t	|fi |¤ŽW S  t
yÖ } zZ| r~| }nt t|¡dd… d }t d|›dtj› d|›d| › d	|›�
¡ W Y d}~dS d}~0 0 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.r   z, filepath=z
, options=)Ú
ValueErrorr   Zfilter_optionsÚRENDER_OPTSr'   r%   r&   r   r   r*   Ú	Exceptionr"   r#   r   r2   )r/   r4   ÚoptionsZrender_optsZrender_s_optsr)   r   r
   r
   r   Ú
try_render¡   s0    
ÿÿþþÿr:   )N)N)NNN)NNN)NNFN)NN)!Ú__doc__r   r   r%   r"   r   Zjinja2.exceptionsÚ r   ÚListÚUnionr   r   ZPathsTÚOptionalZMaybePathsTÚDictÚAnyZMaybeContextTÚIterableÚCallableZMaybeFiltersTr   Ú__annotations__r7   r   r   r   r*   r.   Úboolr'   r:   r
   r
   r
   r   Ú<module>   s`   

 ÿ
þ   þý)  ÿþ    ýý  ÿ
þ