Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flexibility in Extend declaration #22

Merged
merged 6 commits into from Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion sanic_ext/__init__.py
Expand Up @@ -5,7 +5,7 @@
from sanic_ext.extras.serializer.decorator import serializer
from sanic_ext.extras.validation.decorator import validate

__version__ = "21.9.3"
__version__ = "21.12.0"
__all__ = [
"Config",
"Extend",
Expand Down
14 changes: 9 additions & 5 deletions sanic_ext/bootstrap.py
Expand Up @@ -53,16 +53,16 @@ def __init__(
)

self.app = app
self.extensions = []
self._injection_registry: Optional[InjectionRegistry] = None
app.ctx.ext = self
app._ext = self
app.ctx._dependencies = SimpleNamespace()

if not isinstance(config, Config):
config = Config.from_dict(config or {})
self.config = add_fallback_config(app, config, **kwargs)

if not extensions:
extensions = []
extensions = extensions or []
if built_in_extensions:
extensions.extend(
[
Expand All @@ -71,10 +71,14 @@ def __init__(
HTTPExtension,
]
)
init_logs = ["Sanic Extensions:"]
for extclass in extensions[::-1]:
extension = extclass(app, self.config)
extension._startup(self)
self.extensions.append(extension)

def _display(self):
init_logs = ["Sanic Extensions:"]
for extension in self.extensions:
init_logs.append(f" > {extension.name} {extension.label()}")

list(map(logger.info, init_logs))
Expand Down Expand Up @@ -108,4 +112,4 @@ def dependency(self, obj: Any, name: Optional[str] = None) -> None:
def getter(*_):
return obj

self.injection(obj.__class__, getter)
self.add_dependency(obj.__class__, getter)
9 changes: 6 additions & 3 deletions sanic_ext/extensions/injection/extension.py
Expand Up @@ -7,6 +7,9 @@ class InjectionExtension(Extension):
name = "injection"

def startup(self, bootstrap) -> None:
registry = InjectionRegistry()
add_injection(self.app, registry)
bootstrap._injection_registry = registry
self.registry = InjectionRegistry()
add_injection(self.app, self.registry)
bootstrap._injection_registry = self.registry

def label(self):
return f"[{self.registry.length}]"
4 changes: 4 additions & 0 deletions sanic_ext/extensions/injection/registry.py
Expand Up @@ -31,6 +31,10 @@ def finalize(self, allowed_types):
if isinstance(constructor, Constructor):
constructor.prepare(self, allowed_types)

@property
def length(self):
return len(self._registry)


class SignatureRegistry:
def __init__(self):
Expand Down
17 changes: 13 additions & 4 deletions sanic_ext/extensions/openapi/extension.py
Expand Up @@ -12,9 +12,18 @@ def startup(self, _) -> None:

def label(self):
if self.app.config.OAS:
name = f"{self.bp.name}.index"

if "SERVER_NAME" in self.app.config:
return f"[{self.app.url_for(name, _external=True)}]"
return self._make_url()

return ""

def _make_url(self):
name = f"{self.bp.name}.index"
_server = (
None
if "SERVER_NAME" in self.app.config
else self.app.serve_location
) or None
_external = bool(_server) or "SERVER_NAME" in self.app.config
return (
f"[{self.app.url_for(name, _external=_external, _server=_server)}]"
)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -14,7 +14,7 @@
dev_requires = ["black>=21.4b2", "flake8>=3.7.7", "isort>=5.0.0"]

test_requires = [
"sanic_testing==0.7.0",
"sanic_testing>=0.8",
"coverage",
"pytest",
"pytest-cov",
Expand Down
18 changes: 9 additions & 9 deletions tests/extensions/injection/test_add_dependency.py
Expand Up @@ -28,7 +28,7 @@ def handler(request, name: Name):
request.ctx.name = name
return text(name.name)

app.ctx.ext.add_dependency(Name)
app.ext.add_dependency(Name)

request, response = app.test_client.get("/person/george")

Expand All @@ -52,7 +52,7 @@ def handler(request, name: Name):
"in v22.6. Please use 'ext.add_dependency' instead."
)
with pytest.warns(DeprecationWarning, match=message):
app.ctx.ext.injection(Name)
app.ext.injection(Name)

request, response = app.test_client.get("/person/george")

Expand All @@ -71,7 +71,7 @@ def handler(request, name: str, person: Person):
request.ctx.person = person
return text(person.name)

app.ctx.ext.add_dependency(Person)
app.ext.add_dependency(Person)

request, response = app.test_client.get("/person/george")

Expand Down Expand Up @@ -103,8 +103,8 @@ async def person_details(request, person_id: PersonID, person: Person):
f"{person.person_id.person_id}\n{person.name}\n{person.age}"
)

app.ctx.ext.add_dependency(Person, Person.create)
app.ctx.ext.add_dependency(PersonID)
app.ext.add_dependency(Person, Person.create)
app.ext.add_dependency(PersonID)

request, response = app.test_client.get("/person/999")

Expand Down Expand Up @@ -132,7 +132,7 @@ async def post(request, name: Name):
request.ctx.name = name
return text(name.name)

app.ctx.ext.add_dependency(Name)
app.ext.add_dependency(Name)

for client in (app.test_client.get, app.test_client.post):
request, response = client("/person/george")
Expand Down Expand Up @@ -169,9 +169,9 @@ def create(cls, request: Request, b: B):
next(counter)
return cls(b)

app.ctx.ext.add_dependency(A, A.create)
app.ctx.ext.add_dependency(B, B.create)
app.ctx.ext.add_dependency(C, C.create)
app.ext.add_dependency(A, A.create)
app.ext.add_dependency(B, B.create)
app.ext.add_dependency(C, C.create)

@app.get("/")
async def nested(request: Request, c: C):
Expand Down
6 changes: 3 additions & 3 deletions tests/extensions/injection/test_dependency.py
Expand Up @@ -10,8 +10,8 @@ def test_dependency_added(app):
foo = Foo()
foobar = Foo()

app.ctx.ext.dependency(foo)
app.ctx.ext.dependency(foobar, name="something")
app.ext.dependency(foo)
app.ext.dependency(foobar, name="something")

assert app.ctx._dependencies.foo is foo
assert app.ctx._dependencies.something is foobar
Expand All @@ -20,7 +20,7 @@ def test_dependency_added(app):
def test_dependency_injection(app):
foo = Foo()

app.ctx.ext.dependency(foo)
app.ext.dependency(foo)

@app.get("/getfoo")
async def getfoo(request: Request, foo: Foo):
Expand Down