o
    w7e#                  	   @   s  U d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ	 d dlm
Z
 d dlmZ eed< zd dlmZ d d	lmZ d
ZeZW n eefyP   dZeZY nw eeZedejZedZdZG dd deZ dd Z!dd Z"dd Z#dddZ$dd Z%dddZ&dS )     N)Any)log)
type_utils)util)
write_file
JUndefined)DebugUndefined)TemplateTFz##\s*template:(.*)z)\$\{([A-Za-z0-9_.]+)\}|\$([A-Za-z0-9_.]+)zCI_MISSING_JINJA_VAR/c                   @   s    e Zd ZdZdd Zdd ZdS )UndefinedJinjaVariablez>Class used to represent any undefined jinja template variable.c                 C   s   dt | jf S )Nz%s%s)MISSING_JINJA_PREFIX_undefined_name)self r   5/usr/lib/python3/dist-packages/cloudinit/templater.py__str__2   s   zUndefinedJinjaVariable.__str__c                 C   s$   t |td}tdj| j|d)N zhUndefined jinja variable: "{this}-{other}". Jinja tried subtraction. Perhaps you meant "{this}_{other}"?)thisother)strreplacer   	TypeErrorformatr   )r   r   r   r   r   __sub__5   s   zUndefinedJinjaVariable.__sub__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r
   /   s    r
   c                    s    fdd}t || S )zThis does simple replacement of bash variable like templates.

    It identifies patterns like ${a} or $a and can also identify patterns like
    ${a.b} or $a.b which will look for a key 'b' in the dictionary rooted
    by key 'a'.
    c                    s   |  d}|d u r|  d}|d u rtdt|d} }t|dkrE| }t|ts;t	d|t
||f || }t|dks&| }t|tsZt	d||t
|f t|| S )N      z,Match encountered but no valid group present.zRCan not traverse into non-dictionary '%s' of type %s while looking for subkey '%s'z<Can not extract key '%s' from non-dictionary '%s' of type %s)groupRuntimeErrorcollectionsdequesplitlenpopleft
isinstancedictr   tuobj_namer   )matchnamepathselected_paramskeyparamsr   r   replacerG   s2   




zbasic_render.<locals>.replacer)BASIC_MATCHERsub)contentr1   r2   r   r0   r   basic_render?   s   r6   c                 C   s   dd }|  ddkr| dd\}}n| }d}t|}|s$dt| fS |d  }|dvr7td	| |d
krGt	sGt
d dt|fS |d
krRt	rRd
||fS dt|fS )Nc                 S   s4   |  drdnd}t| tddgdjdi || S )N
r   Tzjinja2.ext.do)	undefinedtrim_blocks
extensionsr   )endswith	JTemplater
   render)r5   r1   addr   r   r   jinja_renderf   s   z%detect_template.<locals>.jinja_renderr7   r   r   basic)jinjarA   z.Unknown template rendering type '%s' requestedrB   zcJinja not available as the selected renderer for desired template, reverting to the basic renderer.)findr$   TYPE_MATCHERr+   r6   r    lowerstrip
ValueErrorJINJA_AVAILABLELOGwarning)textr?   identrest
type_matchtemplate_typer   r   r   detect_templatee   s.   




rP   c                 C   s4   |si }t t| \}}}td| | |||S )Nz+Rendering content of '%s' using renderer %s)rP   r   	load_filerI   debug)fnr1   rO   rendererr5   r   r   r   render_from_file   s
   
rU     c                 C   s   t | |}tj|||d d S )N)mode)rU   r   r   )rS   outfnr1   rW   contentsr   r   r   render_to_file   s   
rZ   c                 C   s    |si }t | \}}} || |S )zRender string)rP   )r5   r1   _template_typerT   r   r   r   render_string   s   
r\   c                 C   s   t |d}| }W d    n1 sw   Y  | |d}t|| d }t| |dkr8tj| d S t	||dd d S )Nr)variantprefixr7   -w)omode)
openreadr\   rstripr   	load_yamlsysstdoutwriter   )r^   templateoutputr_   fhrY   
tpl_paramsr   r   r   render_cloudcfg   s   


rn   )rV   )N)'r"   rerg   typingr   	cloudinitr   loggingr   r)   r   cloudinit.atomic_helperr   __annotations__jinja2r   _DebugUndefinedr	   r<   rH   r   ImportErrorAttributeErrorobject	getLoggerr   rI   compileIrD   r3   r   r
   r6   rP   rU   rZ   r\   rn   r   r   r   r   <module>   s:   


&)
