Skip to content

Commit

Permalink
Merge pull request #79 from fosslight/develop
Browse files Browse the repository at this point in the history
Print warning msg when there is no matched sheet
  • Loading branch information
bjk7119 authored Jul 19, 2022
2 parents f3c8a76 + 21d1e2e commit a25fb26
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/fosslight_util/read_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,48 @@
def read_oss_report(excel_file, sheet_names=""):
_oss_report_items = []
_xl_sheets = []
all_sheet_to_read = []
not_matched_sheet = []
any_sheet_matched = False
SHEET_PREFIX_TO_READ = ["bin", "bom", "src"]
if sheet_names:
sheet_name_prefix_math = False
sheet_name_prefix_match = False
sheet_name_to_read = sheet_names.split(",")
else:
sheet_name_prefix_math = True
sheet_name_prefix_match = True
sheet_name_to_read = SHEET_PREFIX_TO_READ

try:
logger.info(f"Read data from : {excel_file}")
xl_workbook = xlrd.open_workbook(excel_file)
for sheet_name in xl_workbook.sheet_names():
all_sheet_in_excel = xl_workbook.sheet_names()

for sheet_to_read in sheet_name_to_read:
try:
sheet_name_lower = sheet_name.lower()
if any(((sheet_name_prefix_math and sheet_name_lower.startswith(sheet_to_read.lower()))
or sheet_name_lower == sheet_to_read.lower())
for sheet_to_read in sheet_name_to_read):
sheet = xl_workbook.sheet_by_name(sheet_name)
if sheet:
logger.info(f"Load a sheet: {sheet_name}")
_xl_sheets.append(sheet)
any_sheet_matched = False
sheet_to_read_lower = sheet_to_read.lower()
all_sheet_to_read.append(sheet_to_read_lower)
for sheet_name in all_sheet_in_excel:
sheet_name_lower = sheet_name.lower()
if (sheet_name_prefix_match and sheet_name_lower.startswith(sheet_to_read_lower)) \
or sheet_to_read_lower == sheet_name_lower:
sheet = xl_workbook.sheet_by_name(sheet_name)
if sheet:
logger.info(f"Load a matched sheet: {sheet_name}")
_xl_sheets.append(sheet)
any_sheet_matched = True
if not any_sheet_matched:
not_matched_sheet.append(sheet_to_read)
except Exception as error:
logger.debug(f"Failed to load sheet: {sheet_name} {error}")

# Not matched any sheet
if len(sheet_name_to_read) == len(not_matched_sheet):
logger.warning("No sheet names are matched.")
# Partially matched
elif (not sheet_name_prefix_match) and not_matched_sheet:
logger.warning(f"Not matched sheet name: {not_matched_sheet}")

for xl_sheet in _xl_sheets:
_item_idx = {
"ID": IDX_CANNOT_FOUND,
Expand Down

0 comments on commit a25fb26

Please sign in to comment.