3 \�me� � @ s U d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm Z d dl mZ ey$d dl mZ d dl mZ dZeZW n eefk r� d ZeZY nX eje�Zejd ej�Zejd�ZdZG d d� de�Zdd� Zdd� Z dd� Z!ddd�Z"dd� Z#ddd�Z$dS )� N)�Any)� type_utils)�util)� write_file)�DebugUndefined)�TemplateTFz##\s*template:(.*)z)\$\{([A-Za-z0-9_.]+)\}|\$([A-Za-z0-9_.]+)zCI_MISSING_JINJA_VAR/c @ s e Zd ZdZdd� Zdd� ZdS )�UndefinedJinjaVariablez>Class used to represent any undefined jinja template variable.c C s dt | jf S )Nz%s%s)�MISSING_JINJA_PREFIX�_undefined_name)�self� r �/usr/lib/python3.6/templater.py�__str__2 s zUndefinedJinjaVariable.__str__c C s( t |�jtd�}tdj| 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 zUndefinedJinjaVariable.__sub__N)�__name__� __module__�__qualname__�__doc__r r r r r r r / s r c s � fdd�}t j|| �S )z�This 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 kr| j d�}|d kr,td��tj|jd��}� }xBt|�dkr�|j� }t|t�sxt d|t j|�|f ��|| }qBW |j� }t|t�s�t 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 �tuZobj_namer )�match�name�pathZselected_params�key)�paramsr r �replacerG s( zbasic_render.<locals>.replacer)� BASIC_MATCHER�sub)�contentr, r- r )r, r �basic_render? s r1 c C s� dd� }| j d�d kr(| jdd�\}}n| }d}tj|�}|sHdt| fS |jd�j� j� }|dkrntd| ��|dkr�t r�t jd � dt|fS |dkr�t r�d||fS dt|fS d S )Nc S s0 | j d�rdnd}t| tddgd�jf |�| S )N� r Tz jinja2.ext.do)Z undefinedZtrim_blocks� extensions)�endswith� JTemplater Zrender)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"