Coverage for lib/ansible/template/vars.py : 45%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com> # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
''' Helper class to template all variable content before jinja2 sees it. This is done by hijacking the variable storage that jinja2 uses, and overriding __contains__ and __getitem__ to look like a dict. Added bonus is avoiding duplicating the large hashes that inject tends to be.
To facilitate using builtin jinja2 things like range, globals are also handled here. '''
''' Initializes this object with a valid Templar() object, as well as several dictionaries of variables representing different scopes (in jinja2 terminology). '''
for key, val in iteritems(locals): if val is not missing: if key[:2] == 'l_': self._locals[key[2:]] = val elif key not in ('context', 'environment', 'template'): self._locals[key] = val
return True if k in i: return True return True
keys = set() keys.update(self._templar._available_variables, self._locals, self._globals, *self._extras) return iter(keys)
keys = set() keys.update(self._templar._available_variables, self._locals, self._globals, *self._extras) return len(keys)
if varname in self._locals: return self._locals[varname] for i in self._extras: if varname in i: return i[varname] if varname in self._globals: return self._globals[varname] else: raise KeyError("undefined variable: %s" % varname)
# HostVars is special, return it as-is, as is the special variable # 'vars', which contains the vars structure else: except AnsibleUndefinedVariable: raise except Exception as e: msg = getattr(e, 'message') or to_native(e) raise AnsibleError("An unhandled exception occurred while templating '%s'. " "Error was a %s, original message: %s" % (to_native(variable), type(e), msg))
''' If locals are provided, create a copy of self containing those locals in addition to what is already in this variable proxy. '''
# FIXME run this only on jinja2>=2.9? # prior to version 2.9, locals contained all of the vars and not just the current # local vars so this was not necessary for locals to propagate down to nested includes new_locals = self._locals.copy() new_locals.update(locals)
return AnsibleJ2Vars(self._templar, self._globals, locals=new_locals, *self._extras) |