| 61 | |
| 62 | @dataclass(frozen=True) |
| 63 | class AppConfig: |
| 64 | models: list[str] |
| 65 | default_connection: str | None = None |
| 66 | migrations: str | None = None |
| 67 | |
| 68 | def __post_init__(self) -> None: |
| 69 | if not isinstance(self.models, list) or not self.models: |
| 70 | raise ConfigurationError("AppConfig.models must be a non-empty list of strings") |
| 71 | for model in self.models: |
| 72 | if not isinstance(model, str) or not model: |
| 73 | raise ConfigurationError("AppConfig.models must contain non-empty strings") |
| 74 | if self.default_connection is not None and not isinstance(self.default_connection, str): |
| 75 | raise ConfigurationError("AppConfig.default_connection must be a string or None") |
| 76 | if self.migrations is not None and not isinstance(self.migrations, str): |
| 77 | raise ConfigurationError("AppConfig.migrations must be a string or None") |
| 78 | |
| 79 | def to_dict(self) -> dict[str, Any]: |
| 80 | data: dict[str, Any] = {"models": self.models} |
| 81 | if self.default_connection is not None: |
| 82 | data["default_connection"] = self.default_connection |
| 83 | if self.migrations is not None: |
| 84 | data["migrations"] = self.migrations |
| 85 | return data |
| 86 | |
| 87 | @classmethod |
| 88 | def from_dict(cls, data: Mapping[str, Any]) -> AppConfig: |
| 89 | if not isinstance(data, Mapping): |
| 90 | raise ConfigurationError("AppConfig must be created from a mapping") |
| 91 | if "models" not in data: |
| 92 | raise ConfigurationError('AppConfig requires "models"') |
| 93 | if not isinstance(data["models"], list): |
| 94 | raise ConfigurationError("AppConfig.models must be a list of strings") |
| 95 | return cls( |
| 96 | models=list(data["models"]), |
| 97 | default_connection=data.get("default_connection"), |
| 98 | migrations=data.get("migrations"), |
| 99 | ) |
| 100 | |
| 101 | |
| 102 | @dataclass(frozen=True) |
no outgoing calls
searching dependent graphs…