3 \me @s UddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z e y$ddl mZddl mZdZeZ Wn eefk rd ZeZ YnXejeZejd ejZejd Zd ZGd dde ZddZddZ ddZ!dddZ"ddZ#dddZ$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 eZdZdZddZddZdS)UndefinedJinjaVariablez>Class used to represent any undefined jinja template variable.cCsdt|jfS)Nz%s%s)MISSING_JINJA_PREFIX_undefined_name)selfr /usr/lib/python3.6/templater.py__str__2szUndefinedJinjaVariable.__str__cCs(t|jtd}tdj|j|ddS)NzhUndefined jinja variable: "{this}-{other}". Jinja tried subtraction. Perhaps you meant "{this}_{other}"?)thisother)strreplacer TypeErrorformatr )r rr r r __sub__5szUndefinedJinjaVariable.__sub__N)__name__ __module__ __qualname____doc__rrr r r r r/srcsfdd}tj||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|jd}|dkr|jd}|dkr,tdtj|jd}}xBt|dkr|j}t|tsxt d|t j ||f||}qBW|j}t|tst d||t j |ft ||S)Nz,Match encountered but no valid group present.zRCan not traverse into non-dictionary '%s' of type %s while looking for subkey '%s'z.replacer) BASIC_MATCHERsub)contentr,r-r )r,r basic_render?s r1cCsdd}|jdd kr(|jdd\}}n|}d}tj|}|sHdt|fS|jdjj}|d krntd||dkrt rt j d dt|fS|dkrt rd||fSdt|fSdS) NcSs0|jdrdnd}t|tddgdjf||S)N rTz jinja2.ext.do)Z undefinedZ trim_blocks extensions)endswith JTemplaterZrender)r0r,addr r r jinja_renderfs z%detect_template..jinja_renderr2rrbasicjinjaz.Unknown template rendering type '%s' requestedzcJinja not available as the selected renderer for desired template, reverting to the basic renderer.)r9r8) findr" TYPE_MATCHERr(r1rlowerstrip ValueErrorJINJA_AVAILABLELOGZwarning)textr7ZidentrestZ type_match template_typer r r detect_templatees(      rEcCs4|si}ttj|\}}}tjd|||||S)Nz+Rendering content of '%s' using renderer %s)rErZ load_filerAdebug)fnr,rDrendererr0r r r render_from_files rIcCst||}tj|||ddS)N)mode)rIrr)rGZoutfnr,rKcontentsr r r render_to_files rMcCs |si}t|\}}}|||S)z Render string)rE)r0r,Z_template_typerHr r r render_stringsrNc Cst|d}|j}WdQRX||d}t||jd}|r\tj|dd}|s\td||dkrrtjj |nt ||dd dS) Nr)variantprefixr2T)defaultz.Cannot render template file %s - invalid yaml.-w)Zomode) openreadrNrstriprZ load_yamlrsysstdoutwriter) rPtemplateoutputZis_yamlrQZfhrLZ tpl_paramsoutr r r render_templates   r^)rJ)N)%r ZloggingrerXZtypingrZ cloudinitrr'rZcloudinit.atomic_helperrZ JUndefinedZjinja2rZ_DebugUndefinedrr5r@ ImportErrorAttributeErrorobjectZ getLoggerrrAcompileIr<r.r rr1rErIrMrNr^r r r r s8         &)