From b2e87ca49a653bb98ee65ab0992a82241abcb0e0 Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Wed, 26 May 2021 10:59:13 +0200 Subject: [PATCH] Refactoring of the cli show module New logging statements for the action and related method call of 'ValidationActions' class. Expanded exception handling for the 'show_validations_parameters' method of the 'ValidationActions' class. Removal of superfluous imports. Signed-off-by: Jiri Podivin Change-Id: I2ad5a212dcd4cdb40ca04f7cce3d18c7527f63b2 --- validations_libs/cli/show.py | 13 +++++------ validations_libs/validation_actions.py | 31 ++++++++++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/validations_libs/cli/show.py b/validations_libs/cli/show.py index ddc2309d..163f8fe1 100644 --- a/validations_libs/cli/show.py +++ b/validations_libs/cli/show.py @@ -14,9 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json -import sys - from cliff.show import ShowOne from validations_libs.validation_actions import ValidationActions @@ -33,7 +30,7 @@ class Show(ShowOne): parser.add_argument('--validation-dir', dest='validation_dir', default=constants.ANSIBLE_VALIDATION_DIR, help=("Path where the validation playbooks " - "is located.")) + "are located.")) parser.add_argument('validation_name', metavar="", type=str, @@ -62,7 +59,7 @@ class ShowGroup(ShowOne): parser.add_argument('--validation-dir', dest='validation_dir', default=constants.ANSIBLE_VALIDATION_DIR, help=("Path where the validation playbooks " - "is located.")) + "are located.")) parser.add_argument('--group', '-g', metavar='', dest="group", @@ -88,7 +85,7 @@ class ShowParameter(ShowOne): parser.add_argument('--validation-dir', dest='validation_dir', default=constants.ANSIBLE_VALIDATION_DIR, help=("Path where the validation playbooks " - "is located.")) + "are located.")) ex_group = parser.add_mutually_exclusive_group(required=False) ex_group.add_argument( @@ -145,7 +142,9 @@ class ShowParameter(ShowOne): parsed_args.group, parsed_args.format_output, parsed_args.download) + if parsed_args.download: - print("The file {} has been created successfully".format( + self.app.LOG.info( + "The file {} has been created successfully".format( parsed_args.download)) return params.keys(), params.values() diff --git a/validations_libs/validation_actions.py b/validations_libs/validation_actions.py index 4724fc7f..cd8de04a 100644 --- a/validations_libs/validation_actions.py +++ b/validations_libs/validation_actions.py @@ -500,19 +500,36 @@ class ValidationActions(object): group) if download_file: params_only = {} - with open(download_file, 'w') as f: - for val_name in params.keys(): - params_only.update(params[val_name].get('parameters')) + try: + with open(download_file, 'w') as parameters_file: + for val_name in params.keys(): + params_only.update(params[val_name].get('parameters')) - if output_format == 'json': - f.write(json.dumps(params_only, + if output_format == 'json': + parameters_file.write( + json.dumps(params_only, indent=4, sort_keys=True)) - else: - f.write(yaml.safe_dump(params_only, + else: + parameters_file.write( + yaml.safe_dump(params_only, allow_unicode=True, default_flow_style=False, indent=2)) + self.log.debug( + "Validations parameters file {} saved as {} ".format( + download_file, + output_format)) + + except (PermissionError, OSError) as error: + self.log.exception( + ( + "Exception {} encountered while tring to write " + "a validations parameters file {}" + ).format( + error, + download_file)) + return params def show_history(self, validation_ids=None, extension='json',