1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
15 from jinja2 import Template
17 from ... import __version__ as version
18 from ..loading import LiteralLocation, LiteralLoader
19 from .reader import Reader
20 from .exceptions import ReaderSyntaxError
23 # TODO: we could put a lot of other useful stuff here.
25 'ARIA_VERSION': version,
29 class JinjaReader(Reader):
33 Forwards the rendered result to a new reader in the reader source.
40 template = Template(data)
41 literal = template.render(CONTEXT)
42 # TODO: might be useful to write the literal result to a file for debugging
43 location = self.location
44 if isinstance(location, basestring) and location.endswith('.jinja'):
45 # Use reader based on the location with the ".jinja" prefix stripped off
46 location = location[:-6]
47 next_reader = self.context.reading.reader_source.get_reader(
48 self.context, LiteralLocation(literal, name=location), LiteralLoader(literal))
50 # Use reader for literal loader
51 next_reader = self.context.reading.reader_source.get_reader(
52 self.context, LiteralLocation(literal), LiteralLoader(literal))
53 return next_reader.read()
54 except Exception as e:
55 raise ReaderSyntaxError('Jinja: %s' % e, cause=e)