docs: fix issues
[parser.git] / tosca2heat / heat-translator / translator / osc / v1 / translate.py
1 #   Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #   not use this file except in compliance with the License. You may obtain
3 #   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, WITHOUT
9 #   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #   License for the specific language governing permissions and limitations
11 #   under the License.
12
13 """Translate action implementations"""
14
15 import logging
16 import logging.config
17 import os
18 import sys
19
20 from cliff import command
21
22 from toscaparser.tosca_template import ToscaTemplate
23 from toscaparser.utils.gettextutils import _
24 from translator.common.utils import UrlUtils
25 from translator.hot.tosca_translator import TOSCATranslator
26 from translator.osc import utils
27
28
29 logging.config.fileConfig('heat_translator_logging.conf')
30 log = logging.getLogger('heat-translator')
31
32
33 class TranslateTemplate(command.Command):
34
35     """Translate a template"""
36
37     auth_required = False
38
39     def get_parser(self, prog_name):
40         parser = super(TranslateTemplate, self).get_parser(prog_name)
41         parser.add_argument(
42             '--template-file',
43             metavar='<template-file>',
44             required=True,
45             help='Path to the file that needs to be translated.')
46         parser.add_argument(
47             '--template-type',
48             metavar='<template-type>',
49             required=True,
50             choices=['tosca'],
51             help='Format of the template file.')
52         parser.add_argument(
53             '--output-file',
54             metavar='<output-file>',
55             help='Path to place the translated content.')
56         parser.add_argument(
57             '--parameter',
58             metavar='<key=value>',
59             action=utils.KeyValueAction,
60             help='Set a property for this template '
61                  '(repeat option to set multiple properties)',
62         )
63         parser.add_argument(
64             '--validate-only',
65             metavar='<true or false>',
66             help='Set to true to only validate a template file.',
67             default='false')
68         return parser
69
70     def take_action(self, parsed_args):
71         log.debug(_('Translating the template with input parameters'
72                     '(%s).'), parsed_args)
73         output = None
74
75         if parsed_args.parameter:
76             parsed_params = parsed_args.parameter
77         else:
78             parsed_params = {}
79
80         if parsed_args.template_type == "tosca":
81             path = parsed_args.template_file
82             a_file = os.path.isfile(path)
83             a_url = UrlUtils.validate_url(path) if not a_file else False
84             if a_file or a_url:
85                 validate = parsed_args.validate_only
86                 if validate and validate.lower() == "true":
87                     ToscaTemplate(path, parsed_params, a_file)
88                     msg = (_('The input "%(path)s" successfully passed '
89                              'validation.') % {'path': path})
90                     print(msg)
91                 else:
92                     tosca = ToscaTemplate(path, parsed_params, a_file)
93                     translator = TOSCATranslator(tosca, parsed_params)
94                     output = translator.translate()
95             else:
96                 msg = _('Could not find template file.')
97                 log.error(msg)
98                 sys.stdout.write(msg)
99                 raise SystemExit
100
101         if output:
102             if parsed_args.output_file:
103                 with open(parsed_args.output_file, 'w+') as f:
104                     f.write(output)
105             else:
106                 print(output)