Coverage for python / lsst / daf / butler / registry / _config.py: 41%
41 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:49 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:49 +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__ = ("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
44 from lsst.resources import ResourcePathExpression
47class RegistryConfig(ConfigSubset):
48 """Configuration specific to a butler Registry."""
50 component = "registry"
51 requiredKeys = ("db",)
52 defaultConfigFile = "registry.yaml"
54 def getDialect(self) -> str:
55 """Parse the ``db`` key of the config and returns the database dialect.
57 Returns
58 -------
59 dialect : `str`
60 Dialect found in the connection string.
61 """
62 conStr = ConnectionStringFactory.fromConfig(self)
63 return conStr.get_backend_name()
65 def getDatabaseClass(self) -> type[Database]:
66 """Return the `Database` class targeted by configuration values.
68 The appropriate class is determined by parsing the ``db`` key to
69 extract the dialect, and then looking that up under the ``engines`` key
70 of the registry config.
71 """
72 dialect = self.getDialect()
73 if dialect not in self["engines"]:
74 raise ValueError(f"Connection string dialect has no known aliases. Received: {dialect}")
75 databaseClassName = self["engines", dialect]
76 databaseClass = doImportType(databaseClassName)
77 if not issubclass(databaseClass, Database):
78 raise TypeError(f"Imported database class {databaseClassName} is not a Database")
79 return databaseClass
81 def makeDefaultDatabaseUri(self, root: str) -> str | None:
82 """Return a default 'db' URI for the registry configured here that is
83 appropriate for a new empty repository with the given root.
85 Parameters
86 ----------
87 root : `str`
88 Filesystem path to the root of the data repository.
90 Returns
91 -------
92 uri : `str`
93 URI usable as the 'db' string in a `RegistryConfig`.
94 """
95 DatabaseClass = self.getDatabaseClass()
96 return DatabaseClass.makeDefaultUri(root)
98 def replaceRoot(self, root: ResourcePathExpression | None) -> None:
99 """Replace any occurrences of `BUTLER_ROOT_TAG` in the connection
100 with the given root directory.
102 Parameters
103 ----------
104 root : `lsst.resources.ResourcePathExpression`, or `None`
105 String to substitute for `BUTLER_ROOT_TAG`. Passing `None` here is
106 allowed only as a convenient way to raise an exception
107 (`ValueError`).
109 Raises
110 ------
111 ValueError
112 Raised if ``root`` is not set but a value is required.
113 """
114 self["db"] = replaceRoot(self["db"], root)
116 @property
117 def connectionString(self) -> sqlalchemy.engine.url.URL:
118 """Return the connection string to the underlying database
119 (`sqlalchemy.engine.url.URL`).
120 """
121 return ConnectionStringFactory.fromConfig(self)
123 @property
124 def areTemporaryTablesAllowed(self) -> bool:
125 """Return `True` if the database allows creating temporary tables for
126 read operations; `False` otherwise.
128 By default this is `True`, because there is a performance penalty
129 for some queries if temporary tables cannot be used.
131 This should be set to `False` when using a Postgres hot standby using
132 physical replication or an AlloyDB read pool, since these backends
133 do not support temporary tables.
134 """
135 key = "temporary_tables"
136 value = self.get(key)
137 if value is None:
138 return True
139 elif isinstance(value, bool):
140 return value
142 raise ValueError(f"Unexpected value '{value}' for '{key}' in registry configuration")