Skip to content

Commit

Permalink
Cleanup (#232)
Browse files Browse the repository at this point in the history
Changes:

- Update release notes
- Note explicit that app must be a module
- move monkay to _monkay.py file
- deprecate EnvironmentType
- remove edgy.conf.functional. Its only use was the settings.
- lazify all imports
- provide types for files, fields modules
  • Loading branch information
devkral authored Nov 18, 2024
1 parent 848cad7 commit 57bf99f
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 267 deletions.
6 changes: 5 additions & 1 deletion docs/migrations/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,13 @@ it triggered the auto discovery of the application.

##### Using the --app or EDGY_DISCOVERY_APP

!!! Note
There was a change in 0.23.0: the import path must be to a module in which the registration via the `Instance` object is automatically triggered.
See [Connection](connection.md).

###### --app

With the `--app` flag.
With the `--app` parameter.

```shell
$ edgy --app src.main makemigrations
Expand Down
7 changes: 7 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ hide:
### Changed

- Rework edgy to use Monkay.
- Imports are now lazy.
- Rework the migrate and shell system to simply use Monkay instance.
- Replace `get_registry_copy` by `get_migration_prepared_registry`.
- Breaking: migration configuration takes place in settings.
Expand All @@ -23,13 +24,19 @@ hide:
- Breaking: `model_apps` is replaced by `preloads` but still available during the migration time.
- Breaking:
An automatic registration is assumed. See [Connection](connection.md) for examples.
- Breaking: `--app` or `EDGY_DEFAULT_APP` must point to a module which does the self-registration not an app instance anymore.
- Deprecate `edgy.conf.enums.EnvironmentType`. Esmeralds `EnvironmentType` or an own definition should be used instead.

### Fixed

- Migrations with ManyToMany fields are broken.
- `get_engine_url_and_metadata` was broken for some operations (thanks @kokoserver).
- IPAddressField was not exposed as edgy.IPAddressField.

### Removed

- `edgy.conf.functional`. It was only used for configuration and is now superseeded by Monkay.

### Contributors

Thanks a lot to @kokoserver. He provided a *lot* of valuable bug reports and PRs.
Expand Down
2 changes: 1 addition & 1 deletion docs_src/settings/custom_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from edgy import EdgySettings
from edgy.conf.enums import EnvironmentType
from esmerald.conf.enums import EnvironmentType


class MyCustomSettings(EdgySettings):
Expand Down
94 changes: 18 additions & 76 deletions edgy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
from __future__ import annotations

__version__ = "0.23.0"
import os
from importlib import import_module
from typing import TYPE_CHECKING, Any, NamedTuple, Optional
from typing import TYPE_CHECKING

from monkay import Monkay

from edgy.conf.global_settings import EdgySettings
from edgy.core.connection import Database, DatabaseURL, Registry
from edgy.core.db.models import (
Manager,
Model,
ModelRef,
RedirectManager,
ReflectModel,
StrictModel,
)
from edgy.core.db.querysets import Prefetch, Q, QuerySet, and_, not_, or_
from edgy.core.utils.sync import run_sync
from ._monkay import Instance, create_monkay
from .core.utils.sync import run_sync

if TYPE_CHECKING:
from .conf.global_settings import EdgySettings
from .core import files
from .core.connection import Database, DatabaseURL, Registry
from .core.db import fields
from .core.db.datastructures import Index, UniqueConstraint
from .core.db.models import (
Manager,
Model,
ModelRef,
RedirectManager,
ReflectModel,
StrictModel,
)
from .core.db.querysets import Prefetch, Q, QuerySet, and_, not_, or_
from .core.signals import Signal
from .exceptions import MultipleObjectsReturned, ObjectNotFound


class Instance(NamedTuple):
registry: Registry
app: Optional[Any] = None


__all__ = [
"Instance",
"get_migration_prepared_registry",
Expand Down Expand Up @@ -108,61 +102,9 @@ class Instance(NamedTuple):
"DatabaseURL",
"Registry",
]
monkay = create_monkay(globals(), __all__)

monkay: Monkay[Instance, EdgySettings] = Monkay(
globals(),
with_extensions=True,
with_instance=True,
# must be at least an empty string to initialize the settings
settings_path=os.environ.get("EDGY_SETTINGS_MODULE", "edgy.conf.global_settings.EdgySettings"),
settings_extensions_name="extensions",
settings_preloads_name="preloads",
uncached_imports={"settings"},
lazy_imports={
"settings": lambda: monkay.settings,
"fields": lambda: import_module("edgy.core.db.fields"),
"files": lambda: import_module("edgy.core.files"),
"Signal": "edgy.core.signals:Signal",
"MultipleObjectsReturned": "edgy.exceptions:MultipleObjectsReturned",
"ObjectNotFound": "edgy.exceptions:ObjectNotFound",
"UniqueConstraint": "edgy.core.db.datastructures:UniqueConstraint",
"Index": "edgy.core.db.datastructures:Index",
},
deprecated_lazy_imports={
"Migrate": {
"path": "edgy.cli.base:Migrate",
"reason": "Use the monkay based system instead.",
"new_attribute": "Instance",
},
"EdgyExtra": {
"path": "edgy.cli.base:Migrate",
"reason": "Use the monkay based system instead.",
"new_attribute": "Instance",
},
},
skip_all_update=True,
)
for name in [
"CASCADE",
"RESTRICT",
"DO_NOTHING",
"SET_NULL",
"SET_DEFAULT",
"PROTECT",
"ConditionalRedirect",
]:
monkay.add_lazy_import(name, f"edgy.core.db.constants.{name}")

for name in __all__:
if name.endswith("Field") or name in {
"OneToOne",
"ManyToMany",
"ForeignKey",
"RefForeignKey",
}:
monkay.add_lazy_import(name, f"edgy.core.db.fields.{name}")

del name
del create_monkay


def get_migration_prepared_registry() -> Registry:
Expand Down
85 changes: 85 additions & 0 deletions edgy/_monkay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from __future__ import annotations

import os
from importlib import import_module
from typing import TYPE_CHECKING, Any, NamedTuple, Optional

from monkay import Monkay

if TYPE_CHECKING:
from edgy.conf.global_settings import EdgySettings
from edgy.core.connection import Registry


class Instance(NamedTuple):
registry: Registry
app: Optional[Any] = None


def create_monkay(global_dict: dict, all_var: list[str]) -> Monkay[Instance, EdgySettings]:
monkay: Monkay[Instance, EdgySettings] = Monkay(
global_dict,
with_extensions=True,
with_instance=True,
# must be at least an empty string to initialize the settings
settings_path=os.environ.get(
"EDGY_SETTINGS_MODULE", "edgy.conf.global_settings.EdgySettings"
),
settings_extensions_name="extensions",
settings_preloads_name="preloads",
uncached_imports={"settings"},
lazy_imports={
"settings": lambda: monkay.settings,
"EdgySettings": "edgy.conf.global_settings:EdgySettings",
"fields": lambda: import_module("edgy.core.db.fields"),
"files": lambda: import_module("edgy.core.files"),
"Signal": "edgy.core.signals:Signal",
"MultipleObjectsReturned": "edgy.exceptions:MultipleObjectsReturned",
"ObjectNotFound": "edgy.exceptions:ObjectNotFound",
"UniqueConstraint": "edgy.core.db.datastructures:UniqueConstraint",
"Index": "edgy.core.db.datastructures:Index",
},
deprecated_lazy_imports={
"Migrate": {
"path": "edgy.cli.base:Migrate",
"reason": "Use the monkay based system instead.",
"new_attribute": "Instance",
},
"EdgyExtra": {
"path": "edgy.cli.base:Migrate",
"reason": "Use the monkay based system instead.",
"new_attribute": "Instance",
},
},
skip_all_update=True,
)
for name in [
"CASCADE",
"RESTRICT",
"DO_NOTHING",
"SET_NULL",
"SET_DEFAULT",
"PROTECT",
"ConditionalRedirect",
]:
monkay.add_lazy_import(name, f"edgy.core.db.constants.{name}")

for name in ["Database", "DatabaseURL", "Registry"]:
monkay.add_lazy_import(name, f"edgy.core.connection.{name}")

for name in ["Prefetch", "Q", "QuerySet", "and_", "not_", "or_"]:
monkay.add_lazy_import(name, f"edgy.core.db.querysets.{name}")

for name in ["Manager", "Model", "ModelRef", "RedirectManager", "ReflectModel", "StrictModel"]:
monkay.add_lazy_import(name, f"edgy.core.db.models.{name}")

for name in all_var:
if name.endswith("Field") or name in {
"OneToOne",
"ManyToMany",
"ForeignKey",
"RefForeignKey",
}:
monkay.add_lazy_import(name, f"edgy.core.db.fields.{name}")

return monkay
10 changes: 9 additions & 1 deletion edgy/conf/enums.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from enum import Enum
from warnings import warn

warn(
"This module is deprecated. Use `esmerald.conf.EnvironmentType` instead when using Esmerald "
"or define otherwise your own EnvironmentType.",
DeprecationWarning,
stacklevel=2,
)


class EnvironmentType(str, Enum):
"""An Enum for HTTP methods."""
"""An Enum for environments."""

DEVELOPMENT = "development"
TESTING = "testing"
Expand Down
Loading

0 comments on commit 57bf99f

Please sign in to comment.