| 101 | |
| 102 | @dataclass(frozen=True) |
| 103 | class TortoiseConfig: |
| 104 | connections: dict[str, ConnectionConfig | DBUrlConfig] |
| 105 | apps: dict[str, AppConfig] |
| 106 | routers: list[str | type] | None = None |
| 107 | use_tz: bool | None = None |
| 108 | timezone: str | None = None |
| 109 | |
| 110 | def __post_init__(self) -> None: |
| 111 | if not isinstance(self.connections, dict) or not self.connections: |
| 112 | raise ConfigurationError("TortoiseConfig.connections must be a non-empty dict") |
| 113 | for name, conn in self.connections.items(): |
| 114 | if not isinstance(name, str) or not name: |
| 115 | raise ConfigurationError("Connection names must be non-empty strings") |
| 116 | if not isinstance(conn, (ConnectionConfig, DBUrlConfig)): |
| 117 | raise ConfigurationError( |
| 118 | "Connection values must be ConnectionConfig or DBUrlConfig" |
| 119 | ) |
| 120 | |
| 121 | if not isinstance(self.apps, dict) or not self.apps: |
| 122 | raise ConfigurationError("TortoiseConfig.apps must be a non-empty dict") |
| 123 | for name, app in self.apps.items(): |
| 124 | if not isinstance(name, str) or not name: |
| 125 | raise ConfigurationError("App names must be non-empty strings") |
| 126 | if not isinstance(app, AppConfig): |
| 127 | raise ConfigurationError("App values must be AppConfig") |
| 128 | if app.default_connection and app.default_connection not in self.connections: |
| 129 | raise ConfigurationError( |
| 130 | f'App "{name}" refers to unknown connection "{app.default_connection}"' |
| 131 | ) |
| 132 | |
| 133 | if self.routers is not None: |
| 134 | if not isinstance(self.routers, list): |
| 135 | raise ConfigurationError("TortoiseConfig.routers must be a list or None") |
| 136 | for router in self.routers: |
| 137 | if not isinstance(router, (str, type)): |
| 138 | raise ConfigurationError("Routers must be str or type") |
| 139 | |
| 140 | if self.use_tz is not None and not isinstance(self.use_tz, bool): |
| 141 | raise ConfigurationError("TortoiseConfig.use_tz must be a bool or None") |
| 142 | |
| 143 | if self.timezone is not None and not isinstance(self.timezone, str): |
| 144 | raise ConfigurationError("TortoiseConfig.timezone must be a string or None") |
| 145 | |
| 146 | def to_dict(self) -> dict[str, Any]: |
| 147 | connections = {name: conn.to_config() for name, conn in self.connections.items()} |
| 148 | apps = {name: app.to_dict() for name, app in self.apps.items()} |
| 149 | config: dict[str, Any] = { |
| 150 | "connections": connections, |
| 151 | "apps": apps, |
| 152 | } |
| 153 | if self.routers is not None: |
| 154 | config["routers"] = self.routers |
| 155 | if self.use_tz is not None: |
| 156 | config["use_tz"] = self.use_tz |
| 157 | if self.timezone is not None: |
| 158 | config["timezone"] = self.timezone |
| 159 | return config |
| 160 |
no outgoing calls
searching dependent graphs…