-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] - Allow importing the object from string in the factory (#179)
- Loading branch information
Showing
9 changed files
with
247 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from esmerald import Factory, Include, Inject | ||
|
||
route_patterns = [ | ||
Include( | ||
"/api/v1", | ||
routes=[ | ||
Include("/accounts", namespace="accounts.v1.urls"), | ||
Include("/articles", namespace="articles.v1.urls"), | ||
Include("/posts", namespace="posts.v1.urls"), | ||
], | ||
interceptors=[LoggingInterceptor], # Custom interceptor | ||
dependencies={ | ||
"user_dao": Inject(Factory("myapp.accounts.daos.UserDAO")), | ||
"article_dao": Inject(Factory("myapp.articles.daos.ArticleDAO")), | ||
"post_dao": Inject(Factory("myapp.posts.daos.PostDAO")), | ||
}, | ||
) | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Functions to use with the Factory dependency injection. | ||
""" | ||
from typing import Any, Callable, Tuple, cast | ||
|
||
from esmerald.exceptions import ImproperlyConfigured | ||
from esmerald.utils.module_loading import import_string | ||
|
||
|
||
def _lookup(klass: Any, comp: Any, import_path: Any) -> Any: | ||
""" | ||
Runs a lookup via __import__ and returns the component. | ||
""" | ||
try: | ||
return getattr(klass, comp) | ||
except AttributeError: | ||
__import__(import_path) | ||
return getattr(klass, comp) | ||
|
||
|
||
def _importer(target: Any, attribute: Any) -> Any: | ||
""" | ||
Gets the attribute from the target. | ||
""" | ||
components = target.split(".") | ||
import_path = components.pop(0) | ||
klass = __import__(import_path) | ||
|
||
for comp in components: | ||
import_path += ".%s" % comp | ||
klass = _lookup(klass, comp, import_path) | ||
return getattr(klass, attribute) | ||
|
||
|
||
def _get_provider_callable(target: str) -> Any: | ||
try: | ||
target, attribute = target.rsplit(".", 1) | ||
except (TypeError, ValueError, AttributeError): | ||
raise TypeError(f"Need a valid target to lookup. You supplied: {target!r}") from None | ||
|
||
def getter() -> Any: | ||
return _importer(target, attribute) | ||
|
||
return getter | ||
|
||
|
||
def load_provider(provider: str) -> Tuple[Callable, bool]: | ||
""" | ||
Loads any callable by string import. This will make | ||
sure that there is no need to have all the imports in one | ||
file to use the `esmerald.injector.Factory`. | ||
Example: | ||
# myapp.daos.py | ||
from esmerald import AsyncDAOProtocol | ||
class MyDAO(AsyncDAOProtocol): | ||
... | ||
# myapp.urls.py | ||
from esmerald import Inject, Factory, Gateway | ||
route_patterns = [ | ||
Gateway( | ||
..., | ||
dependencies={"my_dao": Inject(Factory("myapp.daos.MyDAO"))} | ||
) | ||
] | ||
""" | ||
if not isinstance(provider, str): | ||
raise ImproperlyConfigured( | ||
"The `provider` should be a string with the format <module>.<file>" | ||
) | ||
|
||
is_nested: bool = False | ||
try: | ||
provider_callable = import_string(provider) | ||
except ModuleNotFoundError: | ||
target = _get_provider_callable(provider) | ||
provider_callable = target | ||
is_nested = True | ||
|
||
if not callable(provider_callable): | ||
raise ImproperlyConfigured( | ||
f"The `provider` specified must be a callable, got {type(provider_callable)} instead." | ||
) | ||
|
||
return cast(Callable, provider_callable), is_nested |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters