
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me                 @   s
  U d dl Z 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 ey$d dlmZ d dlmZ dZeZW n  eefk
r   d	ZeZY nX ejeZejd
ejZej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)
type_utils)util)
write_file)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   /usr/lib/python3.6/templater.py__str__2   s    zUndefinedJinjaVariable.__str__c             C   s(   t |jtd}tdj| j|dd S )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 j|| 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   | j d}|d kr| j d}|d kr,tdtj|jd} }xBt|dkr|j }t|tsxt	d|t
j||f || }qBW |j }t|tst	d||t
j|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   tuZobj_namer   )matchnamepathZselected_paramskey)paramsr   r   replacerG   s(    



zbasic_render.<locals>.replacer)BASIC_MATCHERsub)contentr,   r-   r   )r,   r   basic_render?   s    r1   c             C   s   dd }| j dd
kr(| jdd\}}n| }d}tj|}|sHdt| fS |jdj j }|dkrntd| |dkrt	 rt
jd	 dt|fS |dkrt	rd||fS dt|fS d S )Nc             S   s0   | j drdnd}t| tddgdjf || S )N
r   Tzjinja2.ext.do)Z	undefinedZtrim_blocks
extensions)endswith	JTemplater   Zrender)r0   r,   addr   r   r   jinja_renderf   s    z%detect_template.<locals>.jinja_renderr2   r   r   basicjinjaz.Unknown template rendering type '%s' requestedzcJinja not available as the selected renderer for desired template, reverting to the basic renderer.)r9   r8   )findr"   TYPE_MATCHERr(   r1   r   lowerstrip
ValueErrorJINJA_AVAILABLELOGZwarning)textr7   ZidentrestZ
type_matchtemplate_typer   r   r   detect_templatee   s(    



rE   c             C   s4   |si }t tj| \}}}tjd| | |||S )Nz+Rendering content of '%s' using renderer %s)rE   r   Z	load_filerA   debug)fnr,   rD   rendererr0   r   r   r   render_from_file   s
    rI     c             C   s   t | |}tj|||d d S )N)mode)rI   r   r   )rG   Zoutfnr,   rK   contentsr   r   r   render_to_file   s    
rM   c             C   s    |si }t | \}}} || |S )zRender string)rE   )r0   r,   Z_template_typerH   r   r   r   render_string   s    rN   c       	      C   s   t |d}|j }W d Q R X | |d}t||j d }|r\tj|dd}|s\td| |dkrrtjj	| nt
||dd	 d S )
Nr)variantprefixr2   T)defaultz.Cannot render template file %s - invalid yaml.-w)Zomode)openreadrN   rstripr   Z	load_yamlr   sysstdoutwriter   )	rP   templateoutputZis_yamlrQ   ZfhrL   Z
tpl_paramsoutr   r   r   render_template   s    

r^   )rJ   )N)%r    ZloggingrerX   Ztypingr   Z	cloudinitr   r'   r   Zcloudinit.atomic_helperr   Z
JUndefinedZjinja2r   Z_DebugUndefinedr   r5   r@   ImportErrorAttributeErrorobjectZ	getLoggerr   rA   compileIr<   r.   r	   r   r1   rE   rI   rM   rN   r^   r   r   r   r   <module>   s8    


&)
