|
| 1 | +"""Core domain models for text replacement.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from dataclasses import dataclass, field |
| 9 | +from pathlib import Path |
| 10 | +from shutil import copy2 |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class Config: |
| 15 | + """Configuration for text replacement.""" |
| 16 | + |
| 17 | + dictionaries: dict[str, dict[str, str]] |
| 18 | + ignore_extensions: list[str] = field(default_factory=list) |
| 19 | + ignore_directories: list[str] = field(default_factory=list) |
| 20 | + ignore_file_prefixes: list[str] = field(default_factory=list) |
| 21 | + version: str = "1.0" |
| 22 | + |
| 23 | + def get_dictionary(self, name: str, direction: int) -> dict[str, str]: |
| 24 | + """Get replacement dictionary for a direction.""" |
| 25 | + raw = self.dictionaries.get(name, {}) |
| 26 | + if direction == 2: |
| 27 | + raw = {v: k for k, v in raw.items()} |
| 28 | + return raw |
| 29 | + |
| 30 | + |
| 31 | +class FileOperator(ABC): |
| 32 | + """Abstract interface for file operations.""" |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def read_text(self, path: Path) -> str: |
| 36 | + """Read text from a file.""" |
| 37 | + |
| 38 | + @abstractmethod |
| 39 | + def write_text(self, path: Path, content: str) -> None: |
| 40 | + """Write text to a file.""" |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def file_exists(self, path: Path) -> bool: |
| 44 | + """Check if a file exists.""" |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def walk(self, directory: Path): |
| 48 | + """Walk directory tree.""" |
| 49 | + |
| 50 | + @abstractmethod |
| 51 | + def ensure_dir_exists(self, path: Path) -> None: |
| 52 | + """Ensure directory exists.""" |
| 53 | + |
| 54 | + @abstractmethod |
| 55 | + def make_backup(self, path: Path, backup_dir: Path) -> Path: |
| 56 | + """Create a backup of a file.""" |
| 57 | + |
| 58 | + @abstractmethod |
| 59 | + def list_files(self, directory: Path, pattern: str = "*") -> list[Path]: |
| 60 | + """List files matching pattern.""" |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def is_binary(self, path: Path) -> bool: |
| 64 | + """Check if file is binary.""" |
| 65 | + |
| 66 | + @abstractmethod |
| 67 | + def create_backup(self, path: Path, backup_dir: Path) -> Path: |
| 68 | + """Create a backup of a file.""" |
| 69 | + |
| 70 | + |
| 71 | +class LocalFileOperator(FileOperator): |
| 72 | + """Local filesystem file operator with backup support.""" |
| 73 | + |
| 74 | + def __init__(self, backup_dir: Path | None = None): |
| 75 | + self.backup_dir = backup_dir |
| 76 | + |
| 77 | + def read_text(self, path: Path) -> str: |
| 78 | + with open(path, encoding="utf-8") as f: |
| 79 | + return f.read() |
| 80 | + |
| 81 | + def write_text(self, path: Path, content: str) -> None: |
| 82 | + parent = path.parent |
| 83 | + if parent: |
| 84 | + parent.mkdir(parents=True, exist_ok=True) |
| 85 | + |
| 86 | + fd, tmp_path = tempfile.mkstemp(dir=parent, prefix=path.name + ".", text=True) |
| 87 | + try: |
| 88 | + with os.fdopen(fd, "w", encoding="utf-8") as f: |
| 89 | + f.write(content) |
| 90 | + os.replace(tmp_path, path) |
| 91 | + except Exception: |
| 92 | + try: |
| 93 | + os.unlink(tmp_path) |
| 94 | + except OSError: |
| 95 | + pass |
| 96 | + raise |
| 97 | + |
| 98 | + def file_exists(self, path: Path) -> bool: |
| 99 | + return path.exists() |
| 100 | + |
| 101 | + def ensure_dir_exists(self, path: Path) -> None: |
| 102 | + path.mkdir(parents=True, exist_ok=True) |
| 103 | + |
| 104 | + def make_backup(self, path: Path, backup_dir: Path) -> Path: |
| 105 | + if not self.backup_dir: |
| 106 | + raise ValueError("No backup directory configured") |
| 107 | + self.ensure_dir_exists(self.backup_dir) |
| 108 | + rel_path = path.relative_to(os.getcwd()) |
| 109 | + backup_path = self.backup_dir / rel_path |
| 110 | + backup_path.parent.mkdir(parents=True, exist_ok=True) |
| 111 | + copy2(path, backup_path) |
| 112 | + return backup_path |
| 113 | + |
| 114 | + def walk(self, directory: Path): |
| 115 | + yield from os.walk(directory) |
| 116 | + |
| 117 | + def list_files(self, directory: Path, pattern: str = "*") -> list[Path]: |
| 118 | + return list(Path(directory).glob(pattern)) |
| 119 | + |
| 120 | + def is_binary(self, path: Path) -> bool: |
| 121 | + try: |
| 122 | + path.read_text(encoding="utf-8") |
| 123 | + return False |
| 124 | + except UnicodeDecodeError: |
| 125 | + return True |
| 126 | + |
| 127 | + def create_backup(self, path: Path, backup_dir: Path) -> Path: |
| 128 | + if not self.backup_dir: |
| 129 | + raise ValueError("No backup directory configured") |
| 130 | + return self.make_backup(path, backup_dir) |
0 commit comments