Coverage for python/lsst/daf/butler/registry/_registry_factory.py: 44%

26 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-12 09:44 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30__all__ = ("_RegistryFactory",) 

31 

32from typing import TYPE_CHECKING 

33 

34from lsst.resources import ResourcePathExpression 

35from lsst.utils import doImportType 

36 

37from .._config import Config 

38from ..dimensions import DimensionConfig 

39from ._butler_registry import _ButlerRegistry 

40from ._config import RegistryConfig 

41from ._defaults import RegistryDefaults 

42 

43if TYPE_CHECKING: 

44 from .._butler_config import ButlerConfig 

45 

46 

47class _RegistryFactory: 

48 """Interface for creating and initializing Registry instances. 

49 

50 Parameters 

51 ---------- 

52 config : `RegistryConfig` or `str`, optional 

53 Registry configuration, if missing then default configuration will 

54 be loaded from registry.yaml. 

55 

56 Notes 

57 ----- 

58 Each registry implementation can have its own constructor parameters. 

59 The assumption is that an instance of a specific subclass will be 

60 constructed from configuration using ``RegistryClass.fromConfig()`` or 

61 ``RegistryClass.createFromConfig()``. 

62 

63 This class will look for a ``cls`` entry in registry configuration object 

64 (defaulting to ``SqlRegistry``), import that class, and call one of the 

65 above methods on the imported class. 

66 """ 

67 

68 def __init__(self, config: ButlerConfig | RegistryConfig | Config | str | None): 

69 if not isinstance(config, RegistryConfig): 

70 if isinstance(config, str | Config) or config is None: 

71 config = RegistryConfig(config) 

72 else: 

73 raise ValueError(f"Incompatible Registry configuration: {config}") 

74 self._config = config 

75 

76 # Default to the standard registry 

77 registry_cls_name = config.get("cls", "lsst.daf.butler.registries.sql.SqlRegistry") 

78 registry_cls = doImportType(registry_cls_name) 

79 if not issubclass(registry_cls, _ButlerRegistry): 

80 raise TypeError( 

81 f"Registry class obtained from config {registry_cls_name} is not a _ButlerRegistry class." 

82 ) 

83 self._registry_cls = registry_cls 

84 

85 def create_from_config( 

86 self, 

87 dimensionConfig: DimensionConfig | str | None = None, 

88 butlerRoot: ResourcePathExpression | None = None, 

89 ) -> _ButlerRegistry: 

90 """Create registry database and return `_ButlerRegistry` instance. 

91 

92 This method initializes database contents, database must be empty 

93 prior to calling this method. 

94 

95 Parameters 

96 ---------- 

97 dimensionConfig : `DimensionConfig` or `str`, optional 

98 Dimensions configuration, if missing then default configuration 

99 will be loaded from dimensions.yaml. 

100 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional 

101 Path to the repository root this `Registry` will manage. 

102 

103 Returns 

104 ------- 

105 registry : `_ButlerRegistry` 

106 A new `_ButlerRegistry` instance. 

107 """ 

108 return self._registry_cls.createFromConfig(self._config, dimensionConfig, butlerRoot) 

109 

110 def from_config( 

111 self, 

112 butlerRoot: ResourcePathExpression | None = None, 

113 writeable: bool = True, 

114 defaults: RegistryDefaults | None = None, 

115 ) -> _ButlerRegistry: 

116 """Create `_ButlerRegistry` subclass instance from ``config``. 

117 

118 Registry database must be initialized prior to calling this method. 

119 

120 Parameters 

121 ---------- 

122 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional 

123 Path to the repository root this `Registry` will manage. 

124 writeable : `bool`, optional 

125 If `True` (default) create a read-write connection to the database. 

126 defaults : `~lsst.daf.butler.registry.RegistryDefaults`, optional 

127 Default collection search path and/or output `~CollectionType.RUN` 

128 collection. 

129 

130 Returns 

131 ------- 

132 registry : `_ButlerRegistry` (subclass) 

133 A new `_ButlerRegistry` subclass instance. 

134 """ 

135 return self._registry_cls.fromConfig(self._config, butlerRoot, writeable, defaults)