import json import logging from pathlib import Path from typing import List, Dict, Any, Callable, Optional from .config import DATA_DIR logger = logging.getLogger("DataLoader") INTERNAL_REPORT_PATH = DATA_DIR / 'research_report.json' BAYMARD_PATH = DATA_DIR / 'baymard_unified_database_v2.json' NNG_PATH = DATA_DIR / 'nng_articles.json' def _load_json_file( path: Path, label: str, flatten_fn: Optional[Callable[[Any], List[Dict[str, Any]]]] = None, ) -> List[Dict[str, Any]]: """Load a JSON file with standard error handling. Args: path: Path to the JSON file. label: Human-readable label for log messages. flatten_fn: Optional function to transform raw data into a flat list. If None, the raw data is returned as-is (must already be a list). """ try: with open(path, 'r', encoding='utf-8') as f: data = json.load(f) items = flatten_fn(data) if flatten_fn else data logger.info(f"Loaded {len(items)} {label} items.") return items except FileNotFoundError: logger.error(f"{label} file not found: {path}") return [] except json.JSONDecodeError as e: logger.error(f"Invalid JSON in {label} file: {e}") return [] def _flatten_collections(data: Any) -> List[Dict[str, Any]]: """Flatten {collections: {name: {guidelines: [...]}}} structure.""" items: List[Dict[str, Any]] = [] if 'collections' in data: for _name, collection_data in data['collections'].items(): if 'guidelines' in collection_data: items.extend(collection_data['guidelines']) return items def load_internal_research() -> List[Dict[str, Any]]: """Loads internal research reports from JSON.""" return _load_json_file(INTERNAL_REPORT_PATH, "internal research") def load_baymard_guidelines() -> List[Dict[str, Any]]: """Loads Baymard guidelines from JSON.""" return _load_json_file(BAYMARD_PATH, "Baymard", flatten_fn=_flatten_collections) def load_nng_articles() -> List[Dict[str, Any]]: """Loads NN/g articles from JSON.""" return _load_json_file(NNG_PATH, "NN/g", flatten_fn=_flatten_collections) if __name__ == "__main__": # Test loading internal = load_internal_research() baymard = load_baymard_guidelines() nng = load_nng_articles()