Coverage for fastblocks / adapters / images / _base.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

1"""Base classes and protocols for image adapters.""" 

2 

3from typing import Any, Protocol 

4from uuid import UUID 

5 

6# Oneiric imports 

7from oneiric.core.config import OneiricSettings 

8from oneiric.core.resolution import Resolver 

9 

10from ..oneiric_helper import register_candidate 

11 

12# Oneiric resolver for dependency injection 

13depends = Resolver() 

14 

15 

16class ImagesBaseSettings(OneiricSettings): 

17 """Base settings for image adapters using OneiricSettings.""" 

18 

19 cdn_url: str | None = None 

20 media_bucket: str = "media" 

21 default_transformations: dict[str, Any] = {} 

22 lazy_loading: bool = True 

23 

24 def __init__(self, **data: dict[str, Any]) -> None: 

25 super().__init__(**data) 

26 

27 

28class ImagesProtocol(Protocol): 

29 """Protocol for image adapter implementations.""" 

30 

31 async def upload_image(self, file_data: bytes, filename: str) -> str: ... 

32 async def get_image_url( 

33 self, image_id: str, transformations: dict[str, Any] | None = None 

34 ) -> str: ... 

35 def get_img_tag(self, image_id: str, alt: str, **attributes: Any) -> str: ... 

36 

37 

38class ImagesBase: 

39 """Base class for image adapters using Oneiric patterns.""" 

40 

41 # Oneiric-compatible metadata 

42 MODULE_ID: UUID = UUID("01937d86-4f2a-7b3c-8d9e-f3b4d3c2b1a1") # Static UUID7 

43 MODULE_STATUS = "stable" 

44 

45 def __init__(self) -> None: 

46 """Initialize image adapter.""" 

47 # Register with Oneiric resolver 

48 register_candidate( 

49 depends, 

50 domain="fastblocks", 

51 key="images", 

52 factory=lambda: self, 

53 metadata={ 

54 "class": self.__class__.__name__, 

55 "module": self.__class__.__module__, 

56 }, 

57 ) 

58 

59 async def upload_image(self, file_data: bytes, filename: str) -> str: 

60 """Upload image and return image ID.""" 

61 raise NotImplementedError() 

62 

63 async def get_image_url( 

64 self, image_id: str, transformations: dict[str, Any] | None = None 

65 ) -> str: 

66 """Generate image URL with optional transformations.""" 

67 raise NotImplementedError() 

68 

69 def get_img_tag(self, image_id: str, alt: str, **attributes: Any) -> str: 

70 """Generate complete img tag with attributes.""" 

71 raise NotImplementedError()