Coverage for src/kwai/core/events/dependencies.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines the dependencies for events.""" 

2 

3from typing import AsyncGenerator 

4 

5from fast_depends import Depends 

6 

7from kwai.core.db.database import Database 

8from kwai.core.mail.mailer import Mailer 

9from kwai.core.mail.smtp_mailer import SmtpMailer 

10from kwai.core.settings import Settings, get_settings 

11from kwai.core.template.jinja2_engine import Jinja2Engine 

12from kwai.core.template.template_engine import TemplateEngine 

13 

14 

15async def create_database( 

16 settings: Settings = Depends(get_settings), 

17) -> AsyncGenerator[Database, None]: 

18 """Create the database dependency.""" 

19 database = Database(settings.db) 

20 try: 

21 yield database 

22 finally: 

23 await database.close() 

24 

25 

26def create_template_engine( 

27 settings: Settings = Depends(get_settings), 

28) -> TemplateEngine: 

29 """Create the template engine dependency.""" 

30 return Jinja2Engine(website=settings.website) 

31 

32 

33def create_mailer(settings: Settings = Depends(get_settings)) -> Mailer: 

34 """Create the mailer dependency.""" 

35 mailer = SmtpMailer( 

36 host=settings.email.host, 

37 port=settings.email.port, 

38 ssl_=settings.email.ssl, 

39 tls=settings.email.tls, 

40 ) 

41 mailer.connect() 

42 if settings.email.user: 

43 mailer.login(settings.email.user, settings.email.password) 

44 return mailer