Coverage for python/lsst/daf/butler/registry/_registry_factory.py: 43%
24 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 02:53 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 02:53 -0700
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
36from .._config import Config
37from ..dimensions import DimensionConfig
38from ._config import RegistryConfig
39from ._defaults import RegistryDefaults
40from .sql_registry import SqlRegistry
42if TYPE_CHECKING:
43 from .._butler_config 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 registry_cls_name = config.get("cls")
76 # Check that config does not specify unknown registry type, and allow
77 # both old and new location of SqlRegistry.
78 if registry_cls_name not in (
79 "lsst.daf.butler.registries.sql.SqlRegistry",
80 "lsst.daf.butler.registry.sql_registry.SqlRegistry",
81 None,
82 ):
83 raise TypeError(
84 f"Registry class obtained from config {registry_cls_name} is not a SqlRegistry class."
85 )
86 self._registry_cls = SqlRegistry
88 def create_from_config(
89 self,
90 dimensionConfig: DimensionConfig | str | None = None,
91 butlerRoot: ResourcePathExpression | None = None,
92 ) -> SqlRegistry:
93 """Create registry database and return `SqlRegistry` instance.
95 This method initializes database contents, database must be empty
96 prior to calling this method.
98 Parameters
99 ----------
100 dimensionConfig : `DimensionConfig` or `str`, optional
101 Dimensions configuration, if missing then default configuration
102 will be loaded from dimensions.yaml.
103 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
104 Path to the repository root this `Registry` will manage.
106 Returns
107 -------
108 registry : `SqlRegistry`
109 A new `SqlRegistry` instance.
110 """
111 return self._registry_cls.createFromConfig(self._config, dimensionConfig, butlerRoot)
113 def from_config(
114 self,
115 butlerRoot: ResourcePathExpression | None = None,
116 writeable: bool = True,
117 defaults: RegistryDefaults | None = None,
118 ) -> SqlRegistry:
119 """Create `SqlRegistry` subclass instance from ``config``.
121 Registry database must be initialized prior to calling this method.
123 Parameters
124 ----------
125 butlerRoot : convertible to `lsst.resources.ResourcePath`, optional
126 Path to the repository root this `Registry` will manage.
127 writeable : `bool`, optional
128 If `True` (default) create a read-write connection to the database.
129 defaults : `~lsst.daf.butler.registry.RegistryDefaults`, optional
130 Default collection search path and/or output `~CollectionType.RUN`
131 collection.
133 Returns
134 -------
135 registry : `SqlRegistry` (subclass)
136 A new `SqlRegistry` subclass instance.
137 """
138 return self._registry_cls.fromConfig(self._config, butlerRoot, writeable, defaults)