Merge "Provide an option to store translated output in a file"

This commit is contained in:
Jenkins 2015-12-17 22:19:59 +00:00 committed by Gerrit Code Review
commit ab4979a816
3 changed files with 36 additions and 8 deletions

View File

@ -49,8 +49,10 @@ For example, a TOSCA hello world template can be translated by running the follo
python heat_translator.py --template-file=translator/tests/data/tosca_helloworld.yaml --template-type=tosca
This should produce a translated Heat Orchestration Template on the command line. In the near future, new options will be added to save the output
to destination file.
This should produce a translated Heat Orchestration Template on the command line. The translated content can be saved to a desired file by setting --output-file=<path>.
For example: ::
python heat_translator.py --template-file=translator/tests/data/tosca_helloworld.yaml --template-type=tosca --output-file=/tmp/hot_helloworld.yaml
An optional argument can be provided to handle user inputs parameters. Also, a template file can only be validated instead of translation by using --validate-only=true
optional argument. The command below shows an example usage::

View File

@ -72,6 +72,7 @@ class TranslatorShell(object):
% {'value': template_type})
parsed_params = {}
validate_only = None
output_file = None
if len(args) > 2:
parameters = None
for arg in args:
@ -79,6 +80,9 @@ class TranslatorShell(object):
validate_only = arg
if "--parameters=" in arg:
parameters = arg
if "--output-file=" in arg:
output = arg
output_file = output.split('--output-file=')[1]
if parameters:
parsed_params = self._parse_parameters(parameters)
a_file = os.path.isfile(path)
@ -95,7 +99,7 @@ class TranslatorShell(object):
heat_tpl = self._translate(template_type, path, parsed_params,
a_file)
if heat_tpl:
self._write_output(heat_tpl)
self._write_output(heat_tpl, output_file)
else:
raise ValueError(_("The path %(path)s is not a valid file"
" or URL.") % {'path': path})
@ -134,8 +138,13 @@ class TranslatorShell(object):
output = translator.translate()
return output
def _write_output(self, output):
print(output)
def _write_output(self, output, output_file=None):
if output:
if output_file:
with open(output_file, 'w+') as f:
f.write(output)
else:
print(output)
def main(args=None):

View File

@ -11,6 +11,8 @@
# under the License.
import os
import shutil
import tempfile
from toscaparser.common import exception
from toscaparser.utils.gettextutils import _
@ -25,6 +27,7 @@ class ShellTest(TestCase):
template_file = '--template-file=' + tosca_helloworld
template_type = '--template-type=tosca'
template_validation = "--validate-only=true"
failure_msg = _('The program raised an exception unexpectedly.')
def test_missing_arg(self):
error = self.assertRaises(ValueError, shell.main, '')
@ -69,7 +72,7 @@ class ShellTest(TestCase):
try:
shell.main([self.template_file, self.template_type])
except Exception:
self.fail(_('The program raised an exception unexpectedly.'))
self.fail(self.failure_msg)
def test_valid_template_with_parameters(self):
tosca_single_instance_wordpress = os.path.join(
@ -81,14 +84,14 @@ class ShellTest(TestCase):
try:
shell.main([template, self.template_type, parameters])
except Exception:
self.fail(_('The program raised an exception unexpectedly.'))
self.fail(self.failure_msg)
def test_validate_only(self):
try:
shell.main([self.template_file, self.template_type,
self.template_validation])
except Exception:
self.fail(_('The program raised an exception unexpectedly.'))
self.fail(self.failure_msg)
template = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
@ -97,3 +100,17 @@ class ShellTest(TestCase):
self.assertRaises(exception.ValidationError, shell.main,
[invalid_template, self.template_type,
self.template_validation])
def test_output_file(self):
temp_dir = tempfile.mkdtemp()
temp_file = "/test_translation_output.txt"
output_file = "--output-file=" + temp_dir + temp_file
try:
shell.main([self.template_file, self.template_type, output_file])
except Exception:
self.fail(self.failure_msg)
finally:
if temp_dir:
shutil.rmtree(temp_dir)
self.assertTrue(temp_dir is None or
not os.path.exists(temp_dir))