-
Notifications
You must be signed in to change notification settings - Fork 480
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 6 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 |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| 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 +208,44 @@ def from_dict(cls, data: Mapping[str, Any]) -> TortoiseConfig: | |
| use_tz=data.get("use_tz"), | ||
| timezone=data.get("timezone"), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def merge_args( | ||
|
waketzheng marked this conversation as resolved.
Outdated
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
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: | ||
| if config is not None: | ||
| if config_file is not None: | ||
| raise ConfigurationError("Cannot specify both 'config' and 'config_file'") | ||
| return cls.from_dict(config) if isinstance(config, dict) else config | ||
| elif config_file is not None: | ||
| return cls._get_config_from_config_file(config_file) | ||
| elif db_url is None or modules is None: | ||
| raise ConfigurationError( | ||
| "Must provide either 'config', 'config_file', or both 'db_url' and 'modules'" | ||
| ) | ||
| else: | ||
| from tortoise.backends.base.config_generator import generate_config | ||
|
|
||
| config_dict = generate_config(db_url, app_modules=modules) | ||
| return cls.from_dict(config_dict) | ||
|
|
||
| @classmethod | ||
| def _get_config_from_config_file(cls, config_file: str) -> TortoiseConfig: | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
| _, extension = os.path.splitext(config_file) | ||
| if extension in (".yml", ".yaml"): | ||
| import yaml # pylint: disable=C0415 | ||
|
|
||
| with open(config_file) as f: | ||
| config = yaml.safe_load(f) | ||
| elif extension == ".json": | ||
| with open(config_file) as f: | ||
| config = json.load(f) | ||
| else: | ||
| raise ConfigurationError( | ||
| f"Unknown config extension {extension}, only .yml and .json are supported" | ||
| ) | ||
| return cls.from_dict(config) | ||
Uh oh!
There was an error while loading. Please reload this page.