From 3e7fe67008f9344b7f663e08eda9962bc6f3d3aa Mon Sep 17 00:00:00 2001 From: Adam Janovsky Date: Thu, 12 May 2022 17:34:55 +0200 Subject: fix empty raise, more detailed info about badly parsed html --- sec_certs/dataset/common_criteria.py | 18 ++++++++++++------ sec_certs/sample/common_criteria.py | 3 +-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sec_certs/dataset/common_criteria.py b/sec_certs/dataset/common_criteria.py index e9af7b7a..19f0dcd2 100644 --- a/sec_certs/dataset/common_criteria.py +++ b/sec_certs/dataset/common_criteria.py @@ -459,10 +459,11 @@ class CCDataset(Dataset[CommonCriteriaCert], ComplexSerializableType): soup: BeautifulSoup, cert_status: str, table_id: str, category_string: str ) -> Dict[str, "CommonCriteriaCert"]: tables = soup.find_all("table", id=table_id) - assert len(tables) <= 1 - if not tables: - return {} + if not len(tables) == 1: + raise ValueError( + f'The "{file.name}" was expected to contain exactly 1 element. Instead, it contains: {len(tables)}
elements.' + ) table = tables[0] rows = list(table.find_all("tr")) @@ -475,9 +476,14 @@ class CCDataset(Dataset[CommonCriteriaCert], ComplexSerializableType): # The following unused snippet extracts expected number of certs from the table # caption_str = str(table.findAll('caption')) # n_expected_certs = int(caption_str.split(category_string + ' – ')[1].split(' Certified Products')[0]) - table_certs = { - x.dgst: x for x in [CommonCriteriaCert.from_html_row(row, cert_status, category_string) for row in body] - } + + try: + table_certs = { + x.dgst: x + for x in [CommonCriteriaCert.from_html_row(row, cert_status, category_string) for row in body] + } + except ValueError as e: + raise ValueError(f"Bad html file: {file.name} ({str(e)})") from e return table_certs diff --git a/sec_certs/sample/common_criteria.py b/sec_certs/sample/common_criteria.py index 29789d2a..098054ac 100644 --- a/sec_certs/sample/common_criteria.py +++ b/sec_certs/sample/common_criteria.py @@ -540,8 +540,7 @@ class CommonCriteriaCert( cells = list(row.find_all("td")) if len(cells) != 7: - logger.error("Unexpected number of cells in CC html row.") - raise + raise ValueError(f"Unexpected number of
elements in CC html row. Expected: 7, actual: {len(cells)}") name = CommonCriteriaCert._html_row_get_name(cells[0]) manufacturer = CommonCriteriaCert._html_row_get_manufacturer(cells[1]) -- cgit v1.3.1