a '¡Dg�ã@sVUddlZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z ddlmZddlmZddlmZmZmZmZdd lmZmZeeed <zdd lmZdd lm Z Wn e!yâeZe "d ¡Z Yn0e #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)Úb64dÚ json_dumps)ÚPaths)Ú PER_ALWAYS)ÚMISSING_JINJA_PREFIXÚJinjaSyntaxParsingExceptionÚdetect_templateÚ render_string)Ú load_jsonÚload_text_fileÚJUndefinedError)ÚUndefinedError)Ú operator_rez[-.]c@s eZdZdS)ÚJinjaLoadErrorN©Ú__name__Ú __module__Ú __qualname__©rrúE/usr/lib/python3.9/site-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__r rrÚgetZ list_types)ÚselfrÚ_kwargsZhandlerÚctyperrrr!.s  z!JinjaTemplatePartHandler.__init__c CsØ|tjvrdS|j d¡}zt|||ƒ}Wn8tyb} z t d|t| ƒ¡WYd} ~ dSd} ~ 00|sldSt  |¡} |j   | ¡} | s˜t d|| ¡dS| j dkr¸|   ||||||¡n| j dkrÔ|   |||||¡dS)NZinstance_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é)rZCONTENT_SIGNALSrZ get_runpathÚrender_jinja_payload_from_filer ÚLOGÚwarningÚstrZtype_from_starts_withrr"Zhandler_versionÚ handle_part) r#Údatar%ÚfilenameÚpayloadZ frequencyZheadersZjinja_json_fileÚrendered_payloadÚeZsubtypeZ sub_handlerrrrr+6sB  ÿü  ü  ÿ  ÿz$JinjaTemplatePartHandler.handle_partN)rrrÚprefixesrr!r+rrrrr*srFc Cs¶t|ƒddkrtdƒ‚i}d}tj |¡s8td|ƒ‚ztt|ƒƒ}WnRtyš}z:d}t |t t fƒr||j t kr|d|}t|ƒ|‚WYd}~n d}~00t||||ƒ}|s²dS|S)aKRender 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. 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/r0Úmsgrrrr']s6 ÿÿ ÿÿ ÿr'c Cs¬t|| dg¡dd�}|r*t dt|ƒ¡zt||ƒ}Wn<ttfyt}z t d|t |ƒ¡WYd}~dSd}~00dd„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Ú ™sÿz(render_jinja_payload..z%s[^\s]+z:Could not render jinja template variables in file '%s': %sz, )Úconvert_jinja_instance_datar"r(r<rr Ú TypeErrorrr)r*ÚreÚfindallr Újoin)r.r;r=r<Zinstance_jinja_varsr/r0Úwarningsrrrr:Šs4 ýÿÿþýr:)Ú 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)rHÚsubr)rLÚ alias_namerrrÚget_jinja_variable_alias¨srQrAú/rc CsÎi}dd„|Dƒ}t| ¡ƒD]ª\}}|r8d |||¡n|}||vrLt|ƒ}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)ú-rN)rB)rCr3rrrrEÆóz/convert_jinja_instance_data..z {0}{1}{2})Úsepr?r@zv\d+$) ÚsortedÚitemsÚformatrr6ÚdictrFrHÚmatchÚcopyÚdeepcopyrQ) r,ÚprefixrUr?r@ÚresultÚkeyÚvalueZkey_pathZsubkeyZsubvaluerPrrrrF½s. û  rF)F)F)rArRrF)/r[Zloggingr2rHr9rÚtypingrrZ cloudinitrZcloudinit.atomic_helperrrZcloudinit.helpersrZcloudinit.settingsr Zcloudinit.templaterr r r r Zcloudinit.utilrrr5Ú__annotations__Zjinja2.exceptionsrrZ jinja2.lexerrÚ ImportErrorÚcompileZ getLoggerrr(rrr rr'r:r*rQrFrrrrÚs8         4ÿ - ÿ