Coverage for /home/benjarobin/Bootlin/projects/Schneider-Electric-Senux/sbom-cve-check/src/sbom_cve_check/export/registry.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.11.1, created at 2025-11-28 15:37 +0100

1# -*- coding: utf-8 -*- 

2# SPDX-License-Identifier: GPL-2.0-only 

3 

4import pathlib 

5from collections.abc import Callable, Iterable 

6from typing import TypeVar 

7 

8from ..sbom.sbom_base import Sbom 

9from ..utils.class_utils import Singleton 

10from ..utils.plugin import import_builtin_plugin 

11from .export_base import BaseExport 

12 

13_ExportT = TypeVar("_ExportT", bound=BaseExport) 

14 

15 

16class ExportTypeRegistry(metaclass=Singleton): 

17 def __init__(self) -> None: 

18 self._export_types: dict[str, type[BaseExport]] = {} 

19 

20 def register_type(self, type_name: str, export_type: type[BaseExport]) -> None: 

21 assert type_name not in self._export_types 

22 self._export_types[type_name] = export_type 

23 

24 @property 

25 def type_names(self) -> Iterable[str]: 

26 return self._export_types.keys() 

27 

28 def create( 

29 self, 

30 type_name: str, 

31 sbom: Sbom, 

32 out_path: pathlib.Path, 

33 ) -> BaseExport: 

34 cls_export = self._export_types[type_name] 

35 return cls_export(sbom, out_path) 

36 

37 

38def register_export(name: str) -> Callable[[type[_ExportT]], type[_ExportT]]: 

39 def decorator(cls: type[_ExportT]) -> type[_ExportT]: 

40 ExportTypeRegistry().register_type(name, cls) 

41 return cls 

42 

43 return decorator 

44 

45 

46def __register_builtin_types() -> None: 

47 modules = [".export_csv", ".export_spdx3", ".export_yocto"] 

48 for mod in modules: 

49 import_builtin_plugin(__package__, mod) 

50 

51 

52__register_builtin_types()