o
    x[h�  ã                   @   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mZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZmZmZmZ d d	lmZmZ ee ed
< zd dlmZ d dlm Z  W n e!yq   eZe "d¡Z Y nw e #e$¡Z%G dd„ deƒZ&G dd„ deƒZ'G dd„ de
j(ƒZ)	d"dd„Z*d"dd„Z+de,dee, fdd„Z-	d#d d!„Z.dS )$é    N)ÚEACCES)ÚOptionalÚType)Úhandlers)Úb64dÚ
json_dumps)ÚPaths)Ú
PER_ALWAYS)ÚMISSING_JINJA_PREFIXÚJinjaSyntaxParsingExceptionÚdetect_templateÚrender_string)Ú	load_jsonÚload_text_fileÚJUndefinedError)ÚUndefinedError)Úoperator_rez[-.]c                   @   ó   e Zd ZdS )ÚJinjaLoadErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   úC/usr/lib/python3/dist-packages/cloudinit/handlers/jinja_template.pyr   "   ó    r   c                   @   r   )ÚNotJinjaErrorNr   r   r   r   r   r   &   r   r   c                   @   s(   e Zd ZdgZdefdd„Zdd„ ZdS )ÚJinjaTemplatePartHandlerz## template: jinjaÚpathsc                 K   sL   t jj| tdd� || _i | _| dg ¡D ]}| ¡ D ]}|| j|< qqd S )Né   )ÚversionÚsub_handlers)r   ÚHandlerÚ__init__r	   r   r!   ÚgetÚ
list_types)Úselfr   Ú_kwargsÚhandlerÚctyper   r   r   r#   .   s   ÿÿz!JinjaTemplatePartHandler.__init__c              
   C   sÞ   |t jv rd S | j d¡}zt|||ƒ}W n ty1 }	 zt d|t|	ƒ¡ W Y d }	~	d S d }	~	ww |s6d S t  	|¡}
| j
 |
¡}|sLt d||
¡ d S |jdkr]| ||||||¡ d S |jdkrm| |||||¡ d S d S )NÚinstance_data_sensitivez=Ignoring jinja template for %s. Failed to render template. %szPIgnoring jinja template for %s. Could not find supported sub-handler for type %sr   é   )r   ÚCONTENT_SIGNALSr   Úget_runpathÚrender_jinja_payload_from_filer   ÚLOGÚwarningÚstrÚtype_from_starts_withr!   r$   Úhandler_versionÚhandle_part)r&   Údatar)   ÚfilenameÚpayloadÚ	frequencyÚheadersÚjinja_json_fileÚrendered_payloadÚeÚsubtypeÚsub_handlerr   r   r   r4   6   sH   
ÿü€ù	
ü
ÿ

ÿÿz$JinjaTemplatePartHandler.handle_partN)r   r   r   Úprefixesr   r#   r4   r   r   r   r   r   *   s    r   Fc              
   C   sª   t | ƒd dkrtdƒ‚i }d}tj |¡std| ƒ‚ztt|ƒƒ}W n# tyG } zd}t	|t
tfƒr>|jtkr>d| }t|ƒ|‚d}~ww t| |||ƒ}|sSdS |S )aK  Render a jinja template sourcing variables from jinja_vars_path.

    @param payload: String of jinja template content. Should begin with
        ## template: jinja\n.
    @param payload_fn: String representing the filename from which the payload
        was read used in error reporting. Generally in part-handling this is
        'part-##'.
    @param instance_data_file: A path to a json file containing variables that
        will be used as jinja template variables.

    @return: A string of jinja-rendered content with the jinja header removed.
        Returns None on error.
    r   ÚjinjazPayload is not a jinja templateNzFCannot render jinja template vars. Instance data not yet present at %sz"Loading Jinja instance data failedzGCannot render jinja template vars. No read permission on '%s'. Try sudo)r   r   ÚosÚpathÚexistsr   r   r   Ú	ExceptionÚ
isinstanceÚIOErrorÚOSErrorÚerrnor   Úrender_jinja_payload)r7   Ú
payload_fnÚinstance_data_fileÚdebugÚinstance_datar;   r<   Úmsgr   r   r   r.   ]   s:   ÿÿ
ÿÿ
€ø
ÿr.   c              
   C   s¬   t || dg ¡dd�}|rt dt|ƒ¡ zt| |ƒ}W n ttfy: } zt d|t	|ƒ¡ W Y d }~d S d }~ww dd„ t
 dt |¡D ƒ}|rTt d	|d
 |¡¡ |S )Nzbase64-encoded-keysT)Údecode_pathsÚinclude_key_aliaseszConverted jinja variables
%sz"Ignoring jinja template for %s: %sc                 S   s   g | ]
}d |  td¡ ‘qS )z'%s'Ú )Úreplacer
   )Ú.0Úvarr   r   r   Ú
<listcomp>™   s    ÿÿz(render_jinja_payload.<locals>.<listcomp>z%s[^\s]+z:Could not render jinja template variables in file '%s': %sz, )Úconvert_jinja_instance_datar$   r/   rL   r   r   Ú	TypeErrorr   r0   r1   ÚreÚfindallr
   Újoin)r7   rJ   rM   rL   Úinstance_jinja_varsr;   r<   Úwarningsr   r   r   rI   Š   s8   
ýÿ€þÿþýrI   Ú	orig_nameÚreturnc                 C   s   t  td| ¡}|| kr|S dS )a‹  Return a jinja variable alias, replacing any operators with underscores.

    Provide underscore-delimited key aliases to simplify dot-notation
    attribute references for keys which contain operators "." or "-".
    This provides for simpler short-hand jinja attribute notation
    allowing one to avoid quoting keys which contain operators.
    {{ ds.v1_0.config.user_network_config }} instead of
    {{ ds['v1.0'].config["user.network-config"] }}.

    :param orig_name: String representing a jinja variable name to scrub/alias.

    :return: A string with any jinja operators replaced if needed. Otherwise,
        none if no alias required.
    Ú_N)rX   Úsubr   )r]   Ú
alias_namer   r   r   Úget_jinja_variable_alias¨   s   rb   rQ   ú/r   c                 C   sÎ   i }dd„ |D ƒ}t |  ¡ ƒD ]U\}}|rd |||¡n|}||v r&t|ƒ}t|tƒrOt|||||d�||< t d|¡rN||  ¡ D ]\}	}
t	 
|
¡||	< qBn|||< |rdt|ƒ}|rdt	 
|| ¡||< q|S )z¢Process instance-data.json dict for use in jinja templates.

    Replace hyphens with underscores for jinja templates and decode any
    base64_encoded_keys.
    c                 S   s   g | ]}|  d d¡‘qS )ú-r_   )rR   )rS   rB   r   r   r   rU   Æ   s    z/convert_jinja_instance_data.<locals>.<listcomp>z	{0}{1}{2})ÚseprO   rP   zv\d+$)ÚsortedÚitemsÚformatr   rE   ÚdictrV   rX   ÚmatchÚcopyÚdeepcopyrb   )r5   Úprefixre   rO   rP   ÚresultÚkeyÚvalueÚkey_pathÚsubkeyÚsubvaluera   r   r   r   rV   ½   s2   

û€€rV   )F)rQ   rc   r   F)/rk   ÚloggingrA   rX   rH   r   Útypingr   r   Ú	cloudinitr   Úcloudinit.atomic_helperr   r   Úcloudinit.helpersr   Úcloudinit.settingsr	   Úcloudinit.templaterr
   r   r   r   Úcloudinit.utilr   r   rD   Ú__annotations__Újinja2.exceptionsr   r   Újinja2.lexerr   ÚImportErrorÚcompileÚ	getLoggerr   r/   r   r   r"   r   r.   rI   r1   rb   rV   r   r   r   r   Ú<module>   s<   
ý
4
ÿ
-ÿ