HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/handlers/__pycache__/jinja_template.cpython-310.pyc
o

�AdD�@sTUddlZddlZddlZddlmZddlmZmZddlm	Z	ddlm
Zddlm
Z
ddlmZddlmZmZmZdd	lmZmZmZmZeeed
<zddlmZddlmZWne ymeZe�!d
�ZYnwe�"e#�Z$Gdd�de�Z%Gdd�de�Z&Gdd�de	j'�Z(	d"dd�Z)d"dd�Z*de+dee+fdd�Z,	d#d d!�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@�eZdZdS)�JinjaLoadErrorN��__name__�
__module__�__qualname__�rr�C/usr/lib/python3/dist-packages/cloudinit/handlers/jinja_template.pyr �rc@r)�
NotJinjaErrorNrrrrrr$rrc@s(eZdZdgZdefdd�Zdd�ZdS)�JinjaTemplatePartHandlerz## template: jinja�pathscKsLtjj|tdd�||_i|_|�dg�D]}|��D]}||j|<qqdS)N�)�version�sub_handlers)r�Handler�__init__rrr!�get�
list_types)�selfr�_kwargs�handler�ctyperrrr#,s��z!JinjaTemplatePartHandler.__init__cCs�|tjvrdS|j�d�}t|||�}|sdSt�|�}	|j�|	�}
|
s-t�	d||	�dS|
j
dkr>|
�||||||�dS|
j
dkrN|
�|||||�dSdS)N�instance_data_sensitivezPIgnoring jinja template for %s. Could not find supported sub-handler for type %sr�)r�CONTENT_SIGNALSr�get_runpath�render_jinja_payload_from_file�type_from_starts_withr!r$�LOG�warning�handler_version�handle_part)r&�datar)�filename�payload�	frequency�headers�jinja_json_file�rendered_payload�subtype�sub_handlerrrrr34s4
�
�
�

��z$JinjaTemplatePartHandler.handle_partN)rrr�prefixesrr#r3rrrrr(srFc
Cs�t|�ddkrtd��i}d}tj�|�std|��ztt|��}Wn#tyG}zd}t	|t
tf�r>|jt
kr>d|}t|�|�d}~wwt||||�}|sSdS|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.
    r�jinjazPayload 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)r6�
payload_fn�instance_data_file�debug�
instance_datar:�e�msgrrrr.Qs:��
��
��
�r.c
Cs�t||�dg�dd�}|rt�dt|��zt||�}Wnttfy:}zt�d|t	|��WYd}~dSd}~wwdd�t
�dt|�D�}|rTt�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$r0rJr
r�	TypeErrorrr1�str�re�findallr	�join)r6rHrKrJ�instance_jinja_varsr:rL�warningsrrrrG~s8
�������rG�	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)rX�subr)r]�
alias_namerrr�get_jinja_variable_alias�srbrP�/rcCs�i}dd�|D�}t|���D]U\}}|rd�|||�n|}||vr&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.
    cSsg|]}|�dd��qS)�-r_)rQ)rRr@rrrrT�sz/convert_jinja_instance_data.<locals>.<listcomp>z	{0}{1}{2})�seprNrOzv\d+$)�sorted�items�formatrrC�dictrUrX�match�copy�deepcopyrb)r4�prefixrerNrO�result�key�value�key_path�subkey�subvaluerarrrrU�s2

���rU)F)rPrcrF).rkr?rXrFr�typingrr�	cloudinitrr�logging�cloudinit.helpersr�cloudinit.settingsr�cloudinit.templaterr	r
r�cloudinit.utilrr
rrrB�__annotations__�jinja2.exceptionsrr�jinja2.lexerr�ImportError�compile�	getLoggerrr0rrr"rr.rGrWrbrUrrrr�<module>s:
�
*
�
-�