Coverage for python/lsst/daf/butler/registry/_config.py: 53%
32 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-26 02:48 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-26 02:48 -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__ = ("RegistryConfig",)
32from typing import TYPE_CHECKING
34from lsst.utils import doImportType
36from .._config import ConfigSubset
37from ..repo_relocation import replaceRoot
38from .connectionString import ConnectionStringFactory
39from .interfaces import Database
41if TYPE_CHECKING:
42 import sqlalchemy
43 from lsst.resources import ResourcePathExpression
46class RegistryConfig(ConfigSubset):
47 """Configuration specific to a butler Registry."""
49 component = "registry"
50 requiredKeys = ("db",)
51 defaultConfigFile = "registry.yaml"
53 def getDialect(self) -> str:
54 """Parse the `db` key of the config and returns the database dialect.
56 Returns
57 -------
58 dialect : `str`
59 Dialect found in the connection string.
60 """
61 conStr = ConnectionStringFactory.fromConfig(self)
62 return conStr.get_backend_name()
64 def getDatabaseClass(self) -> type[Database]:
65 """Return the `Database` class targeted by configuration values.
67 The appropriate class is determined by parsing the `db` key to extract
68 the dialect, and then looking that up under the `engines` key of the
69 registry config.
70 """
71 dialect = self.getDialect()
72 if dialect not in self["engines"]:
73 raise ValueError(f"Connection string dialect has no known aliases. Received: {dialect}")
74 databaseClassName = self["engines", dialect]
75 databaseClass = doImportType(databaseClassName)
76 if not issubclass(databaseClass, Database):
77 raise TypeError(f"Imported database class {databaseClassName} is not a Database")
78 return databaseClass
80 def makeDefaultDatabaseUri(self, root: str) -> str | None:
81 """Return a default 'db' URI for the registry configured here that is
82 appropriate for a new empty repository with the given root.
84 Parameters
85 ----------
86 root : `str`
87 Filesystem path to the root of the data repository.
89 Returns
90 -------
91 uri : `str`
92 URI usable as the 'db' string in a `RegistryConfig`.
93 """
94 DatabaseClass = self.getDatabaseClass()
95 return DatabaseClass.makeDefaultUri(root)
97 def replaceRoot(self, root: ResourcePathExpression | None) -> None:
98 """Replace any occurrences of `BUTLER_ROOT_TAG` in the connection
99 with the given root directory.
101 Parameters
102 ----------
103 root : `lsst.resources.ResourcePathExpression`, or `None`
104 String to substitute for `BUTLER_ROOT_TAG`. Passing `None` here is
105 allowed only as a convenient way to raise an exception
106 (`ValueError`).
108 Raises
109 ------
110 ValueError
111 Raised if ``root`` is not set but a value is required.
112 """
113 self["db"] = replaceRoot(self["db"], root)
115 @property
116 def connectionString(self) -> sqlalchemy.engine.url.URL:
117 """Return the connection string to the underlying database
118 (`sqlalchemy.engine.url.URL`).
119 """
120 return ConnectionStringFactory.fromConfig(self)