""" Rules Loader - YAML 기반 표준 규칙 데이터 로드 및 캐싱. Usage: from app.rules import get_rules rules = get_rules("html_css") # html_css.yaml 전체 로드 rules = get_rules("accessibility") # accessibility.yaml 전체 로드 """ import logging from pathlib import Path from typing import Any import yaml logger = logging.getLogger(__name__) RULES_DIR = Path(__file__).parent _cache: dict[str, dict[str, Any]] = {} def get_rules(category: str) -> dict[str, Any]: """ Load and cache YAML rules for a given category. Args: category: One of "html_css", "accessibility", "seo", "performance_security" Returns: Parsed YAML data as a dictionary. """ if category in _cache: return _cache[category] yaml_path = RULES_DIR / f"{category}.yaml" if not yaml_path.exists(): logger.error("Rules file not found: %s", yaml_path) return {} with open(yaml_path, "r", encoding="utf-8") as f: data = yaml.safe_load(f) or {} _cache[category] = data logger.info("Loaded rules: %s (%d bytes)", category, yaml_path.stat().st_size) return data def reload_rules(category: str | None = None) -> None: """ Clear cache and reload rules. If category is None, reload all cached rules. """ if category: _cache.pop(category, None) get_rules(category) else: categories = list(_cache.keys()) _cache.clear() for cat in categories: get_rules(cat) def get_all_categories() -> list[str]: """Return list of available rule categories.""" return [ p.stem for p in RULES_DIR.glob("*.yaml") ]