Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- check always against the ExtensionProtocol
- update tests and docs
  • Loading branch information
devkral committed Nov 8, 2024
1 parent 33c815e commit 96b9bc5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
13 changes: 6 additions & 7 deletions docs/en/docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,29 @@ There are two variants how to do it:
```

You can use for the late registration the methods `add_extension`.
It will automatically initialize and call extend for you when passing a class or **Pluggable**,
**but not when passing an instance**.

### Standalone object

But, what if I don't want to use the [Extension](#extension) object for my pluggable? Is this
possible?

Short answer, yes, but this comes with limitations:

* You **cannot** hook the class within a [Pluggable](#pluggable) and use the automated way.
* You **will always need** to start it manually.
´
yes, it must only implement the ExtensionProtocol.

```python hl_lines="9 25 42-43"
{!> ../../../docs_src/pluggables/standalone.py !}
```

## Important notes

As you can see, **pluggables** in Esmerald can be a powerful tool that isolates common
As you can see, **extensions** in Esmerald can be a powerful tool that isolates common
functionality from the main Esmerald application and can be used to leverage the creation of plugins
to be used across your applications and/or to create opensource packages for any need.

## ChildEsmerald and pluggables

A [Pluggable](#pluggable) **is not the same** as a [ChildEsmerald](./routing/router.md#child-esmerald-application).
An [Extension](#extension) **is not the same** as a [ChildEsmerald](./routing/router.md#child-esmerald-application).

These are two completely independent pieces of functionality with completely different purposes, be
careful when considering one and the other.
Expand Down
5 changes: 1 addition & 4 deletions docs_src/pluggables/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,4 @@ async def home(request: Request) -> JSONResponse:
return JSONResponse({"extensions": extensions})


app = Esmerald(routes=[Gateway(handler=home)])

extension = Standalone(app=app)
extension.extend()
app = Esmerald(routes=[Gateway(handler=home)], extensions=[Standalone])
5 changes: 2 additions & 3 deletions esmerald/pluggables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from esmerald.exceptions import ImproperlyConfigured
from esmerald.protocols.extension import ExtensionProtocol
from esmerald.utils.helpers import is_class_and_subclass

if TYPE_CHECKING: # pragma: no cover
from esmerald.applications import Esmerald
Expand Down Expand Up @@ -177,12 +176,12 @@ def __setitem__(self, name: Any, value: Any) -> None:
value.extend(**options)
else:
self.delayed_extend[name] = options
elif isinstance(value, ExtensionProtocol) and not isclass(value):
elif not isclass(value) and isinstance(value, ExtensionProtocol):
if self.delayed_extend is not None:
raise ImproperlyConfigured(
"Cannot pass an initialized extension in extensions parameter."
)
elif is_class_and_subclass(value, Extension):
elif isclass(value) and issubclass(value, ExtensionProtocol):
value = value(app=self.app)
if self.delayed_extend is None:
value.extend()
Expand Down
11 changes: 10 additions & 1 deletion tests/pluggables/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def __init__(self, app: "Esmerald"):
def extend(self) -> None:
container.append("works")

class NonExtension:
def __init__(self, app: "Esmerald"):
super().__init__()
self.app = app

def extend(self) -> None:
pass

class LoggingExtension(Extension):
def __init__(self, app: "Esmerald", name):
super().__init__(app)
Expand Down Expand Up @@ -101,10 +109,11 @@ def extend(self, database) -> None:
"logging": Pluggable(LoggingExtension, name="my logging"),
"database": Pluggable(DatabaseExtension, database="my db"),
"base": ReorderedExtension,
"non-extension": NonExtension,
},
)

assert len(app.extensions.keys()) == 4
assert len(app.extensions.keys()) == 5


def test_start_extension_directly(test_client_factory):
Expand Down

0 comments on commit 96b9bc5

Please sign in to comment.