Coverage for packages/dqm-ml-core/src/dqm_ml_core/utils/registry.py: 99%

51 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-02 17:15 +0000

1"""Plugin registry for dynamically loading DQM components. 

2 

3This module contains the PluginLoadedRegistry class and load_registered_plugins 

4function for discovering and loading metric processors, data loaders, 

5and output writers via Python entry points. 

6""" 

7 

8from __future__ import annotations 

9 

10from importlib.metadata import EntryPoints, entry_points 

11import logging 

12from typing import Any 

13 

14from dqm_ml_core.api.processor import Processor 

15 

16logger = logging.getLogger(__name__) 

17 

18 

19# TODO once a base class for all registry created, dict shall have dict[str, base_class] 

20def load_registered_plugins(plugin_group: str, base_class: Any, base_name: str = "default") -> dict[str, Any]: 

21 """Discover and load plugins registered via Python entry points. 

22 

23 Args: 

24 plugin_group: The entry point group name (e.g., 'dqm_ml.metrics'). 

25 base_class: Optional base class to verify plugin type safety. 

26 base_name: Name of the base class to ignore during discovery. 

27 

28 Returns: 

29 A dictionary mapping plugin names to their loaded classes. 

30 """ 

31 plugin_entry_points: EntryPoints = entry_points(group=plugin_group) 

32 

33 registry = {} 

34 for v in plugin_entry_points: 

35 # Filter base class registry (not callable) 

36 if v.name != base_name: 36 ↛ 34line 36 didn't jump to line 34 because the condition on line 36 was always true

37 obj = v.load() 

38 if base_class is None or issubclass(obj, base_class): 

39 logger.debug(f"Referencing {plugin_group} - {v.name} class {obj} from {base_class}") 

40 registry[v.name] = obj 

41 else: 

42 logger.error(f"Entry point {plugin_group} - {v.name} class {obj} not derived from {base_class} ignored") 

43 

44 # return a dict to class builder registry 

45 return registry 

46 

47 

48class PluginLoadedRegistry: 

49 """ 

50 Singleton registry that provides lazy access to all registered DQM components. 

51 

52 Components include: 

53 - Metrics (Processor) 

54 - DataLoaders 

55 - OutputWriters 

56 """ 

57 

58 _metrics_registry: dict[str, type[Processor]] | None = None 

59 _features_registry: dict[str, type[Processor]] | None = None 

60 _gap_registry: dict[str, type[Processor]] | None = None 

61 _dataloaders_registry: dict[str, Any] | None = None 

62 _outputwriter_registry: dict[str, Any] | None = None 

63 

64 @classmethod 

65 def get_metrics_registry(cls) -> dict[str, type[Processor]]: 

66 """Return the registry of available metric processors. 

67 

68 Returns: 

69 A dictionary mapping metric processor names to their classes. 

70 """ 

71 if not cls._metrics_registry: 

72 from dqm_ml_core.api.metrics_processor import MetricsProcessor 

73 

74 cls._metrics_registry = load_registered_plugins("dqm_ml.metrics", MetricsProcessor) 

75 

76 return cls._metrics_registry 

77 

78 @classmethod 

79 def get_features_registry(cls) -> dict[str, type[Processor]]: 

80 """Return the registry of available feature extraction processors. 

81 

82 Returns: 

83 A dictionary mapping feature processor names to their classes. 

84 """ 

85 if not cls._features_registry: 

86 from dqm_ml_core.api.features_processor import FeaturesProcessor 

87 

88 cls._features_registry = load_registered_plugins("dqm_ml.features", FeaturesProcessor) 

89 

90 return cls._features_registry 

91 

92 @classmethod 

93 def get_gap_registry(cls) -> dict[str, type[Processor]]: 

94 """Return the registry of available gap processors. 

95 

96 Returns: 

97 A dictionary mapping gap processor names to their classes. 

98 """ 

99 if not cls._gap_registry: 

100 from dqm_ml_core.api.gap_processor import GapProcessor 

101 

102 cls._gap_registry = load_registered_plugins("dqm_ml.gap", GapProcessor) 

103 

104 return cls._gap_registry 

105 

106 @classmethod 

107 def get_dataloaders_registry(cls) -> dict[str, Any]: 

108 """Return the registry of available data loaders. 

109 

110 Returns: 

111 A dictionary mapping data loader names to their classes. 

112 """ 

113 if not cls._dataloaders_registry: 

114 cls._dataloaders_registry = load_registered_plugins("dqm_ml.dataloaders", None) # TODO add base class 

115 return cls._dataloaders_registry 

116 

117 @classmethod 

118 def get_outputwriter_registry(cls) -> dict[str, Any]: 

119 """Return the registry of available output writers. 

120 

121 Returns: 

122 A dictionary mapping output writer names to their classes. 

123 """ 

124 if not cls._outputwriter_registry: 

125 cls._outputwriter_registry = load_registered_plugins("dqm_ml.outputwriter", None) # TODO add base class 

126 

127 return cls._outputwriter_registry