Coverage for python/lsst/daf/butler/registry/_registry_factory.py: 42%
25 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 08:00 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 08:00 +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/>.
28from __future__ import annotations
30__all__ = ("_RegistryFactory",)
32from typing import TYPE_CHECKING
34from lsst.resources import ResourcePathExpression
35from lsst.utils import doImportType
37from ..core import Config, DimensionConfig
38from ._butler_registry import _ButlerRegistry
39from ._config import RegistryConfig
40from ._defaults import RegistryDefaults
42if TYPE_CHECKING:
43 from .._butlerConfig import ButlerConfig
46class _RegistryFactory:
47 """Interface for creating and initializing Registry instances.
49 Parameters
50 ----------
51 config : `RegistryConfig` or `str`, optional
52 Registry configuration, if missing then default configuration will
53 be loaded from registry.yaml.
55 Notes
56 -----
57 Each registry implementation can have its own constructor parameters.
58 The assumption is that an instance of a specific subclass will be
59 constructed from configuration using ``RegistryClass.fromConfig()`` or
60 ``RegistryClass.createFromConfig()``.
62 This class will look for a ``cls`` entry in registry configuration object
63 (defaulting to ``SqlRegistry``), import that class, and call one of the
64 above methods on the imported class.
65 """
67 def __init__(self, config: ButlerConfig | RegistryConfig | Config | str | None):
68 if not isinstance(config, RegistryConfig):
69 if isinstance(config, str | Config) or config is None:
70 config = RegistryConfig(config)
71 else:
72 raise ValueError(f"Incompatible Registry configuration: {config}")
73 self._config = config
75 # Default to the standard registry
76 registry_cls_name = config.get("cls", "lsst.daf.butler.registries.sql.SqlRegistry")
77 registry_cls = doImportType(registry_cls_name)
78 if not issubclass(registry_cls, _ButlerRegistry):
79 raise TypeError(
80 f"Registry class obtained from config {registry_cls_name} is not a _ButlerRegistry class."
81 )
82 self._registry_cls = registry_cls
84 def create_from_config(
85 self,
86 dimensionConfig: DimensionConfig | str | None = None,
87 butlerRoot: ResourcePathExpression | None = None,
88 ) -> _ButlerRegistry:
89 """Create registry database and return `_ButlerRegistry` instance.
91 This method initializes database contents, database must be empty
92 prior to calling this method.
94 Parameters
95 ----------
96 dimensionConfig : `DimensionConfig` or `str`, optional
97 Dimensions configuration, if missing then default configuration
98 will be loaded from dimensions.yaml.
99 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
100 Path to the repository root this `Registry` will manage.
102 Returns
103 -------
104 registry : `_ButlerRegistry`
105 A new `_ButlerRegistry` instance.
106 """
107 return self._registry_cls.createFromConfig(self._config, dimensionConfig, butlerRoot)
109 def from_config(
110 self,
111 butlerRoot: ResourcePathExpression | None = None,
112 writeable: bool = True,
113 defaults: RegistryDefaults | None = None,
114 ) -> _ButlerRegistry:
115 """Create `_ButlerRegistry` subclass instance from ``config``.
117 Registry database must be initialized prior to calling this method.
119 Parameters
120 ----------
121 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
122 Path to the repository root this `Registry` will manage.
123 writeable : `bool`, optional
124 If `True` (default) create a read-write connection to the database.
125 defaults : `~lsst.daf.butler.registry.RegistryDefaults`, optional
126 Default collection search path and/or output `~CollectionType.RUN`
127 collection.
129 Returns
130 -------
131 registry : `_ButlerRegistry` (subclass)
132 A new `_ButlerRegistry` subclass instance.
133 """
134 return self._registry_cls.fromConfig(self._config, butlerRoot, writeable, defaults)