diff --git a/src/rules/__init__.py b/src/rules/__init__.py new file mode 100644 index 0000000..342f38a --- /dev/null +++ b/src/rules/__init__.py @@ -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 {}