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

Migrated to Asphalt 5 #48

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated code and tests to match the new resource API
agronholm committed Mar 26, 2024

Verified

This commit was signed with the committer’s verified signature.
neo1973 Markus Härer
commit e16c4b0448c8fb1cbdbe3ba7e4579086a123369a
4 changes: 2 additions & 2 deletions examples/attachment.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

# isort: off
import click
from asphalt.core import CLIApplicationComponent, require_resource, run_application
from asphalt.core import CLIApplicationComponent, get_resource_nowait, run_application
from asphalt.mailer import Mailer


@@ -46,7 +46,7 @@ async def start(self) -> None:
await super().start()

async def run(self) -> None:
mailer = require_resource(Mailer) # type: ignore[type-abstract]
mailer = get_resource_nowait(Mailer) # type: ignore[type-abstract]
message = mailer.create_message(
subject=self.subject,
sender=self.sender,
4 changes: 2 additions & 2 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@

# isort: off
import click
from asphalt.core import CLIApplicationComponent, require_resource, run_application
from asphalt.core import CLIApplicationComponent, get_resource_nowait, run_application
from asphalt.mailer import Mailer


@@ -45,7 +45,7 @@ async def start(self) -> None:
await super().start()

async def run(self) -> None:
mailer = require_resource(Mailer) # type: ignore[type-abstract]
mailer = get_resource_nowait(Mailer) # type: ignore[type-abstract]
await mailer.create_and_deliver(
subject=self.subject, sender=self.sender, to=self.to, plain_body=self.body
)
6 changes: 3 additions & 3 deletions src/asphalt/mailer/mailers/smtp.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
from typing import Any

from aiosmtplib import SMTP, SMTPTimeoutError
from asphalt.core import current_context, require_resource
from asphalt.core import add_teardown_callback, get_resource_nowait

from .._api import DeliveryError, Mailer

@@ -63,7 +63,7 @@ def __init__(

async def start(self) -> None:
if isinstance(self.tls_context, str):
tls_context: SSLContext | None = require_resource(
tls_context: SSLContext | None = get_resource_nowait(
SSLContext, self.tls_context
)
else:
@@ -75,7 +75,7 @@ async def start(self) -> None:
tls_context=tls_context,
timeout=self.timeout,
)
current_context().add_teardown_callback(self._smtp.close)
add_teardown_callback(self._smtp.close)

async def deliver(self, messages: EmailMessage | Iterable[EmailMessage]) -> None:
if isinstance(messages, EmailMessage):
8 changes: 4 additions & 4 deletions tests/mailers/test_smtp.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
import pytest
from aiosmtpd.handlers import Message as AIOSMTPMessage
from aiosmtpd.smtp import SMTP, AuthResult, Envelope, Session
from asphalt.core import Context
from asphalt.core import Context, add_resource

from asphalt.mailer import DeliveryError
from asphalt.mailer.mailers.smtp import SMTPMailer
@@ -73,11 +73,11 @@ def test_port_selection(

async def test_resources() -> None:
mailer = SMTPMailer(tls_context="contextresource")
async with Context() as ctx:
async with Context():
sslcontext = ssl.create_default_context()
ctx.add_resource(sslcontext, "contextresource")
add_resource(sslcontext, "contextresource")
await mailer.start()
assert mailer.tls_context is sslcontext
assert mailer._smtp.tls_context is sslcontext


async def test_deliver(
4 changes: 2 additions & 2 deletions tests/test_component.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import logging

import pytest
from asphalt.core import Context, qualified_name, require_resource
from asphalt.core import Context, get_resource_nowait, qualified_name
from pytest import LogCaptureFixture

from asphalt.mailer import Mailer, MailerComponent
@@ -17,7 +17,7 @@ async def test_component(caplog: LogCaptureFixture, backend: str) -> None:
component = MailerComponent(backend=backend)
async with Context():
await component.start()
mailer = require_resource(Mailer) # type: ignore[type-abstract]
mailer = get_resource_nowait(Mailer) # type: ignore[type-abstract]

records = [record for record in caplog.records if record.name == "asphalt.mailer"]
records.sort(key=lambda r: r.message)