o x[h @s&UddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddl mZeed<zdd lmZdd lmZd ZeZWneefyZd ZeZYnweeZd ZGdddeZGdddeZe dddZ ddZ!ddZ"d ddZ#ddZ$d!ddZ%dS)"N)Any)TemplateSyntaxError) performance) type_utils)util) write_file JUndefined)DebugUndefined)TemplateTFzCI_MISSING_JINJA_VAR/c sTeZdZdeddffdd ZddZe dd ed ed edefd d ZdZ Z S)JinjaSyntaxParsingExceptionerrorreturnNcs*t|jpd|j|j|j|j|_dS)Nzunknown syntax error)super__init__messagelinenonamefilenamesource)selfr  __class__5/usr/lib/python3/dist-packages/cloudinit/templater.pyr/s z$JinjaSyntaxParsingException.__init__cCs(|j|j|j|j|jddS);Avoid jinja2.TemplateSyntaxError multi-line __str__ format. syntax_error line_number line_content)format_error_messagerrr splitlinesstriprrrr__str__;s z#JinjaSyntaxParsingException.__str__rrrcCs$|rd|nd}tjj|||dS)rz: r%r)r message_templateformatrrrrr Cs z0JinjaSyntaxParsingException.format_error_messagezfUnable to parse Jinja template due to syntax error: {syntax_error} on line {line_number}{line_content})r%) __name__ __module__ __qualname__rrr$ staticmethodstrr r& __classcell__rrrrr .s(   r c@s eZdZdZddZddZdS)UndefinedJinjaVariablez>Class used to represent any undefined jinja template variable.cCsdt|jfS)Nz%s%s)MISSING_JINJA_PREFIX_undefined_namer#rrrr$\szUndefinedJinjaVariable.__str__cCs$t|td}tdj|j|d)Nr%zhUndefined jinja variable: "{this}-{other}". Jinja tried subtraction. Perhaps you meant "{this}_{other}"?)thisother)r,replacer/ TypeErrorr'r0)rr2rrr__sub___szUndefinedJinjaVariable.__sub__N)r(r)r*__doc__r$r5rrrrr.Ys r.zRendering basic templatecsfdd}td||S)zThis 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'. cs|d}|dur|d}|durtdt|d}}t|dkrE|}t|ts;t d|t ||f||}t|dks&|}t|tsZt d||t |ft ||S)Nrz,Match encountered but no valid group present.zRCan not traverse into non-dictionary '%s' of type %s while looking for subkey '%s'z.replacerz)\$\{([A-Za-z0-9_.]+)\}|\$([A-Za-z0-9_.]+))resub)contentrIrJrrHr basic_renderis rNcCsdd}|ddkr|dd\}}n|}d}td|tj}|s'dt|fS|d}|d vr:t d ||d krJt sJt d dt|fS|d krUt rUd ||fSdt|fS) Nc Ss|drdnd}z+tdt|tddgdjd i|| WdWS1s-wYWdStyM}z |jd7_t|d|d}~wt y[}z||d}~ww) N r%zRendering jinja2 templateTz jinja2.ext.do) undefined trim_blocks extensionsr7)r r) endswithrTimed JTemplater.renderrrr Exception)rMrIaddtemplate_syntax_error unknown_errorrrr jinja_renders: ( z%detect_template..jinja_renderrOr7r%z##\s*template:(.*)basic)jinjar]z.Unknown template rendering type '%s' requestedr^zcJinja not available as the selected renderer for desired template, reverting to the basic renderer.) findr=rKrDIrNr9lowerr" ValueErrorJINJA_AVAILABLELOGwarning)textr[identrest type_match template_typerrrdetect_templates.      rkcCs4|si}tt|\}}}td|||||S)Nz+Rendering content of '%s' using renderer %s)rkrload_text_filerddebug)fnrIrjrendererrMrrrrender_from_files  rpcCst||}tj|||ddS)N)mode)rprr)rnoutfnrIrrcontentsrrrrender_to_files rucCs |si}t|\}}}|||S)z Render string)rk)rMrI_template_typerorrr render_strings rwcCsrt|}||d}t||d}|r$tj|dd}|s$td||dkr0tj|dSt ||dddS) N)variantprefixrOT)defaultz.Cannot render template file %s - invalid yaml.-w)omode) rrlrwrstrip load_yamlr:sysstdoutwriter)rxtemplateoutputis_yamlryrt tpl_paramsoutrrrrender_templates  r)rq)N)&r;loggingrKrtypingrjinja2r cloudinitrrrBrcloudinit.atomic_helperr__annotations__r _DebugUndefinedr rUrcr ImportErrorAttributeErrorobject getLoggerr(rdr/r r.timedrNrkrprurwrrrrrs>          + (2