Hide keyboard shortcuts

Hot-keys 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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("RegistryConfig",) 

25 

26from typing import Type, TYPE_CHECKING 

27 

28from lsst.utils import doImport 

29 

30from ..core import ConfigSubset 

31from ..core.repoRelocation import replaceRoot 

32from .connectionString import ConnectionStringFactory 

33 

34if TYPE_CHECKING: 34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never true

35 from .interfaces import Database 

36 

37 

38class RegistryConfig(ConfigSubset): 

39 component = "registry" 

40 requiredKeys = ("db",) 

41 defaultConfigFile = "registry.yaml" 

42 

43 def getDialect(self): 

44 """Parses the `db` key of the config and returns the database dialect. 

45 

46 Returns 

47 ------- 

48 dialect : `str` 

49 Dialect found in the connection string. 

50 """ 

51 conStr = ConnectionStringFactory.fromConfig(self) 

52 return conStr.get_backend_name() 

53 

54 def getDatabaseClass(self) -> Type[Database]: 

55 """Returns the `Database` class targeted by configuration values. 

56 

57 The appropriate class is determined by parsing the `db` key to extract 

58 the dialect, and then looking that up under the `engines` key of the 

59 registry config. 

60 """ 

61 dialect = self.getDialect() 

62 if dialect not in self["engines"]: 

63 raise ValueError(f"Connection string dialect has no known aliases. Received: {dialect}") 

64 databaseClass = self["engines", dialect] 

65 return doImport(databaseClass) 

66 

67 def makeDefaultDatabaseUri(self, root: str): 

68 """Return a default 'db' URI for the registry configured here that is 

69 appropriate for a new empty repository with the given root. 

70 

71 Parameters 

72 ---------- 

73 root : `str` 

74 Filesystem path to the root of the data repository. 

75 

76 Returns 

77 ------- 

78 uri : `str` 

79 URI usable as the 'db' string in a `RegistryConfig`. 

80 """ 

81 DatabaseClass = self.getDatabaseClass() 

82 return DatabaseClass.makeDefaultUri(root) 

83 

84 def replaceRoot(self, root: str): 

85 """Replace any occurrences of `BUTLER_ROOT_TAG` in the connection 

86 with the given root directory. 

87 """ 

88 self["db"] = replaceRoot(self["db"], root) 

89 

90 @property 

91 def connectionString(self): 

92 """Return the connection string to the underlying database 

93 (`sqlalchemy.engine.url.URL`). 

94 """ 

95 return ConnectionStringFactory.fromConfig(self)