vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / reading / jinja.py
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
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
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.
12
13 import os
14
15 from jinja2 import Template
16
17 from ... import __version__ as version
18 from ..loading import LiteralLocation, LiteralLoader
19 from .reader import Reader
20 from .exceptions import ReaderSyntaxError
21
22
23 # TODO: we could put a lot of other useful stuff here.
24 CONTEXT = {
25     'ARIA_VERSION': version,
26     'ENV': os.environ}
27
28
29 class JinjaReader(Reader):
30     """
31     ARIA Jinja reader.
32
33     Forwards the rendered result to a new reader in the reader source.
34     """
35
36     def read(self):
37         data = self.load()
38         try:
39             data = str(data)
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))
49             else:
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)