a )g'@sddlmZmZmZeZddlZddlZddlm Z ddl m Z ddl m ZddlmZmZmZddlmZddlmZmZdd lmZdd lmZeZed Zed Zed Z GdddZ!dS))absolute_importdivisionprint_functionN)generate)UndefinedError) constants) AnsibleErrorAnsibleUndefinedVariableAnsibleTemplateError) text_type) to_nativeto_text)FieldAttribute)DisplayzF(hostvars\[.+\]|[\w_]+)\s+(not\s+is|is|is\s+not)\s+(defined|undefined)z lookup\s*\(z^[_A-Za-z][_a-zA-Z0-9]*$csReZdZdZededddZdfdd Zdd Zd d Z d d Z ddZ Z S) Conditionalz This is a mix-in class, to be used with Base to allow the object to be run conditionally when a condition is met or skipped. listT)ZisadefaultextendZprependNcs4t|ds"|durtdn||_tt|dS)N_loaderzs zConditional._validate_whencCsDg}|}t|}|r@||||d}t|}q|Sr) DEFINED_REGEXsearchappendgroupsend)r conditionalresultsZcondmrrrextract_defined_undefinedBs  z%Conditional.extract_defined_undefinedc Csd}t|drt|d}d}zf|jD]Z}|dus8|dkr>d}nt|trN|}n||||}|rd|}td||f|s$qq$Wn@ty}z(t dt |t |f|dWYd}~n d}~00|S)z} Loops through the conditionals set on this object, returning False if any of them evaluate as such. NZ_dsTzEvaluated conditional (%s): %sz4The conditional check '%s' failed. The error was: %s)obj) rgetattrwhenrbool_check_conditionaldisplaydebug Exceptionrr )rtemplarall_varsZdsresultr*reserrrevaluate_conditionalNs&      2z Conditional.evaluate_conditionalc s*}|rtd||_ztd|jdttrPdkrVWStdrhtdGfdddt j zB|j dd}t ||j dd}t j |d d }}||Wn4ty}ztd t|WYd}~n d}~00d } |j| d} | d kr$WdS| dkr4WdStd|Wnttfy$}zztdt|d} |} | D]`\} }}| dd| ddkrd|v|dkk}|rWWYd}~dSWWYd}~dSqWn$tytd||fYn0WYd}~n d}~00dS)z This method does the low-level evaluation of each conditional set on this object, using jinja2 to wrap the conditionals for evaluation. zjconditional statements should not include jinja2 templating delimiters such as {{ }} or {%% %%}. Found: %sZ __UNSAFE__)disable_lookupsr.z9Conditional is marked as unsafe, and cannot be evaluated.cseZdZdfdd ZdS)z.CleansingNodeVisitorFcst|tjrd}ndt|tjr$d}nRt|tjrvrv|rR|jdrRtdn$|rvtj|jdd}}| |t |D]}|j |||dqdS)NT__z-Invalid access found in the conditional: '%s'execmode) inside_call inside_yield) rastZCallZYieldZStrs startswithrparsevisitZiter_child_nodes generic_visit)rZnoderBrCparsedcnvZ child_nodeCleansingNodeVisitorr*r=rrrIs(    zJConditional._check_conditional..CleansingNodeVisitor.generic_visitN)FF)__name__ __module__ __qualname__rIrrLrrrMsrMNr?r@z Invalid conditional detected: %sz3{%% if %s %%} True {%% else %%} False {%% endif %%}TrueTFalseFz"unable to evaluate conditional: %sz&'(hostvars\[.+\]|[\w_]+)' is undefinedr'"notZdefinedz+error while evaluating conditional (%s): %s)Z is_templater4ZwarningZavailable_variablesrtemplaterr r rDZ NodeVisitorZ environmentrGrrHr6rr stripr rrecompiler&strr(r-replace)rr*r7r8originalr:rJrKr;Z presentedvalZvar_nameZ def_undefZdu_varZlogicstateZ should_existrrLrr3tsT   &   zConditional._check_conditional)N) rNrOrP__doc__rrr1rr$r-r<r3 __classcell__rrrrr*s  &r)"Z __future__rrrtypeZ __metaclass__rDrXZjinja2.compilerrZjinja2.exceptionsrZansiblerCZansible.errorsrr r Zansible.module_utils.sixr Zansible.module_utils._textr r Zansible.playbook.attributerZansible.utils.displayrr4rYr%Z LOOKUP_REGEXZVALID_VAR_REGEXrrrrrs