Coverage for python/lsst/daf/butler/registry/attributes.py: 37%

55 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-11 03:16 -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/>. 

27 

28"""The default concrete implementation of the class that manages 

29attributes for `Registry`. 

30""" 

31 

32from __future__ import annotations 

33 

34__all__ = ["DefaultButlerAttributeManager"] 

35 

36from collections.abc import Iterable 

37from typing import ClassVar 

38 

39import sqlalchemy 

40 

41from ..ddl import FieldSpec, TableSpec 

42from .interfaces import ( 

43 ButlerAttributeExistsError, 

44 ButlerAttributeManager, 

45 Database, 

46 StaticTablesContext, 

47 VersionTuple, 

48) 

49 

50# Schema version 1.0.1 signifies that we do not write schema digests. Writing 

51# is done by the `versions` module, but table is controlled by this manager. 

52_VERSION = VersionTuple(1, 0, 1) 

53 

54 

55class DefaultButlerAttributeManager(ButlerAttributeManager): 

56 """An implementation of `ButlerAttributeManager` that stores attributes 

57 in a database table. 

58 

59 Parameters 

60 ---------- 

61 db : `Database` 

62 Database engine interface for the namespace in which this table lives. 

63 table : `sqlalchemy.schema.Table` 

64 SQLAlchemy representation of the table that stores attributes. 

65 registry_schema_version : `VersionTuple` or `None`, optional 

66 The version of the registry schema. 

67 """ 

68 

69 def __init__( 

70 self, 

71 db: Database, 

72 table: sqlalchemy.schema.Table, 

73 registry_schema_version: VersionTuple | None = None, 

74 ): 

75 super().__init__(registry_schema_version=registry_schema_version) 

76 self._db = db 

77 self._table = table 

78 

79 _TABLE_NAME: ClassVar[str] = "butler_attributes" 

80 

81 _TABLE_SPEC: ClassVar[TableSpec] = TableSpec( 

82 fields=[ 

83 FieldSpec("name", dtype=sqlalchemy.String, length=1024, primaryKey=True), 

84 FieldSpec("value", dtype=sqlalchemy.String, length=65535, nullable=False), 

85 ], 

86 ) 

87 

88 def clone(self, db: Database) -> DefaultButlerAttributeManager: 

89 # Docstring inherited from ButlerAttributeManager. 

90 return DefaultButlerAttributeManager(db, self._table, self._registry_schema_version) 

91 

92 @classmethod 

93 def initialize( 

94 cls, db: Database, context: StaticTablesContext, registry_schema_version: VersionTuple | None = None 

95 ) -> ButlerAttributeManager: 

96 # Docstring inherited from ButlerAttributeManager. 

97 table = context.addTable(cls._TABLE_NAME, cls._TABLE_SPEC) 

98 return cls(db=db, table=table, registry_schema_version=registry_schema_version) 

99 

100 def get(self, name: str, default: str | None = None) -> str | None: 

101 # Docstring inherited from ButlerAttributeManager. 

102 sql = sqlalchemy.sql.select(self._table.columns.value).where(self._table.columns.name == name) 

103 with self._db.query(sql) as sql_result: 

104 row = sql_result.fetchone() 

105 if row is not None: 

106 return row[0] 

107 return default 

108 

109 def set(self, name: str, value: str, *, force: bool = False) -> None: 

110 # Docstring inherited from ButlerAttributeManager. 

111 if not name or not value: 

112 raise ValueError("name and value cannot be empty") 

113 if force: 

114 self._db.replace( 

115 self._table, 

116 { 

117 "name": name, 

118 "value": value, 

119 }, 

120 ) 

121 else: 

122 try: 

123 self._db.insert( 

124 self._table, 

125 { 

126 "name": name, 

127 "value": value, 

128 }, 

129 ) 

130 except sqlalchemy.exc.IntegrityError as exc: 

131 raise ButlerAttributeExistsError(f"attribute {name} already exists") from exc 

132 

133 def delete(self, name: str) -> bool: 

134 # Docstring inherited from ButlerAttributeManager. 

135 numRows = self._db.delete(self._table, ["name"], {"name": name}) 

136 return numRows > 0 

137 

138 def items(self) -> Iterable[tuple[str, str]]: 

139 # Docstring inherited from ButlerAttributeManager. 

140 sql = sqlalchemy.sql.select( 

141 self._table.columns.name, 

142 self._table.columns.value, 

143 ) 

144 with self._db.query(sql) as sql_result: 

145 sql_rows = sql_result.fetchall() 

146 for row in sql_rows: 

147 yield row[0], row[1] 

148 

149 def empty(self) -> bool: 

150 # Docstring inherited from ButlerAttributeManager. 

151 sql = sqlalchemy.sql.select(sqlalchemy.sql.func.count()).select_from(self._table) 

152 with self._db.query(sql) as sql_result: 

153 count = sql_result.scalar() 

154 return count == 0 

155 

156 @classmethod 

157 def currentVersions(cls) -> list[VersionTuple]: 

158 # Docstring inherited from VersionedExtension. 

159 return [_VERSION]