-
Notifications
You must be signed in to change notification settings - Fork 500
feat: expose classmethod to build tortoise config #2162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
23467a9
e656ce9
b1e1093
1de8dfc
d7d62d5
0cf3b41
c9e905a
260de75
5f71748
3b78e1e
5167053
cb9f3f4
9ea070b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,14 @@ | |
|
|
||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from tortoise.exceptions import ConfigurationError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable | ||
| from types import ModuleType | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DBUrlConfig: | ||
|
|
@@ -202,3 +206,36 @@ def from_dict(cls, data: Mapping[str, Any]) -> TortoiseConfig: | |
| use_tz=data.get("use_tz"), | ||
| timezone=data.get("timezone"), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def merge_args( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure the method name is clear, Maybe Moreover, maybe we need to normalize the classmethods in this class: @classmethod
def generate_config(
cls,
config: dict[str, Any] | Self | None = None,
config_file: str | None = None,
db_url: str | None = None,
modules: dict[str, Iterable[str | ModuleType]] | None = None,
) -> Self:
...
@classmethod
def generate_config_from_db_url_and_modules(cls, db_url: str, modules: dict[str, Iterable[str | ModuleType]]) -> Self:
...
@classmethod
def generate_config_from_config_file(cls, config_file: str) -> Self:
...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestions. I’ve updated the naming:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is one solution. However, I strongly recommend using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
waketzheng marked this conversation as resolved.
Outdated
|
||
| cls, | ||
| config: dict[str, Any] | TortoiseConfig | None = None, | ||
| config_file: str | None = None, | ||
| db_url: str | None = None, | ||
| modules: dict[str, Iterable[str | ModuleType]] | None = None, | ||
| ) -> TortoiseConfig: | ||
| # Handle config_file: load it as config dict | ||
| if config_file is not None: | ||
| from tortoise import Tortoise | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
|
|
||
| if config is not None: | ||
| raise ConfigurationError("Cannot specify both 'config' and 'config_file'") | ||
| config = Tortoise._get_config_from_config_file(config_file) | ||
|
|
||
| # Convert input to TortoiseConfig for typed access | ||
| typed_config: TortoiseConfig | ||
| if config is None: | ||
| from tortoise.backends.base.config_generator import generate_config | ||
|
|
||
| if db_url is None or modules is None: | ||
| raise ConfigurationError( | ||
| "Must provide either 'config', 'config_file', or both 'db_url' and 'modules'" | ||
| ) | ||
| config_dict = generate_config(db_url, app_modules=modules) | ||
| typed_config = cls.from_dict(config_dict) | ||
| elif isinstance(config, dict): | ||
| typed_config = cls.from_dict(config) | ||
| else: | ||
| typed_config = config | ||
| return typed_config | ||
Uh oh!
There was an error while loading. Please reload this page.