Coverage for python/lsst/daf/butler/registry/_config.py: 51%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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__ = ("RegistryConfig",)
26from typing import TYPE_CHECKING, Optional, Type, Union
28from lsst.utils import doImportType
30from ..core import ConfigSubset
31from ..core.repoRelocation import replaceRoot
32from .connectionString import ConnectionStringFactory
33from .interfaces import Database
35if TYPE_CHECKING: 35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true
36 import sqlalchemy
38 from ..core import ButlerURI
41class RegistryConfig(ConfigSubset):
42 component = "registry"
43 requiredKeys = ("db",)
44 defaultConfigFile = "registry.yaml"
46 def getDialect(self) -> str:
47 """Parses the `db` key of the config and returns the database dialect.
49 Returns
50 -------
51 dialect : `str`
52 Dialect found in the connection string.
53 """
54 conStr = ConnectionStringFactory.fromConfig(self)
55 return conStr.get_backend_name()
57 def getDatabaseClass(self) -> Type[Database]:
58 """Returns the `Database` class targeted by configuration values.
60 The appropriate class is determined by parsing the `db` key to extract
61 the dialect, and then looking that up under the `engines` key of the
62 registry config.
63 """
64 dialect = self.getDialect()
65 if dialect not in self["engines"]:
66 raise ValueError(f"Connection string dialect has no known aliases. Received: {dialect}")
67 databaseClassName = self["engines", dialect]
68 databaseClass = doImportType(databaseClassName)
69 if not issubclass(databaseClass, Database):
70 raise TypeError(f"Imported database class {databaseClassName} is not a Database")
71 return databaseClass
73 def makeDefaultDatabaseUri(self, root: str) -> Optional[str]:
74 """Return a default 'db' URI for the registry configured here that is
75 appropriate for a new empty repository with the given root.
77 Parameters
78 ----------
79 root : `str`
80 Filesystem path to the root of the data repository.
82 Returns
83 -------
84 uri : `str`
85 URI usable as the 'db' string in a `RegistryConfig`.
86 """
87 DatabaseClass = self.getDatabaseClass()
88 return DatabaseClass.makeDefaultUri(root)
90 def replaceRoot(self, root: Optional[Union[str, ButlerURI]]) -> None:
91 """Replace any occurrences of `BUTLER_ROOT_TAG` in the connection
92 with the given root directory.
94 Parameters
95 ----------
96 root : `str`, `ButlerURI`, or `None`
97 String to substitute for `BUTLER_ROOT_TAG`. Passing `None` here is
98 allowed only as a convenient way to raise an exception
99 (`ValueError`).
101 Raises
102 ------
103 ValueError
104 Raised if ``root`` is not set but a value is required.
105 """
106 self["db"] = replaceRoot(self["db"], root)
108 @property
109 def connectionString(self) -> sqlalchemy.engine.url.URL:
110 """Return the connection string to the underlying database
111 (`sqlalchemy.engine.url.URL`).
112 """
113 return ConnectionStringFactory.fromConfig(self)