Skip to content

Commit

Permalink
Add dymmond-settings to requirements
Browse files Browse the repository at this point in the history
* Deprecate pydantic-settings
  • Loading branch information
tarsil committed Jan 16, 2024
1 parent 41c0fca commit 6875ba2
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 103 deletions.
8 changes: 4 additions & 4 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ This is exactly what happened.

The way of using the settings object within a Mongoz use of the ORM is via:

* **MONGOZ_SETTINGS_MODULE** environment variable.
* **SETTINGS_MODULE** environment variable.

All the settings are **[Pydantic BaseSettings](https://pypi.org/project/pydantic-settings/)** objects which makes it easier to use and override
when needed.

### MONGOZ_SETTINGS_MODULE
### SETTINGS_MODULE

Mongoz by default uses is looking for a `MONGOZ_SETTINGS_MODULE` environment variable to run and
Mongoz by default uses is looking for a `SETTINGS_MODULE` environment variable to run and
apply the given settings to your instance.

If no `MONGOZ_SETTINGS_MODULE` is found, Mongoz then uses its own internal settings which are
If no `SETTINGS_MODULE` is found, Mongoz then uses its own internal settings which are
widely applied across the system.

#### Custom settings
Expand Down
2 changes: 2 additions & 0 deletions mongoz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .core.db.querysets.expressions import Expression, SortExpression
from .core.db.querysets.operators import Q
from .core.signals import Signal
from .core.utils.sync import run_sync
from .exceptions import DocumentNotFound, ImproperlyConfigured, MultipleDocumentsReturned

__all__ = [
Expand Down Expand Up @@ -67,4 +68,5 @@
"Time",
"UUID",
"settings",
"run_sync",
]
43 changes: 3 additions & 40 deletions mongoz/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
import os
from typing import Any, Optional, Type

from mongoz.conf.functional import LazyObject, empty
from mongoz.conf.module_import import import_string
if not os.environ.get("SETTINGS_MODULE"):
os.environ.setdefault("SETTINGS_MODULE", "mongoz.conf.global_settings.MongozSettings")

ENVIRONMENT_VARIABLE = "MONGOZ_SETTINGS_MODULE"

DBSettings = Type["MongozLazySettings"]


class MongozLazySettings(LazyObject):
def _setup(self, name: Optional[str] = None) -> None:
"""
Load the settings module pointed to by the environment variable. This
is used the first time settings are needed, if the user hasn't
configured settings manually.
"""
settings_module: str = os.environ.get(
ENVIRONMENT_VARIABLE, "mongoz.conf.global_settings.MongozSettings"
)
settings: Any = import_string(settings_module)

for setting, _ in settings().model_dump(exclude={"filter_operators"}).items():
assert setting.islower(), "%s should be in lowercase." % setting

self._wrapped = settings()

def __repr__(self: "MongozLazySettings") -> str:
# Hardcode the class name as otherwise it yields 'Settings'.
if self._wrapped is empty:
return "<MongozLazySettings [Unevaluated]>"
return '<MongozLazySettings "{settings_module}">'.format(
settings_module=self._wrapped.__class__.__name__
)

@property
def configured(self) -> Any:
"""Return True if the settings have already been configured."""
return self._wrapped is not empty


settings: DBSettings = MongozLazySettings() # type: ignore
from dymmond_settings import settings as settings
20 changes: 0 additions & 20 deletions mongoz/conf/config.py

This file was deleted.

9 changes: 0 additions & 9 deletions mongoz/conf/enums.py

This file was deleted.

15 changes: 8 additions & 7 deletions mongoz/conf/global_settings.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from dataclasses import dataclass
from functools import cached_property
from typing import TYPE_CHECKING, Dict, List, cast
from typing import TYPE_CHECKING, ClassVar, Dict, List, cast

from pydantic_settings import BaseSettings, SettingsConfigDict
from dymmond_settings import Settings

from mongoz.exceptions import OperatorInvalid

if TYPE_CHECKING:
from mongoz import Expression


class MongozSettings(BaseSettings):
model_config = SettingsConfigDict(extra="allow", ignored_types=(cached_property,))
ipython_args: List[str] = ["--no-banner"]
@dataclass
class MongozSettings(Settings):
ipython_args: ClassVar[List[str]] = ["--no-banner"]
ptpython_config_file: str = "~/.config/ptpython/config.py"

parsed_ids: List[str] = ["id", "pk"]
parsed_ids: ClassVar[List[str]] = ["id", "pk"]

filter_operators: Dict[str, str] = {
filter_operators: ClassVar[Dict[str, str]] = {
"exact": "eq",
"neq": "neq",
"contains": "contains",
Expand Down
22 changes: 0 additions & 22 deletions mongoz/conf/module_import.py

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ classifiers = [
]
dependencies = [
"motor>=3.3.1",
"dymmond-settings>=1.0.1",
"orjson>=3.9.5",
"pydantic>=2.5.3,<3.0.0",
"pydantic-settings>=2.0.3",
]
keywords = ["mongoz"]

Expand Down

0 comments on commit 6875ba2

Please sign in to comment.