Coverage for python/lsst/daf/butler/registry/_registry_factory.py: 42%
25 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-12 09:20 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-12 09:20 +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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = ("_RegistryFactory",)
26from typing import TYPE_CHECKING
28from lsst.resources import ResourcePathExpression
29from lsst.utils import doImportType
31from ..core import Config, DimensionConfig
32from ._butler_registry import _ButlerRegistry
33from ._config import RegistryConfig
34from ._defaults import RegistryDefaults
36if TYPE_CHECKING:
37 from .._butlerConfig import ButlerConfig
40class _RegistryFactory:
41 """Interface for creating and initializing Registry instances.
43 Parameters
44 ----------
45 config : `RegistryConfig` or `str`, optional
46 Registry configuration, if missing then default configuration will
47 be loaded from registry.yaml.
49 Notes
50 -----
51 Each registry implementation can have its own constructor parameters.
52 The assumption is that an instance of a specific subclass will be
53 constructed from configuration using ``RegistryClass.fromConfig()`` or
54 ``RegistryClass.createFromConfig()``.
56 This class will look for a ``cls`` entry in registry configuration object
57 (defaulting to ``SqlRegistry``), import that class, and call one of the
58 above methods on the imported class.
59 """
61 def __init__(self, config: ButlerConfig | RegistryConfig | Config | str | None):
62 if not isinstance(config, RegistryConfig):
63 if isinstance(config, str | Config) or config is None:
64 config = RegistryConfig(config)
65 else:
66 raise ValueError(f"Incompatible Registry configuration: {config}")
67 self._config = config
69 # Default to the standard registry
70 registry_cls_name = config.get("cls", "lsst.daf.butler.registries.sql.SqlRegistry")
71 registry_cls = doImportType(registry_cls_name)
72 if not issubclass(registry_cls, _ButlerRegistry):
73 raise TypeError(
74 f"Registry class obtained from config {registry_cls_name} is not a _ButlerRegistry class."
75 )
76 self._registry_cls = registry_cls
78 def create_from_config(
79 self,
80 dimensionConfig: DimensionConfig | str | None = None,
81 butlerRoot: ResourcePathExpression | None = None,
82 ) -> _ButlerRegistry:
83 """Create registry database and return `_ButlerRegistry` instance.
85 This method initializes database contents, database must be empty
86 prior to calling this method.
88 Parameters
89 ----------
90 dimensionConfig : `DimensionConfig` or `str`, optional
91 Dimensions configuration, if missing then default configuration
92 will be loaded from dimensions.yaml.
93 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
94 Path to the repository root this `Registry` will manage.
96 Returns
97 -------
98 registry : `_ButlerRegistry`
99 A new `_ButlerRegistry` instance.
100 """
101 return self._registry_cls.createFromConfig(self._config, dimensionConfig, butlerRoot)
103 def from_config(
104 self,
105 butlerRoot: ResourcePathExpression | None = None,
106 writeable: bool = True,
107 defaults: RegistryDefaults | None = None,
108 ) -> _ButlerRegistry:
109 """Create `_ButlerRegistry` subclass instance from ``config``.
111 Registry database must be initialized prior to calling this method.
113 Parameters
114 ----------
115 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
116 Path to the repository root this `Registry` will manage.
117 writeable : `bool`, optional
118 If `True` (default) create a read-write connection to the database.
119 defaults : `~lsst.daf.butler.registry.RegistryDefaults`, optional
120 Default collection search path and/or output `~CollectionType.RUN`
121 collection.
123 Returns
124 -------
125 registry : `_ButlerRegistry` (subclass)
126 A new `_ButlerRegistry` subclass instance.
127 """
128 return self._registry_cls.fromConfig(self._config, butlerRoot, writeable, defaults)