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

51 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-28 04:40 -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 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/>. 

21from __future__ import annotations 

22 

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

24attributes for `Registry`. 

25""" 

26 

27__all__ = ["DefaultButlerAttributeManager"] 

28 

29from typing import ClassVar, Iterable, Optional, Tuple 

30 

31import sqlalchemy 

32 

33from ..core.ddl import FieldSpec, TableSpec 

34from .interfaces import ( 

35 ButlerAttributeExistsError, 

36 ButlerAttributeManager, 

37 Database, 

38 StaticTablesContext, 

39 VersionTuple, 

40) 

41 

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

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

44_VERSION = VersionTuple(1, 0, 1) 

45 

46 

47class DefaultButlerAttributeManager(ButlerAttributeManager): 

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

49 in a database table. 

50 

51 Parameters 

52 ---------- 

53 db : `Database` 

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

55 table : `sqlalchemy.schema.Table` 

56 SQLAlchemy representation of the table that stores attributes. 

57 """ 

58 

59 def __init__(self, db: Database, table: sqlalchemy.schema.Table): 

60 self._db = db 

61 self._table = table 

62 

63 _TABLE_NAME: ClassVar[str] = "butler_attributes" 

64 

65 _TABLE_SPEC: ClassVar[TableSpec] = TableSpec( 

66 fields=[ 

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

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

69 ], 

70 ) 

71 

72 @classmethod 

73 def initialize(cls, db: Database, context: StaticTablesContext) -> ButlerAttributeManager: 

74 # Docstring inherited from ButlerAttributeManager. 

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

76 return cls(db=db, table=table) 

77 

78 def get(self, name: str, default: Optional[str] = None) -> Optional[str]: 

79 # Docstring inherited from ButlerAttributeManager. 

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

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

82 row = sql_result.fetchone() 

83 if row is not None: 

84 return row[0] 

85 return default 

86 

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

88 # Docstring inherited from ButlerAttributeManager. 

89 if not name or not value: 

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

91 if force: 

92 self._db.replace( 

93 self._table, 

94 { 

95 "name": name, 

96 "value": value, 

97 }, 

98 ) 

99 else: 

100 try: 

101 self._db.insert( 

102 self._table, 

103 { 

104 "name": name, 

105 "value": value, 

106 }, 

107 ) 

108 except sqlalchemy.exc.IntegrityError as exc: 

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

110 

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

112 # Docstring inherited from ButlerAttributeManager. 

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

114 return numRows > 0 

115 

116 def items(self) -> Iterable[Tuple[str, str]]: 

117 # Docstring inherited from ButlerAttributeManager. 

118 sql = sqlalchemy.sql.select( 

119 self._table.columns.name, 

120 self._table.columns.value, 

121 ) 

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

123 sql_rows = sql_result.fetchall() 

124 for row in sql_rows: 

125 yield row[0], row[1] 

126 

127 def empty(self) -> bool: 

128 # Docstring inherited from ButlerAttributeManager. 

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

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

131 count = sql_result.scalar() 

132 return count == 0 

133 

134 @classmethod 

135 def currentVersion(cls) -> Optional[VersionTuple]: 

136 # Docstring inherited from VersionedExtension. 

137 return _VERSION