Coverage for python/lsst/daf/butler/registry/attributes.py: 37%
53 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-12 09:20 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-12 09:20 +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 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/>.
22"""The default concrete implementation of the class that manages
23attributes for `Registry`.
24"""
26from __future__ import annotations
28__all__ = ["DefaultButlerAttributeManager"]
30from collections.abc import Iterable
31from typing import ClassVar
33import sqlalchemy
35from ..core.ddl import FieldSpec, TableSpec
36from .interfaces import (
37 ButlerAttributeExistsError,
38 ButlerAttributeManager,
39 Database,
40 StaticTablesContext,
41 VersionTuple,
42)
44# Schema version 1.0.1 signifies that we do not write schema digests. Writing
45# is done by the `versions` module, but table is controlled by this manager.
46_VERSION = VersionTuple(1, 0, 1)
49class DefaultButlerAttributeManager(ButlerAttributeManager):
50 """An implementation of `ButlerAttributeManager` that stores attributes
51 in a database table.
53 Parameters
54 ----------
55 db : `Database`
56 Database engine interface for the namespace in which this table lives.
57 table : `sqlalchemy.schema.Table`
58 SQLAlchemy representation of the table that stores attributes.
59 """
61 def __init__(
62 self,
63 db: Database,
64 table: sqlalchemy.schema.Table,
65 registry_schema_version: VersionTuple | None = None,
66 ):
67 super().__init__(registry_schema_version=registry_schema_version)
68 self._db = db
69 self._table = table
71 _TABLE_NAME: ClassVar[str] = "butler_attributes"
73 _TABLE_SPEC: ClassVar[TableSpec] = TableSpec(
74 fields=[
75 FieldSpec("name", dtype=sqlalchemy.String, length=1024, primaryKey=True),
76 FieldSpec("value", dtype=sqlalchemy.String, length=65535, nullable=False),
77 ],
78 )
80 @classmethod
81 def initialize(
82 cls, db: Database, context: StaticTablesContext, registry_schema_version: VersionTuple | None = None
83 ) -> ButlerAttributeManager:
84 # Docstring inherited from ButlerAttributeManager.
85 table = context.addTable(cls._TABLE_NAME, cls._TABLE_SPEC)
86 return cls(db=db, table=table, registry_schema_version=registry_schema_version)
88 def get(self, name: str, default: str | None = None) -> str | None:
89 # Docstring inherited from ButlerAttributeManager.
90 sql = sqlalchemy.sql.select(self._table.columns.value).where(self._table.columns.name == name)
91 with self._db.query(sql) as sql_result:
92 row = sql_result.fetchone()
93 if row is not None:
94 return row[0]
95 return default
97 def set(self, name: str, value: str, *, force: bool = False) -> None:
98 # Docstring inherited from ButlerAttributeManager.
99 if not name or not value:
100 raise ValueError("name and value cannot be empty")
101 if force:
102 self._db.replace(
103 self._table,
104 {
105 "name": name,
106 "value": value,
107 },
108 )
109 else:
110 try:
111 self._db.insert(
112 self._table,
113 {
114 "name": name,
115 "value": value,
116 },
117 )
118 except sqlalchemy.exc.IntegrityError as exc:
119 raise ButlerAttributeExistsError(f"attribute {name} already exists") from exc
121 def delete(self, name: str) -> bool:
122 # Docstring inherited from ButlerAttributeManager.
123 numRows = self._db.delete(self._table, ["name"], {"name": name})
124 return numRows > 0
126 def items(self) -> Iterable[tuple[str, str]]:
127 # Docstring inherited from ButlerAttributeManager.
128 sql = sqlalchemy.sql.select(
129 self._table.columns.name,
130 self._table.columns.value,
131 )
132 with self._db.query(sql) as sql_result:
133 sql_rows = sql_result.fetchall()
134 for row in sql_rows:
135 yield row[0], row[1]
137 def empty(self) -> bool:
138 # Docstring inherited from ButlerAttributeManager.
139 sql = sqlalchemy.sql.select(sqlalchemy.sql.func.count()).select_from(self._table)
140 with self._db.query(sql) as sql_result:
141 count = sql_result.scalar()
142 return count == 0
144 @classmethod
145 def currentVersions(cls) -> list[VersionTuple]:
146 # Docstring inherited from VersionedExtension.
147 return [_VERSION]