rule_base.py
- Pfad:
/var/www/tools/ki-protokoll/claude-hook/quality/rule_base.py
- Namespace: claude-hook.quality
- Zeilen: 151 | Größe: 3,954 Bytes
- Geändert: 2025-12-25 16:58:45 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 95
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 50 (10%)
Issues 5
| Zeile |
Typ |
Beschreibung |
| 39 |
magic_number |
Magic Number gefunden: 100 |
| 39 |
magic_number |
Magic Number gefunden: 1000 |
| 40 |
magic_number |
Magic Number gefunden: 60 |
| 40 |
magic_number |
Magic Number gefunden: 24 |
| 40 |
magic_number |
Magic Number gefunden: 365 |
Dependencies 4
- use re
- use pathlib.Path
- use typing.List
- use typing.Callable
Klassen 1
Funktionen 5
-
is_in_allowlist()
Zeile 53
-
count_non_empty_lines()
Zeile 58
-
block()
Zeile 63
-
allow()
Zeile 71
-
collect_rules()
Zeile 140
Code
#!/usr/bin/env python3
"""
Basis-Klasse für Quality-Gate-Regeln.
Stellt gemeinsame Funktionalität für alle Regel-Module bereit.
"""
import re
from pathlib import Path
from typing import List, Callable
# =============================================================================
# ALLOWLIST DEFINITIONS
# =============================================================================
GLOBAL_ALLOWLIST = [
"/vendor/",
"/tests/",
"/Test/",
]
DTO_ALLOWLIST = [
"/Infrastructure/DTO/",
"/DTO/",
]
MIGRATION_ALLOWLIST = [
"/migrations/",
"/Migration/",
]
# =============================================================================
# COMMON NUMBERS - Erlaubte Magic Numbers
# =============================================================================
COMMON_NUMBERS = {
'0', '1', '2',
'10', '100', '1000',
'60', '24', '365',
'30', '31', '28', '29',
'12', '52', '7',
'200', '201', '204',
'301', '302', '304',
'400', '401', '403', '404', '500',
}
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
def is_in_allowlist(file_path: str, allowlist: list) -> bool:
"""Prüft ob Pfad in Allowlist ist."""
return any(allowed in file_path for allowed in allowlist)
def count_non_empty_lines(content: str) -> int:
"""Zählt nicht-leere Zeilen."""
return len([line for line in content.split('\n') if line.strip()])
def block(rule_id: str, message: str) -> dict:
"""Erzeugt Block-Response für Pre-Hooks."""
return {
"allowed": False,
"message": f"QUALITY VIOLATION [{rule_id}]: {message}"
}
def allow() -> dict:
"""Erzeugt Allow-Response für Pre-Hooks."""
return {"allowed": True}
# =============================================================================
# RULE BASE CLASS
# =============================================================================
class Rule:
"""
Basis-Klasse für alle Quality-Gate-Regeln.
Jede Regel ist eine Callable-Klasse die file_path und content nimmt
und eine Liste von Warnings zurückgibt.
"""
def __init__(self, allowlist: List[str] = None):
"""
Args:
allowlist: Zusätzliche Allowlist-Pfade für diese Regel
"""
self.allowlist = allowlist or []
def should_skip(self, file_path: str) -> bool:
"""
Prüft ob diese Datei übersprungen werden soll.
Args:
file_path: Pfad zur zu prüfenden Datei
Returns:
True wenn Datei übersprungen werden soll
"""
return is_in_allowlist(file_path, GLOBAL_ALLOWLIST + self.allowlist)
def check(self, file_path: str, content: str) -> List[str]:
"""
Führt die Regel-Prüfung aus.
Args:
file_path: Pfad zur zu prüfenden Datei
content: Dateiinhalt
Returns:
Liste von Warning-Messages
"""
raise NotImplementedError("Subclasses must implement check()")
def __call__(self, file_path: str, content: str) -> List[str]:
"""
Macht die Regel-Klasse callable.
Args:
file_path: Pfad zur zu prüfenden Datei
content: Dateiinhalt
Returns:
Liste von Warning-Messages
"""
if self.should_skip(file_path):
return []
return self.check(file_path, content)
# =============================================================================
# RULE COLLECTION
# =============================================================================
def collect_rules(rule_list: List[Callable]) -> List[Callable]:
"""
Sammelt alle Regeln aus einem Modul.
Args:
rule_list: Liste von Regel-Funktionen oder Regel-Klassen
Returns:
Liste von callables
"""
return rule_list