From 60d90fc9ff142314809c9e50edd96b8e5c170c7e Mon Sep 17 00:00:00 2001 From: spzala Date: Thu, 17 Dec 2015 00:43:58 -0800 Subject: [PATCH] Provide an option to store translated output in a file It can be very helfpul for user to have translated content stored in a desired file. A new optional arg --output-file is created herewith. Change-Id: Ic0cbc4eb4e8701bf8aff663b66d44a8fced2ed88 --- doc/source/usage.rst | 6 ++++-- translator/shell.py | 15 ++++++++++++--- translator/tests/test_shell.py | 23 ++++++++++++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 0d2e8e2a..f1002f42 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -47,8 +47,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=. +For example: :: + + python heat_translator.py --template-file=translator/tests/data/tosca_helloworld.yaml --template-type=tosca --output-file=/tmp/hot_helloworld.yaml Alternatively, you can install a particular release of Heat-Translator as available at https://pypi.python.org/pypi/heat-translator. In this case, you can simply run translation via CLI entry point:: diff --git a/translator/shell.py b/translator/shell.py index 29eaf274..1c58f589 100644 --- a/translator/shell.py +++ b/translator/shell.py @@ -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): diff --git a/translator/tests/test_shell.py b/translator/tests/test_shell.py index a458d98f..abe8f5e7 100644 --- a/translator/tests/test_shell.py +++ b/translator/tests/test_shell.py @@ -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))