Coverage for src/qdrant_loader/core/conversion/formats.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-20 10:15 +0000

1"""Conversion eligibility policy, derived from docling's own format tables. 

2 

3:class:`FormatPolicy` is the single source of truth for "should we convert this?". 

4It filters docling's ``FormatToMimeType`` table by our enabled-formats list plus a 

5size cap, instead of duplicating a hand-maintained MIME dict (two sources of truth 

6that drift). Like the option builder, it is a small pure class holding the injected 

7config and imports docling lazily. 

8""" 

9 

10from __future__ import annotations 

11 

12from typing import TYPE_CHECKING 

13 

14from .config import ConversionConfig 

15 

16if TYPE_CHECKING: 

17 from docling.datamodel.base_models import InputFormat 

18 

19 

20class FormatPolicy: 

21 """Decides which inputs are eligible for conversion under the config.""" 

22 

23 def __init__(self, config: ConversionConfig) -> None: 

24 self._config = config 

25 

26 def allowed_formats(self) -> list[InputFormat]: 

27 """The enabled formats, resolved to docling ``InputFormat`` members.""" 

28 from docling.datamodel.base_models import InputFormat 

29 

30 formats_by_name = {fmt.value: fmt for fmt in InputFormat} 

31 unknown = [ 

32 name for name in self._config.enabled_formats if name not in formats_by_name 

33 ] 

34 if unknown: 

35 raise ValueError( 

36 f"Unknown conversion format(s) {unknown!r}; valid formats are " 

37 f"{sorted(formats_by_name)}." 

38 ) 

39 return [formats_by_name[name] for name in self._config.enabled_formats] 

40 

41 def supported_mime_types(self) -> set[str]: 

42 """Accepted MIME types = docling's table filtered to the enabled formats.""" 

43 from docling.datamodel.base_models import FormatToMimeType 

44 

45 allowed = set(self.allowed_formats()) 

46 return { 

47 mime_type 

48 for fmt, mime_types in FormatToMimeType.items() 

49 if fmt in allowed 

50 for mime_type in mime_types 

51 } 

52 

53 def is_within_size_limit(self, size_bytes: int) -> bool: 

54 return size_bytes <= self._config.max_file_size