Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/handlers/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/handlers/__pycache__/jinja_template.cpython-38.pyc

U

�AdD�@sPUddlZddlZddlZddlmZddlmZmZddlm	Z	ddlm
Zddlm
Z
ddlmZddlmZmZmZdd	lmZmZmZmZeeed
<zddlmZddlmZWn"e k
r�eZe�!d
�ZYnXe�"e#�Z$Gdd�de�Z%Gdd�de�Z&Gdd�de	j'�Z(d!dd�Z)d"dd�Z*e+ee+d�dd�Z,d#dd �Z-dS)$�N)�EACCES)�Optional�Type)�handlers)�log)�Paths)�
PER_ALWAYS)�MISSING_JINJA_PREFIX�detect_template�
render_string)�b64d�
json_dumps�	load_file�	load_json�JUndefinedError)�UndefinedError)�operator_rez[-.]c@seZdZdS)�JinjaLoadErrorN��__name__�
__module__�__qualname__�rr�C/usr/lib/python3/dist-packages/cloudinit/handlers/jinja_template.pyr src@seZdZdS)�
NotJinjaErrorNrrrrrr$src@s(eZdZdgZed�dd�Zdd�ZdS)�JinjaTemplatePartHandlerz## template: jinja)�pathscKsLtjj|tdd�||_i|_|�dg�D]}|��D]}||j|<q6q*dS)N�)�version�sub_handlers)r�Handler�__init__rrr�getZ
list_types)�selfrZ_kwargsZhandler�ctyperrrr!,sz!JinjaTemplatePartHandler.__init__cCs�|tjkrdS|j�d�}t|||�}|s.dSt�|�}	|j�|	�}
|
sZt�	d||	�dS|
j
dkrz|
�||||||�n|
j
dkr�|
�|||||�dS)NZinstance_data_sensitivezPIgnoring jinja template for %s. Could not find supported sub-handler for type %sr�)rZCONTENT_SIGNALSrZget_runpath�render_jinja_payload_from_fileZtype_from_starts_withrr"�LOG�warningZhandler_version�handle_part)r#�datar$�filename�payloadZ	frequencyZheadersZjinja_json_file�rendered_payloadZsubtypeZsub_handlerrrrr)4sH
�
�
�
�z$JinjaTemplatePartHandler.handle_partN)rrr�prefixesrr!r)rrrrr(srFc
Cs�t|�ddkrtd��i}d}tj�|�s8td|��ztt|��}WnPtk
r�}z2d}t	|t
tf�r~|jt
kr~d|}t|�|�W5d}~XYnXt||||�}|s�dS|S)aRRender a jinja template payload sourcing variables from jinja_vars_path.

    @param payload: String of jinja template content. Should begin with
        ## template: jinja
.
    @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.
    rZjinjazPayload 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�existsrrr�	Exception�
isinstance�IOError�OSError�errnor�render_jinja_payload)r,�
payload_fnZinstance_data_file�debug�
instance_datar-�e�msgrrrr&Qs<��
���r&c
Cs�t||�dg�dd�}|r*t�dt|��zt||�}Wn>ttfk
rv}zt�d|t	|��WY�dSd}~XYnXdd�t
�dt|�D�}|r�t�d	|d
�
|��|S)Nzbase64-encoded-keysT)�decode_paths�include_key_aliaseszConverted jinja variables
%sz"Ignoring jinja template for %s: %scSsg|]}d|�td��qS)z'%s'�)�replacer	)�.0�varrrr�
<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'r9r
r�	TypeErrorrr(�str�re�findallr	�join)r,r8r:r9Zinstance_jinja_varsr-r;�warningsrrrr7~s8
�����r7)�	orig_name�returncCst�td|�}||kr|SdS)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)rG�subr)rK�
alias_namerrr�get_jinja_variable_alias�srPr?�/rcCs�i}dd�|D�}t|���D]�\}}|r8d�|||�n|}||krLt|�}t|t�r�t|||||d�||<t�d|�r�||��D]\}	}
t	�
|
�||	<q�n|||<|rt|�}|rt	�
||�||<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.
    cSsg|]}|�dd��qS)�-rM)r@)rAr0rrrrC�sz/convert_jinja_instance_data.<locals>.<listcomp>z	{0}{1}{2})�sepr=r>zv\d+$)�sorted�items�formatrr3�dictrDrG�match�copy�deepcopyrP)r*�prefixrSr=r>�result�key�valueZkey_pathZsubkeyZsubvaluerOrrrrD�s.
�
rD)F)F)r?rQrF).rYr/rGr6r�typingrrZ	cloudinitrrZloggingZcloudinit.helpersrZcloudinit.settingsrZcloudinit.templaterr	r
rZcloudinit.utilrr
rrr2�__annotations__Zjinja2.exceptionsrrZjinja2.lexerr�ImportError�compileZ	getLoggerrr'rrr rr&r7rFrPrDrrrr�<module>s>
*�
-
�