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
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, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 """Translate action implementations"""
20 from cliff import command
22 from toscaparser.tosca_template import ToscaTemplate
23 from toscaparser.utils.gettextutils import _
24 from translator.common.utils import UrlUtils
25 from translator.conf.config import ConfigProvider
26 from translator.hot.tosca_translator import TOSCATranslator
27 from translator.osc import utils
29 conf_file = ConfigProvider.get_translator_logging_file()
30 logging.config.fileConfig(conf_file)
31 log = logging.getLogger('heat-translator')
34 class TranslateTemplate(command.Command):
36 """Translate a template"""
40 def get_parser(self, prog_name):
41 parser = super(TranslateTemplate, self).get_parser(prog_name)
44 metavar='<template-file>',
46 help='Path to the file that needs to be translated.')
49 metavar='<template-type>',
52 help='Format of the template file.')
55 metavar='<output-file>',
56 help='Path to place the translated content.')
59 metavar='<key=value>',
60 action=utils.KeyValueAction,
61 help='Set a property for this template '
62 '(repeat option to set multiple properties)',
66 metavar='<true or false>',
67 help='Set to true to only validate a template file.',
71 def take_action(self, parsed_args):
72 log.debug(_('Translating the template with input parameters'
73 '(%s).'), parsed_args)
76 if parsed_args.parameter:
77 parsed_params = parsed_args.parameter
81 if parsed_args.template_type == "tosca":
82 path = parsed_args.template_file
83 a_file = os.path.isfile(path)
84 a_url = UrlUtils.validate_url(path) if not a_file else False
86 validate = parsed_args.validate_only
87 if validate and validate.lower() == "true":
88 ToscaTemplate(path, parsed_params, a_file)
89 msg = (_('The input "%(path)s" successfully passed '
90 'validation.') % {'path': path})
93 tosca = ToscaTemplate(path, parsed_params, a_file)
94 translator = TOSCATranslator(tosca, parsed_params)
95 output = translator.translate()
97 msg = _('Could not find template file.')
103 if parsed_args.output_file:
104 with open(parsed_args.output_file, 'w+') as f: