Skip to content

Commit

Permalink
Merge pull request #44 from fosslight/develop
Browse files Browse the repository at this point in the history
Add extended header feature for additional information on excel
  • Loading branch information
soimkim authored Dec 29, 2021
2 parents d886319 + 3b4371d commit 292dec7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ coloredlogs
pygit2==1.6.1
python3-wget
beautifulsoup4
jsonmerge
6 changes: 3 additions & 3 deletions src/fosslight_util/output_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def check_output_format(output='', format=''):
return success, msg, output_path, output_file, output_extension


def write_output_file(output_file_without_ext, file_extension, sheet_list):
def write_output_file(output_file_without_ext, file_extension, sheet_list, extended_header={}):
success = True
msg = ''

if file_extension == '':
success, msg = write_excel_and_csv(output_file_without_ext, sheet_list)
success, msg = write_excel_and_csv(output_file_without_ext, sheet_list, False, extended_header)
elif file_extension == '.xlsx':
success, msg = write_result_to_excel(output_file_without_ext + file_extension, sheet_list)
success, msg = write_result_to_excel(output_file_without_ext + file_extension, sheet_list, extended_header)
elif file_extension == '.csv':
success, msg = write_result_to_csv(output_file_without_ext + file_extension, sheet_list)
elif file_extension == '.json':
Expand Down
36 changes: 22 additions & 14 deletions src/fosslight_util/write_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import copy
from pathlib import Path
import fosslight_util.constant as constant
from jsonmerge import merge

_HEADER = {'BIN (': ['ID', 'Binary Name', 'Source Code Path',
'NOTICE.html', 'OSS Name', 'OSS Version',
Expand All @@ -30,8 +31,7 @@
logger = logging.getLogger(constant.LOGGER_NAME)


def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False):
# sheet_list = {} // Key = Sheet_name, Value = list of [row_items]
def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False, extended_header={}):
success = True
error_msg = ""
success_csv = True
Expand All @@ -43,10 +43,11 @@ def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False)
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)
success, error_msg = write_result_to_excel(filename_without_extension + ".xlsx", sheet_list, extended_header)

if ignore_os or platform.system() != "Windows":
success_csv, error_msg_csv = write_result_to_csv(filename_without_extension + ".csv", sheet_list, True)
success_csv, error_msg_csv = write_result_to_csv(filename_without_extension + ".csv",
sheet_list, True, extended_header)
if not success:
error_msg = "[Error] Writing excel:" + error_msg
if not success_csv:
Expand Down Expand Up @@ -82,18 +83,24 @@ def remove_empty_sheet(sheet_items):
return success, final_sheet_to_print


def get_header_row(sheet_name, sheet_content):
def get_header_row(sheet_name, sheet_content, extended_header={}):
selected_header = []
for header_key in _HEADER.keys():
if header_key in sheet_name:
selected_header = _HEADER[header_key]
break
if len(selected_header) == 0:

merged_headers = merge(_HEADER, extended_header)

selected_header = merged_headers.get(sheet_name)
if not selected_header:
for header_key in merged_headers.keys():
if sheet_name.startswith(header_key):
selected_header = merged_headers[header_key]
break

if not selected_header:
selected_header = sheet_content.pop(0)
return selected_header, sheet_content


def write_result_to_csv(output_file, sheet_list_origin, separate_sheet=False):
def write_result_to_csv(output_file, sheet_list_origin, separate_sheet=False, extended_header={}):
success = True
error_msg = ""
file_extension = ".csv"
Expand All @@ -111,7 +118,7 @@ def write_result_to_csv(output_file, sheet_list_origin, separate_sheet=False):
merge_sheet = []
for sheet_name, sheet_contents in sheet_list.items():
row_num = 1
header_row, sheet_content_without_header = get_header_row(sheet_name, sheet_contents[:])
header_row, sheet_content_without_header = get_header_row(sheet_name, sheet_contents[:], extended_header)

if not separate_sheet:
merge_sheet.extend(sheet_content_without_header)
Expand All @@ -136,9 +143,10 @@ def write_result_to_csv(output_file, sheet_list_origin, separate_sheet=False):
return success, error_msg


def write_result_to_excel(out_file_name, sheet_list):
def write_result_to_excel(out_file_name, sheet_list, extended_header={}):
success = True
error_msg = ""

try:
is_not_null, sheet_list = remove_empty_sheet(sheet_list)
if is_not_null:
Expand All @@ -147,7 +155,7 @@ def write_result_to_excel(out_file_name, sheet_list):

workbook = xlsxwriter.Workbook(out_file_name)
for sheet_name, sheet_contents in sheet_list.items():
selected_header, sheet_content_without_header = get_header_row(sheet_name, sheet_contents[:])
selected_header, sheet_content_without_header = get_header_row(sheet_name, sheet_contents[:], extended_header)
worksheet = create_worksheet(workbook, sheet_name, selected_header)
write_result_to_sheet(worksheet, sheet_content_without_header)
workbook.close()
Expand Down

0 comments on commit 292dec7

Please sign in to comment.