diff options
| author | J08nY | 2025-02-14 14:55:38 +0100 |
|---|---|---|
| committer | J08nY | 2025-02-14 16:08:43 +0100 |
| commit | 461faa44ee51caf9d219dcfba9925d1a02c0a366 (patch) | |
| tree | 1305ffe6e9afde6f8b7d15f8fb6dfffa07f123fc /src/sec_certs/utils | |
| parent | 2a2b60565df9904d913cd830e7675af91749c5c1 (diff) | |
| download | sec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.tar.gz sec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.tar.zst sec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.zip | |
Get rid of RETURNCODE_OK. This is not C99.
Diffstat (limited to 'src/sec_certs/utils')
| -rw-r--r-- | src/sec_certs/utils/extract.py | 30 | ||||
| -rw-r--r-- | src/sec_certs/utils/helpers.py | 3 | ||||
| -rw-r--r-- | src/sec_certs/utils/pdf.py | 9 |
3 files changed, 15 insertions, 27 deletions
diff --git a/src/sec_certs/utils/extract.py b/src/sec_certs/utils/extract.py index 6af7f4cc..35361e94 100644 --- a/src/sec_certs/utils/extract.py +++ b/src/sec_certs/utils/extract.py @@ -267,19 +267,9 @@ def search_only_headers_anssi(filepath: Path): # noqa: C901 relative_filepath = "/".join(str(filepath).split("/")[-4:]) error_msg = f"Failed to parse ANSSI frontpage headers from {relative_filepath}; {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) from e - # if True: - # print('# hits for rule') - # sorted_rules = sorted(num_rules_hits.items(), - # key=operator.itemgetter(1), reverse=True) - # used_rules = [] - # for rule in sorted_rules: - # print('{:4d} : {}'.format(rule[1], rule[0])) - # if rule[1] > 0: - # used_rules.append(rule[0]) - - return constants.RETURNCODE_OK, items_found + return items_found def search_only_headers_bsi(filepath: Path): # noqa: C901 @@ -376,9 +366,9 @@ def search_only_headers_bsi(filepath: Path): # noqa: C901 relative_filepath = "/".join(str(filepath).split("/")[-4:]) error_msg = f"Failed to parse BSI headers from frontpage: {relative_filepath}; {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) from e - return constants.RETURNCODE_OK, items_found + return items_found def search_only_headers_nscib(filepath: Path): # noqa: C901 @@ -456,9 +446,9 @@ def search_only_headers_nscib(filepath: Path): # noqa: C901 except Exception as e: error_msg = f"Failed to parse NSCIB headers from frontpage: {filepath}; {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) from e - return constants.RETURNCODE_OK, items_found + return items_found def search_only_headers_niap(filepath: Path): @@ -507,9 +497,9 @@ def search_only_headers_niap(filepath: Path): except Exception as e: error_msg = f"Failed to parse NIAP headers from frontpage: {filepath}; {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) from e - return constants.RETURNCODE_OK, items_found + return items_found def search_only_headers_canada(filepath: Path): # noqa: C901 @@ -580,9 +570,9 @@ def search_only_headers_canada(filepath: Path): # noqa: C901 except Exception as e: error_msg = f"Failed to parse Canada headers from frontpage: {filepath}; {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) from e - return constants.RETURNCODE_OK, items_found + return items_found def flatten_matches(dct: dict) -> dict: diff --git a/src/sec_certs/utils/helpers.py b/src/sec_certs/utils/helpers.py index 63de6fd9..4c3ab43b 100644 --- a/src/sec_certs/utils/helpers.py +++ b/src/sec_certs/utils/helpers.py @@ -142,13 +142,12 @@ def download_file( # noqa: C901 if show_progress_bar: pbar.update(len(data)) - return r.status_code + return r.status_code except requests.exceptions.Timeout: return requests.codes.timeout except Exception as e: logger.error(f"Failed to download from {url}; {e}") return constants.RETURNCODE_NOK - return constants.RETURNCODE_NOK def download_parallel( diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py index 3195e67e..39046717 100644 --- a/src/sec_certs/utils/pdf.py +++ b/src/sec_certs/utils/pdf.py @@ -13,7 +13,6 @@ import pikepdf import pytesseract from PIL import Image -from sec_certs import constants from sec_certs.constants import ( GARBAGE_ALPHA_CHARS_THRESHOLD, GARBAGE_AVG_LLEN_THRESHOLD, @@ -151,7 +150,7 @@ def parse_pdf_date(dateval: bytes | None) -> datetime | None: return None -def extract_pdf_metadata(filepath: Path) -> tuple[str, dict[str, Any] | None]: # noqa: C901 +def extract_pdf_metadata(filepath: Path) -> dict[str, Any]: # noqa: C901 """ Extract PDF metadata, such as the number of pages, author, title, etc. @@ -237,9 +236,9 @@ def extract_pdf_metadata(filepath: Path) -> tuple[str, dict[str, Any] | None]: relative_filepath = "/".join(str(filepath).split("/")[-4:]) error_msg = f"Failed to read metadata of {relative_filepath}, error: {e}" logger.error(error_msg) - return error_msg, None + raise ValueError(error_msg) - return constants.RETURNCODE_OK, metadata + return metadata def text_is_garbage(text: str) -> bool: @@ -274,7 +273,7 @@ def text_is_garbage(text: str) -> bool: # If the average length of a line is small, this is garbage. if avg_line_len < GARBAGE_AVG_LLEN_THRESHOLD: return True - # If there a small amount of lines that have more than one character at every second character, this is garbage. + # If there is a small amount of lines that have more than one character at every second character, this is garbage. # This detects the ANSSI spacing issues. if every_second < GARBAGE_EVERY_SECOND_CHAR_THRESHOLD: return True |
