From dea72ebeea7cdc169d2ea713f6adc91ad6d03ac5 Mon Sep 17 00:00:00 2001 From: Soim Kim Date: Sun, 2 May 2021 22:27:03 +0900 Subject: [PATCH] Do not print empty sheets --- setup.py | 2 +- src/fosslight_util/write_excel.py | 53 +++++++++++++++++++++++++------ tests/test_excel.py | 2 ++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 55025fd..40e1fe4 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ if __name__ == "__main__": setup( name='fosslight_util', - version='1.0.3', + version='1.0.4', package_dir={"": "src"}, packages=find_packages(where='src'), description='FOSSLight Util', diff --git a/src/fosslight_util/write_excel.py b/src/fosslight_util/write_excel.py index eb3506c..4afcdef 100755 --- a/src/fosslight_util/write_excel.py +++ b/src/fosslight_util/write_excel.py @@ -12,6 +12,7 @@ import time import pandas as pd from pathlib import Path +import fosslight_util.constant as constant _HEADER = {'SRC': ['ID', 'Source Name or Path', 'OSS Name', 'OSS Version', 'License', 'Download Location', @@ -21,6 +22,9 @@ 'License', 'Download Location', 'Homepage', 'Copyright Text', 'Exclude', 'Comment']} _OUTPUT_FILE_PREFIX = "OSS-Report_" +_EMPTY_ITEM_MSG = "* There is no item"\ + " to print in OSS-Report.\n" +logger = logging.getLogger(constant.LOGGER_NAME) def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False): @@ -30,22 +34,53 @@ def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False) success_csv = True error_msg_csv = "" - output_dir = os.path.dirname(filename_without_extension) - Path(output_dir).mkdir(parents=True, exist_ok=True) + is_not_null, sheet_list = remove_empty_sheet(sheet_list) - success, error_msg = write_result_to_excel(filename_without_extension + + if is_not_null: + output_dir = os.path.dirname(filename_without_extension) + Path(output_dir).mkdir(parents=True, exist_ok=True) + + success, error_msg = write_result_to_excel(filename_without_extension + ".xlsx", sheet_list) - if ignore_os or platform.system() != "Windows": - success_csv, error_msg_csv = write_result_to_csv(filename_without_extension + + if ignore_os or platform.system() != "Windows": + success_csv, error_msg_csv = write_result_to_csv(filename_without_extension + ".csv", sheet_list) - if not success: - error_msg = "[Error] Writing excel:" + error_msg - if not success_csv: - error_msg += "\n[Error] Writing csv:" + error_msg_csv + if not success: + error_msg = "[Error] Writing excel:" + error_msg + if not success_csv: + error_msg += "\n[Error] Writing csv:" + error_msg_csv + else: + success = False + error_msg = _EMPTY_ITEM_MSG return (success and success_csv), error_msg +def remove_empty_sheet(sheet_items): + skip_sheet_name = [] + cnt_sheet_to_print = 0 + final_sheet_to_print = {} + success = False + + try: + if sheet_items: + for sheet_name, sheet_content in sheet_items.items(): + logger.debug("ITEM COUNT:"+str(len(sheet_content))) + if len(sheet_content) > 0: + final_sheet_to_print[sheet_name] = sheet_content + cnt_sheet_to_print += 1 + else: + skip_sheet_name.append(sheet_name) + if cnt_sheet_to_print != 0: + success = True + if len(skip_sheet_name) > 0: + logger.warn("* Empty sheet(not printed):"+ str(skip_sheet_name)) + except Exception as ex: + logger.warn("* Warning:"+str(ex)) + + return success, final_sheet_to_print + + def write_result_to_csv(output_file, sheet_list): success = True error_msg = "" diff --git a/tests/test_excel.py b/tests/test_excel.py index ad49f92..db0f09c 100755 --- a/tests/test_excel.py +++ b/tests/test_excel.py @@ -37,6 +37,8 @@ def main(): sheet_contents['SRC'] = src_sheet_items sheet_contents['BIN_TEST'] = bin_sheet_items + sheet_contents['SRC_NULL'] = [] + sheet_contents['NULL_SHEET'] = [] success, msg = write_excel_and_csv( 'test_result/excel/OSS-Report', sheet_contents) logger.warning("Result:" + str(success) + ", error_msg:" + msg)