
    x[h                        % S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJr  S SKJrJrJrJr  S S	KJrJr  \\   \S
'    S SKJr  S SKJ r   \RF                  " \$5      r% " S S\5      r& " S S\5      r' " S S\
RP                  5      r) SS jr*SS jr+S\,S\\,   4S jr- SS jr.g! \! a    \r\RD                  " S5      r  Nwf = f)    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                       \ rS rSrSrg)JinjaLoadError"    N__name__
__module____qualname____firstlineno____static_attributes__r       C/usr/lib/python3/dist-packages/cloudinit/handlers/jinja_template.pyr   r   "       r   r   c                       \ rS rSrSrg)NotJinjaError&   r   Nr   r   r   r   r"   r"   &   r    r   r"   c                   .    \ rS rSrS/rS\4S jrS rSrg)JinjaTemplatePartHandler*   z## template: jinjapathsc                     [         R                  R                  U [        SS9  Xl        0 U l        UR                  S/ 5       H(  nUR                  5        H  nX0R
                  U'   M     M*     g )N   )versionsub_handlers)r   Handler__init__r
   r'   r+   get
list_types)selfr'   _kwargshandlerctypes        r   r-   !JinjaTemplatePartHandler.__init__.   s`    !!$
A!>
{{>26G ++-+2!!%( . 7r   c                 ,   U[         R                  ;   a  g U R                  R                  S5      n [	        XCU5      nU(       d  g [         R                  " U5      n
U R                  R                  U
5      nU(       d  [        R                  SUU
5        g UR                  S:X  a  UR                  XX8XV5        g UR                  S:X  a  UR                  XX8U5        g g ! [
         a*  n	[        R                  SU[        U	5      5         S n	A	g S n	A	ff = f)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)r0   datar3   filenamepayload	frequencyheadersjinja_json_filerendered_payloadesubtypesub_handlers               r   r@   $JinjaTemplatePartHandler.handle_part6   s
   H,,,**001JK	=?   001AB''++G4KK+	 &&!+##X ((A-##X .3 + 	KK0A	 	s   C 
D) DD)r'   r+   N)	r   r   r   r   prefixesr	   r-   r@   r   r   r   r   r%   r%   *   s    $%H3e 3$r   r%   c                    [        U 5      S   S:w  a  [        S5      e0 nSn[        R                  R	                  U5      (       d  [        SU-  5      e [        [        U5      5      n[        XXC5      nU(       d  gU$ ! [         aG  nSn[        U[        [        45      (       a  UR                  [        :X  a  SU-  n[        U5      UeSnAff = f)a#  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)rC   
payload_fninstance_data_filedebuginstance_datarG   rH   msgs           r   r:   r:   ]   s      w"g-=>>M77>>,--12
 	

)!.1C"DE ,]   )2a'7+,,ww& &(:;  S!q()s   A? ?
C	ACCc                    [        UUR                  S/ 5      SS9nU(       a  [        R                  S[	        U5      5         [        X5      n[        R                  " S[        -  U5       Vs/ s H  nSUR                  [        S5      -  PM     nnU(       a&  [        R                  S	US
R                  U5      5        U$ ! [        [        4 a*  n[        R                  SU[        U5      5         S nAg S nAff = fs  snf )Nzbase64-encoded-keysT)decode_pathsinclude_key_aliaseszConverted jinja variables
%sz"Ignoring jinja template for %s: %sz%s[^\s]+z'%s' z:Could not render jinja template variables in file '%s': %sz, )convert_jinja_instance_datar.   r;   rZ   r   r   	TypeErrorr   r<   r=   refindallr   replacejoin)	rC   rX   r[   rZ   instance_jinja_varsrG   rH   varwarningss	            r   rW   rW      s    5"&&'<bA 
 		+Z8K-L	
(F ::..0@

C 	1266
   HIIh	

  ' 8*c!fMs   C .#C?C< C77C<	orig_namereturnc                 J    [         R                  " [        SU 5      nX:w  a  U$ g)ac  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)rc   subr   )rj   
alias_names     r   get_jinja_variable_aliasrp      s%     S)4Jr   c           	      N   0 nU Vs/ s H  ofR                  SS5      PM     nn[        U R                  5       5       H  u  pxU(       a  SR                  XU5      OUn	X;   a  [	        U5      n[        U[        5      (       a_  [        UU	UUUS9XW'   [        R                  " SU5      (       a3  XW   R                  5        H  u  p[        R                  " U5      XZ'   M     OXU'   U(       d  M  [        U5      nU(       d  M  [        R                  " XW   5      X\'   M     U$ s  snf )zProcess instance-data.json dict for use in jinja templates.

Replace hyphens with underscores for jinja templates and decode any
base64_encoded_keys.
-rm   z	{0}{1}{2})sepr^   r_   zv\d+$)re   sorteditemsformatr   rS   dictra   rc   matchcopydeepcopyrp   )rA   prefixrs   r^   r_   resultrP   keyvaluekey_pathsubkeysubvaluero   s                r   ra   ra      s    F7CD|tLLc*|LDTZZ\*
;A;%%f37s#KEeT""5)$7FK xx#&&(.(9(9(;$F%)]]8%<FN )<  3K1#6Jz%)]]6;%?"+ +, M/ Es   D")F)r`   /r   F)/ry   loggingrO   rc   rV   r   typingr   r   	cloudinitr   cloudinit.atomic_helperr   r   cloudinit.helpersr	   cloudinit.settingsr
   cloudinit.templaterr   r   r   r   cloudinit.utilr   r   rR   __annotations__jinja2.exceptionsr   r   jinja2.lexerr   ImportErrorcompile	getLoggerr   r;   r   r"   r,   r%   r:   rW   r=   rp   ra   r   r   r   <module>r      s      	 	  !  4 # )  5i  &C( !	Y 		I 	0x// 0h 49*Z<  , DI G  &O**W%K&s   B> >CC