From 470ddb8ffd89f206e747c8fd9292e1fbf8cbb495 Mon Sep 17 00:00:00 2001 From: Ma Lu Date: Tue, 22 Oct 2024 14:28:09 +0200 Subject: [PATCH] refactored to linter score 9.81/10 --- .../util/auto_rule_tester/auto_rule_tester.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/logprep/util/auto_rule_tester/auto_rule_tester.py b/logprep/util/auto_rule_tester/auto_rule_tester.py index 16aec003e..150bad1f3 100644 --- a/logprep/util/auto_rule_tester/auto_rule_tester.py +++ b/logprep/util/auto_rule_tester/auto_rule_tester.py @@ -47,23 +47,21 @@ import hashlib import json -from pathlib import Path import re import sys import tempfile from collections import OrderedDict, defaultdict +from collections.abc import Iterable from difflib import ndiff from logging import getLogger from os import path +from pathlib import Path from pprint import pprint -from typing import TYPE_CHECKING -from collections.abc import Iterable - -from more_itertools import nth +from typing import TYPE_CHECKING, Union from colorama import Fore +from more_itertools import nth from ruamel.yaml import YAML, YAMLError -from typing import Union from logprep.factory import Factory from logprep.framework.rule_tree.rule_tree import RuleTree @@ -139,7 +137,8 @@ def update_errors(self, processor: PreDetector, extra_output: list, problems: di problems["warnings"].extend(id_warnings) def print_rules(self, rules, t_idx=None): - """Iterate through every printable and assign right processing resulting in a coloured output + """Iterate through every printable and assign right processing resulting in + a coloured output Parameters ---------- @@ -154,7 +153,8 @@ def print_rules(self, rules, t_idx=None): @staticmethod def print_diff_test(key, rule, t_idx=None): - """Determine right processing for printable: no iterable, indexed and non index queried iterable + """Determine right processing for printable: no iterable, indexed and non + index queried iterable Parameters ---------- @@ -230,7 +230,7 @@ def load_json_or_yaml(self, file_path) -> Union[list, dict]: return json.load(file) except (json.JSONDecodeError, YAMLError) as error: - raise ValueError(f"Error decoding {file_path}: {str(error)}") + raise ValueError(f"Error decoding {file_path}: {str(error)}") from error class AutoRuleTester: @@ -284,7 +284,6 @@ def _run_if_any_rules_exist(self, rules_pn: dict) -> None: """ if any(processor_test_cfg["rules"] for processor_test_cfg in rules_pn.values()): self._run_tests_for_rules(rules_pn) - return else: print_fcolor(Fore.YELLOW, "~\nThere are no rules within any of the rules directories!") @@ -409,7 +408,8 @@ def _load_rules(self, processor: "Processor", rule_type: str): def _prepare_test_eval( self, processor: "Processor", rule_dict: dict, rule_type: str, temp_rule_path: str ) -> None: - """Prepare test eval: Create rule file, then reset tree of processor and then load the rules for the processor + """Prepare test eval: Create rule file, then reset tree of processor and then load + the rules for the processor Parameters ---------- @@ -427,7 +427,8 @@ def _prepare_test_eval( self._load_rules(processor, rule_type) def _eval_file_rule_test(self, rule_test: dict, processor: "Processor", r_idx: int): - """Main logic to check each rule file, compare and validate it, then print out results. For each processor a process is spawned. + """Main logic to check each rule file, compare and validate it, then print out results. + For each processor a process is spawned. Parameters ---------- @@ -455,7 +456,7 @@ def _eval_file_rule_test(self, rule_test: dict, processor: "Processor", r_idx: i f"- Can't process RULE FILE {rule_test['file']}. No extra output generated" ) sys.exit(1) - except BaseException as error: + except Exception: self._success = False self._result["- Failed Tests"] += 1 continue @@ -552,7 +553,6 @@ def _check_which_rule_files_miss_tests(self, rules_pn) -> None: """ rule_tests = {"with tests": [], "without tests": []} for _, processor_test_cfg in rules_pn.items(): - processor_type = processor_test_cfg["type"] rules = processor_test_cfg["rules"] for rule in rules: @@ -757,4 +757,4 @@ def _get_rules_to_add(processor_cfg) -> list: rules_to_add.append(("generic_rules", processor_cfg["generic_rules"][0])) rules_to_add.append(("specific_rules", processor_cfg["specific_rules"][0])) - return rules_to_add + return rules_to_add \ No newline at end of file