Skip to content

Commit 4cdb45b

Browse files
authored
Merge pull request #5 from fosslight/develop
Do not print empty sheets
2 parents 70c788c + dea72eb commit 4cdb45b

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if __name__ == "__main__":
1414
setup(
1515
name='fosslight_util',
16-
version='1.0.3',
16+
version='1.0.4',
1717
package_dir={"": "src"},
1818
packages=find_packages(where='src'),
1919
description='FOSSLight Util',

src/fosslight_util/write_excel.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import time
1313
import pandas as pd
1414
from pathlib import Path
15+
import fosslight_util.constant as constant
1516

1617
_HEADER = {'SRC': ['ID', 'Source Name or Path', 'OSS Name',
1718
'OSS Version', 'License', 'Download Location',
@@ -21,6 +22,9 @@
2122
'License', 'Download Location', 'Homepage',
2223
'Copyright Text', 'Exclude', 'Comment']}
2324
_OUTPUT_FILE_PREFIX = "OSS-Report_"
25+
_EMPTY_ITEM_MSG = "* There is no item"\
26+
" to print in OSS-Report.\n"
27+
logger = logging.getLogger(constant.LOGGER_NAME)
2428

2529

2630
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)
3034
success_csv = True
3135
error_msg_csv = ""
3236

33-
output_dir = os.path.dirname(filename_without_extension)
34-
Path(output_dir).mkdir(parents=True, exist_ok=True)
37+
is_not_null, sheet_list = remove_empty_sheet(sheet_list)
3538

36-
success, error_msg = write_result_to_excel(filename_without_extension +
39+
if is_not_null:
40+
output_dir = os.path.dirname(filename_without_extension)
41+
Path(output_dir).mkdir(parents=True, exist_ok=True)
42+
43+
success, error_msg = write_result_to_excel(filename_without_extension +
3744
".xlsx", sheet_list)
38-
if ignore_os or platform.system() != "Windows":
39-
success_csv, error_msg_csv = write_result_to_csv(filename_without_extension +
45+
if ignore_os or platform.system() != "Windows":
46+
success_csv, error_msg_csv = write_result_to_csv(filename_without_extension +
4047
".csv", sheet_list)
41-
if not success:
42-
error_msg = "[Error] Writing excel:" + error_msg
43-
if not success_csv:
44-
error_msg += "\n[Error] Writing csv:" + error_msg_csv
48+
if not success:
49+
error_msg = "[Error] Writing excel:" + error_msg
50+
if not success_csv:
51+
error_msg += "\n[Error] Writing csv:" + error_msg_csv
52+
else:
53+
success = False
54+
error_msg = _EMPTY_ITEM_MSG
4555

4656
return (success and success_csv), error_msg
4757

4858

59+
def remove_empty_sheet(sheet_items):
60+
skip_sheet_name = []
61+
cnt_sheet_to_print = 0
62+
final_sheet_to_print = {}
63+
success = False
64+
65+
try:
66+
if sheet_items:
67+
for sheet_name, sheet_content in sheet_items.items():
68+
logger.debug("ITEM COUNT:"+str(len(sheet_content)))
69+
if len(sheet_content) > 0:
70+
final_sheet_to_print[sheet_name] = sheet_content
71+
cnt_sheet_to_print += 1
72+
else:
73+
skip_sheet_name.append(sheet_name)
74+
if cnt_sheet_to_print != 0:
75+
success = True
76+
if len(skip_sheet_name) > 0:
77+
logger.warn("* Empty sheet(not printed):"+ str(skip_sheet_name))
78+
except Exception as ex:
79+
logger.warn("* Warning:"+str(ex))
80+
81+
return success, final_sheet_to_print
82+
83+
4984
def write_result_to_csv(output_file, sheet_list):
5085
success = True
5186
error_msg = ""

tests/test_excel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def main():
3737

3838
sheet_contents['SRC'] = src_sheet_items
3939
sheet_contents['BIN_TEST'] = bin_sheet_items
40+
sheet_contents['SRC_NULL'] = []
41+
sheet_contents['NULL_SHEET'] = []
4042
success, msg = write_excel_and_csv(
4143
'test_result/excel/OSS-Report', sheet_contents)
4244
logger.warning("Result:" + str(success) + ", error_msg:" + msg)

0 commit comments

Comments
 (0)