Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_simple(self):
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
self.assert_raises(TemplateNotFound, env.get_template, 'missing.html')
.. versionchanged:: 2.4
If `names` contains a :class:`Template` object it is returned
from the function unchanged.
"""
if not names:
raise TemplatesNotFound(message=u'Tried to select from an empty list '
u'of templates.')
globals = self.make_globals(globals)
for name in names:
if isinstance(name, Template):
return name
if parent is not None:
name = self.join_path(name, parent)
try:
return self._load_template(name, globals)
except TemplateNotFound:
pass
raise TemplatesNotFound(names)
'HOSTNAME=Salt\n',
'DOMAIN=saltstack.com\n',
'SEARCH=test.saltstack.com\n'])
mock = MagicMock(side_effect=jinja2.exceptions.TemplateNotFound
('error'))
with patch.object(jinja2.Environment, 'get_template', mock):
self.assertEqual(debian_ip.build_network_settings(), '')
with patch.dict(debian_ip.__grains__, {'osfullname': 'Ubuntu',
'osrelease': '10'}):
mock = MagicMock(return_value=True)
with patch.dict(debian_ip.__salt__, {'service.available': mock,
'service.disable': mock,
'service.enable': mock}):
mock = MagicMock(side_effect=jinja2.exceptions.TemplateNotFound
('error'))
with patch.object(jinja2.Environment, 'get_template', mock):
self.assertEqual(debian_ip.build_network_settings(), '')
with patch.object(debian_ip, '_read_temp',
MagicMock(return_value=True)):
self.assertTrue(debian_ip.build_network_settings
(test='True'))
def get_loader(self, template):
try:
prefix, name = template.split(self.delimiter, 1)
loader = self.mapping[prefix]
except (ValueError, KeyError):
raise TemplateNotFound(template)
return loader, name
.. versionchanged:: 2.4
If `names` contains a :class:`Template` object it is returned
from the function unchanged.
"""
if not names:
raise TemplatesNotFound(message=u'Tried to select from an empty list '
u'of templates.')
globals = self.make_globals(globals)
for name in names:
if isinstance(name, Template):
return name
if parent is not None:
name = self.join_path(name, parent)
try:
return self._load_template(name, globals)
except TemplateNotFound:
pass
raise TemplatesNotFound(names)
def load(self, environment, name, globals=None):
for loader in self.loaders:
try:
return loader.load(environment, name, globals)
except TemplateNotFound:
pass
raise TemplateNotFound(name)
def _load_template(self, name, globals):
request = getattr(settings, 'request_handler', None)
if request:
for prefix in request.theme.template_dir_list:
try:
return super(CoffinEnvironment, self)._load_template(join(prefix, name), globals)
except TemplateNotFound:
pass
try:
return super(CoffinEnvironment, self)._load_template(name, globals)
except TemplateNotFound:
raise TemplateNotFound(name)
else:
return super(CoffinEnvironment, self)._load_template(name, globals)
def load(self, environment, name, globals=None):
loader, local_name = self.get_loader(name)
try:
return loader.load(environment, local_name, globals)
except TemplateNotFound:
# re-raise the exception with the correct filename here.
# (the one that includes the prefix)
raise TemplateNotFound(name)
def _generate_template(state):
template = state.template
render_vars = PublishRunVars(state)
try:
template.generate(state.run_dest, render_vars)
except jinja2.TemplateRuntimeError as e:
raise GenerateError(e, template)
except jinja2.exceptions.TemplateNotFound as e:
e.message = "template not found: %s" % e.message
raise GenerateError(e, template)
def get_source(self, environment, template):
for loader in self.loaders:
try:
return loader.get_source(environment, template)
except TemplateNotFound:
pass
raise TemplateNotFound(template)