Add rules module: security and antipattern detection rules
This commit is contained in:
57
src/rules/__init__.py
Normal file
57
src/rules/__init__.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
"""Rule registry and loading functionality."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
class RuleRegistry:
|
||||||
|
"""Registry for analysis rules."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._rules = {}
|
||||||
|
self._enabled_by_default = set()
|
||||||
|
|
||||||
|
def register(self, rule_id: str, rule_config: dict, enabled: bool = True):
|
||||||
|
self._rules[rule_id] = rule_config
|
||||||
|
if enabled:
|
||||||
|
self._enabled_by_default.add(rule_id)
|
||||||
|
|
||||||
|
def get_rule(self, rule_id: str) -> Optional[dict]:
|
||||||
|
return self._rules.get(rule_id)
|
||||||
|
|
||||||
|
def is_enabled(self, rule_id: str, config: Optional[dict] = None) -> bool:
|
||||||
|
if config and "rules" in config:
|
||||||
|
rule_config = config["rules"].get(rule_id, {})
|
||||||
|
if "enabled" in rule_config:
|
||||||
|
return rule_config["enabled"]
|
||||||
|
return rule_id in self._enabled_by_default
|
||||||
|
|
||||||
|
def get_enabled_rules(self, config: Optional[dict] = None) -> list[str]:
|
||||||
|
enabled = []
|
||||||
|
for rule_id in self._rules:
|
||||||
|
if self.is_enabled(rule_id, config):
|
||||||
|
enabled.append(rule_id)
|
||||||
|
return enabled
|
||||||
|
|
||||||
|
def list_rules(self) -> list[str]:
|
||||||
|
return list(self._rules.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(config_path: Optional[Path] = None) -> dict:
|
||||||
|
"""Load configuration from file."""
|
||||||
|
if config_path and config_path.exists():
|
||||||
|
with open(config_path) as f:
|
||||||
|
return yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
default_paths = [
|
||||||
|
Path.home() / ".aicoderc.yaml",
|
||||||
|
Path.cwd() / ".aicoderc.yaml",
|
||||||
|
]
|
||||||
|
|
||||||
|
for path in default_paths:
|
||||||
|
if path.exists():
|
||||||
|
with open(path) as f:
|
||||||
|
return yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
return {}
|
||||||
Reference in New Issue
Block a user