From 6875ba28319bd47a430071a2c457e07e69d8f8d2 Mon Sep 17 00:00:00 2001 From: tarsil Date: Tue, 16 Jan 2024 10:06:58 +0000 Subject: [PATCH] Add dymmond-settings to requirements * Deprecate pydantic-settings --- docs/settings.md | 8 +++---- mongoz/__init__.py | 2 ++ mongoz/conf/__init__.py | 43 +++------------------------------- mongoz/conf/config.py | 20 ---------------- mongoz/conf/enums.py | 9 ------- mongoz/conf/global_settings.py | 15 ++++++------ mongoz/conf/module_import.py | 22 ----------------- pyproject.toml | 2 +- 8 files changed, 18 insertions(+), 103 deletions(-) delete mode 100644 mongoz/conf/config.py delete mode 100644 mongoz/conf/enums.py delete mode 100644 mongoz/conf/module_import.py diff --git a/docs/settings.md b/docs/settings.md index b21d6b9..24a215b 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -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 diff --git a/mongoz/__init__.py b/mongoz/__init__.py index 3e0ed28..f4206e4 100644 --- a/mongoz/__init__.py +++ b/mongoz/__init__.py @@ -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__ = [ @@ -67,4 +68,5 @@ "Time", "UUID", "settings", + "run_sync", ] diff --git a/mongoz/conf/__init__.py b/mongoz/conf/__init__.py index cabcbb2..91f6a48 100644 --- a/mongoz/conf/__init__.py +++ b/mongoz/conf/__init__.py @@ -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 "" - return ''.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 diff --git a/mongoz/conf/config.py b/mongoz/conf/config.py deleted file mode 100644 index bb8aa82..0000000 --- a/mongoz/conf/config.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Configuration for Pydantic models.""" -from __future__ import annotations as _annotations - -from typing_extensions import TypedDict - - -class ModelConfig(TypedDict, total=False): - """ - A TypedDict for configuring Document behaviour. - """ - - strict: bool - """ - Weather the model should be strict in the creation or not. - """ - populate_by_alias: bool - """ - Whether an aliased field may be populated by its name as given by the model - attribute, as well as the alias. Defaults to `False`. - """ diff --git a/mongoz/conf/enums.py b/mongoz/conf/enums.py deleted file mode 100644 index 35694e8..0000000 --- a/mongoz/conf/enums.py +++ /dev/null @@ -1,9 +0,0 @@ -from enum import Enum - - -class EnvironmentType(str, Enum): - """An Enum for HTTP methods.""" - - DEVELOPMENT = "development" - TESTING = "testing" - PRODUCTION = "production" diff --git a/mongoz/conf/global_settings.py b/mongoz/conf/global_settings.py index 6738ad4..7cfd527 100644 --- a/mongoz/conf/global_settings.py +++ b/mongoz/conf/global_settings.py @@ -1,7 +1,8 @@ +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 @@ -9,14 +10,14 @@ 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", diff --git a/mongoz/conf/module_import.py b/mongoz/conf/module_import.py deleted file mode 100644 index 59e9105..0000000 --- a/mongoz/conf/module_import.py +++ /dev/null @@ -1,22 +0,0 @@ -from importlib import import_module -from typing import Any - - -def import_string(dotted_path: str) -> Any: - """ - Import a dotted module path and return the attribute/class designated by the - last name in the path. Raise ImportError if the import failed. - """ - try: - module_path, class_name = dotted_path.rsplit(".", 1) - except ValueError as err: - raise ImportError("%s doesn't look like a module path" % dotted_path) from err - - module = import_module(module_path) - - try: - return getattr(module, class_name) - except AttributeError as err: - raise ImportError( - 'Module "{}" does not define a "{}" attribute/class'.format(module_path, class_name) - ) from err diff --git a/pyproject.toml b/pyproject.toml index 10b0489..83f380d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]