Source code for bragi_quantum.api.handlers.data

# ══════════════════════════════════════════════════════════════════════════
#     ___   ___ ___   _      _   ___
#    / _ \ / __|_  ) | |    /_\ | _ )
#   | (_) | (__ / /  | |__ / _ \| _ \
#    \__\_\\___/___| |____/_/ \_\___/
#
#   QC2 @ CHALMERS CALIBRATION TOOLKIT
#   (c) 2026 Chalmers University of Technology and Project Contributors
# ──────────────────────────────────────────────────────────────────────────
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
# ──────────────────────────────────────────────────────────────────────────
#   CONTRIBUTORS list:
#
#   Matteo Robbiati, robbiati@chalmers.se
#   Axel Andersson, axean@chalmers.se
#   Yanchen Lin, linya@chalmers.se
# ══════════════════════════════════════════════════════════════════════════


from __future__ import annotations

from pathlib import Path
from typing import Literal, override

import redis
from pydantic import BaseModel, PrivateAttr

from bragi_quantum.datalayer.utils.logging import logger


[docs] class RedisDataHandler(BaseModel): """Configuration for Redis data handling. ``redis_port=None`` connects an in-memory ``fakeredis`` store instead of a Redis server — for tests and hardware-free dummy runs. """ database_type: Literal["redis"] = "redis" redis_port: int | None = 6379 redis_host: str = "localhost" # Store the live connection here _conn: redis.Redis | None = PrivateAttr(default=None)
[docs] @override def model_post_init(self, __context): """Initialize the Redis connection after Pydantic validation.""" if self.redis_port is None: import fakeredis # test dependency, only needed for fake stores self._conn = fakeredis.FakeStrictRedis(decode_responses=True) return self._conn = redis.Redis( host=self.redis_host, port=self.redis_port, decode_responses=True ) try: self._conn.ping() except redis.ConnectionError: logger.warning( f"⚠️ Warning: Could not connect to Redis at {self.redis_host}:{self.redis_port}" )
@property def conn(self) -> redis.Redis: """Public getter for the connection.""" if self._conn is None: raise RuntimeError("Redis connection not initialized.") return self._conn
[docs] class SnapshotDataHandler(BaseModel): """Configuration for local snapshot data handling.""" database_type: Literal["snapshot"] = "snapshot" snapshot_dir: Path = Path("./snapshots")