diff options
| author | Adam Janovsky | 2023-02-03 12:19:19 +0100 |
|---|---|---|
| committer | Adam Janovsky | 2023-02-03 12:19:19 +0100 |
| commit | 468b63f990e95522095cc7a860b68e84cfce9cf4 (patch) | |
| tree | 0e5f8a27bed00c79419217342d8cfd800c66da9d | |
| parent | e5465f38f053941e0bcf3387da4e49394e169ee0 (diff) | |
| download | sec-certs-468b63f990e95522095cc7a860b68e84cfce9cf4.tar.gz sec-certs-468b63f990e95522095cc7a860b68e84cfce9cf4.tar.zst sec-certs-468b63f990e95522095cc7a860b68e84cfce9cf4.zip | |
add flake8-simplify to ruff
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | src/sec_certs/dataset/cc.py | 17 | ||||
| -rw-r--r-- | src/sec_certs/dataset/cpe.py | 2 | ||||
| -rw-r--r-- | src/sec_certs/dataset/dataset.py | 4 | ||||
| -rw-r--r-- | src/sec_certs/dataset/fips.py | 4 | ||||
| -rw-r--r-- | src/sec_certs/dataset/fips_algorithm.py | 2 | ||||
| -rw-r--r-- | src/sec_certs/model/cpe_matching.py | 2 | ||||
| -rw-r--r-- | src/sec_certs/sample/cc.py | 15 | ||||
| -rw-r--r-- | src/sec_certs/sample/fips.py | 6 | ||||
| -rw-r--r-- | src/sec_certs/serialization/json.py | 7 | ||||
| -rw-r--r-- | src/sec_certs/utils/helpers.py | 11 | ||||
| -rw-r--r-- | src/sec_certs/utils/pdf.py | 10 | ||||
| -rw-r--r-- | tests/test_common.py | 2 |
13 files changed, 31 insertions, 52 deletions
diff --git a/pyproject.toml b/pyproject.toml index 7026dff1..beb1ed33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,7 @@ "PL", # pylint "PTH", # enforce pathlib usage "C4", # comprehensions + "SIM", ] ignore = [ "E501", # line-length, should be handled by black diff --git a/src/sec_certs/dataset/cc.py b/src/sec_certs/dataset/cc.py index 7f7771ff..bb806d18 100644 --- a/src/sec_certs/dataset/cc.py +++ b/src/sec_certs/dataset/cc.py @@ -326,10 +326,7 @@ class CCDataset(Dataset[CCCertificate, CCAuxillaryDatasets], ComplexSerializable def _get_primary_key_str(row: Tag): return row["category"] + row["cert_name"] + row["report_link"] - if "active" in str(file): - cert_status = "active" - else: - cert_status = "archived" + cert_status = "active" if "active" in str(file) else "archived" csv_header = [ "category", @@ -481,10 +478,7 @@ class CCDataset(Dataset[CCCertificate, CCAuxillaryDatasets], ComplexSerializable return table_certs - if "active" in str(file): - cert_status = "active" - else: - cert_status = "archived" + cert_status = "active" if "active" in str(file) else "archived" cc_cat_abbreviations = ["AC", "BP", "DP", "DB", "DD", "IC", "KM", "MD", "MF", "NS", "OS", "OD", "DG", "TC"] cc_table_ids = ["tbl" + x for x in cc_cat_abbreviations] @@ -717,7 +711,7 @@ class CCDataset(Dataset[CCCertificate, CCAuxillaryDatasets], ComplexSerializable return set() res = set() for scheme, matches in kws["cc_cert_id"].items(): - for match in matches.keys(): + for match in matches: try: canonical = CertificateId(scheme, match).canonical res.add(canonical) @@ -899,10 +893,7 @@ class CCDatasetMaintenanceUpdates(CCDataset, ComplexSerializableType): class CCSchemeDataset: @staticmethod def _download_page(url, session=None): - if session: - conn = session - else: - conn = requests + conn = session if session else requests resp = conn.get(url, headers={"User-Agent": "seccerts.org"}) if resp.status_code != requests.codes.ok: raise ValueError(f"Unable to download: status={resp.status_code}") diff --git a/src/sec_certs/dataset/cpe.py b/src/sec_certs/dataset/cpe.py index 8728c35c..1a20c71e 100644 --- a/src/sec_certs/dataset/cpe.py +++ b/src/sec_certs/dataset/cpe.py @@ -62,7 +62,7 @@ class CPEDataset(JSONPathDataset, ComplexSerializableType): def __contains__(self, item: CPE) -> bool: if not isinstance(item, CPE): raise ValueError(f"{item} is not of CPE class") - return item.uri in self.cpes.keys() and self.cpes[item.uri] == item + return item.uri in self.cpes and self.cpes[item.uri] == item def __eq__(self, other: object) -> bool: return isinstance(other, CPEDataset) and self.cpes == other.cpes diff --git a/src/sec_certs/dataset/dataset.py b/src/sec_certs/dataset/dataset.py index fb079bfe..0066646b 100644 --- a/src/sec_certs/dataset/dataset.py +++ b/src/sec_certs/dataset/dataset.py @@ -456,9 +456,7 @@ class Dataset(Generic[CertSubType, AuxillaryDatasetsSubType], ComplexSerializabl logger.info("Translating label studio matches into their CPE representations and assigning to certificates.") for annotation in tqdm(data, desc="Translating label studio matches"): - cpe_candidate_keys = { - key for key in annotation.keys() if "option_" in key and annotation[key] != "No good match" - } + cpe_candidate_keys = {key for key in annotation if "option_" in key and annotation[key] != "No good match"} if "verified_cpe_match" not in annotation: incorrect_keys: set[str] = set() diff --git a/src/sec_certs/dataset/fips.py b/src/sec_certs/dataset/fips.py index c051cce8..6f8eff3e 100644 --- a/src/sec_certs/dataset/fips.py +++ b/src/sec_certs/dataset/fips.py @@ -187,13 +187,13 @@ class FIPSDataset(Dataset[FIPSCertificate, FIPSAuxillaryDatasets], ComplexSerial def _download_html_resources(self) -> None: logger.info("Downloading HTML files that list FIPS certificates.") html_urls = list(FIPSDataset.LIST_OF_CERTS_HTML.values()) - html_paths = [self.web_dir / x for x in FIPSDataset.LIST_OF_CERTS_HTML.keys()] + html_paths = [self.web_dir / x for x in FIPSDataset.LIST_OF_CERTS_HTML] helpers.download_parallel(html_urls, html_paths) def _get_all_certs_from_html_sources(self) -> list[FIPSCertificate]: return list( itertools.chain.from_iterable( - self._get_certificates_from_html(self.web_dir / x) for x in self.LIST_OF_CERTS_HTML.keys() + self._get_certificates_from_html(self.web_dir / x) for x in self.LIST_OF_CERTS_HTML ) ) diff --git a/src/sec_certs/dataset/fips_algorithm.py b/src/sec_certs/dataset/fips_algorithm.py index 0525653b..df113381 100644 --- a/src/sec_certs/dataset/fips_algorithm.py +++ b/src/sec_certs/dataset/fips_algorithm.py @@ -46,7 +46,7 @@ class FIPSAlgorithmDataset(JSONPathDataset, ComplexSerializableType): def __contains__(self, item: FIPSAlgorithm) -> bool: if not isinstance(item, FIPSAlgorithm): raise ValueError(f"{item} is not of FIPSAlgorithm class") - return item.dgst in self.algs.keys() and self.algs[item.dgst] == item + return item.dgst in self.algs and self.algs[item.dgst] == item def __eq__(self, other: object) -> bool: return isinstance(other, FIPSAlgorithmDataset) and self.algs == other.algs diff --git a/src/sec_certs/model/cpe_matching.py b/src/sec_certs/model/cpe_matching.py index 2602b00c..2c6b3f07 100644 --- a/src/sec_certs/model/cpe_matching.py +++ b/src/sec_certs/model/cpe_matching.py @@ -148,7 +148,7 @@ class CPEClassifier(BaseEstimator): def filter_condition(regex: Pattern, cpe: CPE, min_value: int, soft: bool = True): if matches := re.findall(regex, cpe.update): return int(re.findall(r"\d+", matches[0])[0]) >= min_value - return True if soft else False + return soft update_regexes = [cert_rules.SERVICE_PACK_RE, cert_rules.RELEASE_RE] diff --git a/src/sec_certs/sample/cc.py b/src/sec_certs/sample/cc.py index 1441a7b4..3930104b 100644 --- a/src/sec_certs/sample/cc.py +++ b/src/sec_certs/sample/cc.py @@ -471,9 +471,9 @@ class CCCertificate( self.manufacturer_web = sec_certs.utils.sanitization.sanitize_link(manufacturer_web) self.protection_profiles = protection_profiles self.maintenance_updates = maintenance_updates - self.state = self.InternalState() if not state else state - self.pdf_data = self.PdfData() if not pdf_data else pdf_data - self.heuristics: CCCertificate.Heuristics = self.Heuristics() if not heuristics else heuristics + self.state = state if state else self.InternalState() + self.pdf_data = pdf_data if pdf_data else self.PdfData() + self.heuristics: CCCertificate.Heuristics = heuristics if heuristics else self.Heuristics() @property def dgst(self) -> str: @@ -794,11 +794,10 @@ class CCCertificate( :param CCCertificate cert: cert to download the pdf security target for :return CCCertificate: returns the modified certificate with updated state """ - exit_code: str | int - if not cert.st_link: - exit_code = "No link" - else: - exit_code = helpers.download_file(cert.st_link, cert.state.st_pdf_path) + exit_code: str | int = ( + helpers.download_file(cert.st_link, cert.state.st_pdf_path) if cert.st_link else "No link" + ) + if exit_code != requests.codes.ok: error_msg = f"failed to download ST from {cert.st_link}, code: {exit_code}" logger.error(f"Cert dgst: {cert.dgst} " + error_msg) diff --git a/src/sec_certs/sample/fips.py b/src/sec_certs/sample/fips.py index 9396145f..4a9d573f 100644 --- a/src/sec_certs/sample/fips.py +++ b/src/sec_certs/sample/fips.py @@ -64,7 +64,7 @@ class FIPSHTMLParser: [x.find("div", class_="col-md-3") for x in entries], [x.find("div", class_="col-md-9") for x in entries] ) entries = [(FIPSHTMLParser.normalize_string(key.text), entry) for key, entry in entries] - entries = [parse_single_detail_entry(*x) for x in entries if x[0] in DETAILS_KEY_NORMALIZATION_DICT.keys()] + entries = [parse_single_detail_entry(*x) for x in entries if x[0] in DETAILS_KEY_NORMALIZATION_DICT] entries = {x: y for x, y in entries} if "caveat" in entries: @@ -139,7 +139,7 @@ class FIPSHTMLParser: @staticmethod def parse_tested_configurations(tested_configurations: Tag) -> list[str] | None: configurations = [y.text for y in tested_configurations.find_all("li")] - return configurations if not configurations == ["N/A"] else None + return None if configurations == ["N/A"] else configurations @staticmethod def normalize_embodiment(embodiment_element: Tag) -> str: @@ -385,7 +385,7 @@ class FIPSCertificate( """Returns numbers of certificates from keywords["fips_certlike"]["Certlike"]""" if self.keywords and "fips_certlike" in self.keywords: fips_certlike = self.keywords["fips_certlike"].get("Certlike", {}) - matches = {re.search(r"#\s{0,1}\d{1,4}", x) for x in fips_certlike.keys()} + matches = {re.search(r"#\s{0,1}\d{1,4}", x) for x in fips_certlike} return {"".join([x for x in match.group() if x.isdigit()]) for match in matches if match} else: return set() diff --git a/src/sec_certs/serialization/json.py b/src/sec_certs/serialization/json.py index bc77a3a3..7b523b9b 100644 --- a/src/sec_certs/serialization/json.py +++ b/src/sec_certs/serialization/json.py @@ -94,10 +94,7 @@ def serialize(func: Callable): def get_class_fullname(obj: Any) -> str: - if isinstance(obj, type): - klass = obj - else: - klass = obj.__class__ + klass = obj if isinstance(obj, type) else obj.__class__ module = klass.__module__ if module == "builtins": return klass.__qualname__ @@ -135,7 +132,7 @@ class CustomJSONDecoder(json.JSONDecoder): def object_hook(self, obj): if "_type" in obj and obj["_type"] == "Set": return set(obj["elements"]) - if "_type" in obj and obj["_type"] in self.serializable_complex_types.keys(): + if "_type" in obj and obj["_type"] in self.serializable_complex_types: complex_type = obj.pop("_type") return self.serializable_complex_types[complex_type].from_dict(obj) if "_type" in obj: diff --git a/src/sec_certs/utils/helpers.py b/src/sec_certs/utils/helpers.py index 3b294662..596ecf62 100644 --- a/src/sec_certs/utils/helpers.py +++ b/src/sec_certs/utils/helpers.py @@ -44,12 +44,11 @@ def download_file( ctx = nullcontext if r.status_code == requests.codes.ok: - with ctx() as pbar: - with output.open("wb") as f: - for data in r.iter_content(1024): - f.write(data) - if show_progress_bar: - pbar.update(len(data)) + with ctx() as pbar, output.open("wb") as f: + for data in r.iter_content(1024): + f.write(data) + if show_progress_bar: + pbar.update(len(data)) return r.status_code except requests.exceptions.Timeout: diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py index 38d61ded..edda0570 100644 --- a/src/sec_certs/utils/pdf.py +++ b/src/sec_certs/utils/pdf.py @@ -251,14 +251,8 @@ def text_is_garbage(text: str) -> bool: if len(set(line[1::2])) > 1: every_second += 1 - if lines: - avg_line_len = content_len / lines - else: - avg_line_len = 0 - if size: - alpha = alpha_len / size - else: - alpha = 0 + avg_line_len = content_len / lines if lines else 0 + alpha = alpha_len / size if size else 0 # If number of lines is small, this is garbage. if lines < GARBAGE_LINES_THRESHOLD: diff --git a/tests/test_common.py b/tests/test_common.py index d5e62cc4..fa7a3775 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -6,4 +6,4 @@ def test_rules(): assert "fips_cert_id" in fips_rules for rule_group in rules: if rule_group not in ("cc_rules", "fips_rules"): - assert rule_group in cc_rules.keys() or rule_group in fips_rules.keys() + assert rule_group in cc_rules or rule_group in fips_rules |
