Coverage for src/kwai/cli/dependencies.py: 0%

27 statements  

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

1"""Module that defines dependencies for the CLI program.""" 

2 

3import contextlib 

4 

5from typing import AsyncGenerator 

6 

7import inject 

8 

9from inject import Binder 

10from redis.asyncio import Redis 

11 

12from kwai.core.db.database import Database 

13from kwai.core.settings import Settings, get_settings 

14 

15 

16@contextlib.asynccontextmanager 

17@inject.autoparams() 

18async def create_database(settings: Settings) -> AsyncGenerator[Database, None]: 

19 """Create the database dependency.""" 

20 database = Database(settings.db) 

21 try: 

22 yield database 

23 finally: 

24 await database.close() 

25 

26 

27@contextlib.asynccontextmanager 

28@inject.autoparams() 

29async def create_redis(settings: Settings) -> AsyncGenerator[Redis, None]: 

30 """Create the Redis dependency.""" 

31 redis = Redis( 

32 host=settings.redis.host, 

33 port=settings.redis.port, 

34 password=settings.redis.password, 

35 ) 

36 try: 

37 yield redis 

38 finally: 

39 await redis.aclose() 

40 

41 

42def _configure_dependencies(binder: Binder): 

43 binder.bind_to_provider(Settings, get_settings) 

44 binder.bind_to_provider(Database, create_database) 

45 binder.bind_to_provider(Redis, create_redis) 

46 

47 

48def configure(): 

49 """Configure the dependency injection system for the CLI program.""" 

50 inject.configure(_configure_dependencies)