Handles file I/O operations.
| 8 | # ------------------------------------------------------------ |
| 9 | |
| 10 | |
| 11 | class FileManager: |
| 12 | """Handles file I/O operations.""" |
| 13 | |
| 14 | @staticmethod |
| 15 | def ensure_directory(path: str) -> None: |
| 16 | """Create directory if it doesn't exist.""" |
| 17 | os.makedirs(path, exist_ok=True) |
| 18 | |
| 19 | @staticmethod |
| 20 | def save_json(data: Any, filepath: str) -> None: |
| 21 | """Save data as JSON to file.""" |
| 22 | with open(filepath, "w", encoding="utf-8") as f: |
| 23 | json.dump(data, f, indent=4, ensure_ascii=False) |
| 24 | |
| 25 | @staticmethod |
| 26 | def load_json(filepath: str) -> Optional[Dict[str, Any]]: |
| 27 | """Load JSON from file, return None if file doesn't exist.""" |
| 28 | if not os.path.exists(filepath): |
| 29 | return None |
| 30 | |
| 31 | with open(filepath, "r", encoding="utf-8") as f: |
| 32 | return json.load(f) |
| 33 | |
| 34 | @staticmethod |
| 35 | def save_text(content: str, filepath: str) -> None: |
| 36 | """Save text content to file.""" |
| 37 | with open(filepath, "w", encoding="utf-8") as f: |
| 38 | f.write(content) |
| 39 | |
| 40 | @staticmethod |
| 41 | def load_text(filepath: str) -> str: |
| 42 | """Load text content from file.""" |
| 43 | with open(filepath, "r", encoding="utf-8") as f: |
| 44 | return f.read() |
| 45 | |
| 46 |