Skip to content

Commit

Permalink
refactored to linter score 9.81/10
Browse files Browse the repository at this point in the history
  • Loading branch information
Malutthias committed Oct 22, 2024
1 parent cd0a309 commit 470ddb8
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions logprep/util/auto_rule_tester/auto_rule_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
----------
Expand All @@ -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
----------
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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!")

Expand Down Expand Up @@ -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
----------
Expand All @@ -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
----------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 470ddb8

Please sign in to comment.