Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Oct 13, 2023
2 parents 3e5235b + cc7fe9e commit a965fb3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 1 addition & 2 deletions spinn_front_end_common/utilities/base_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import time
from typing import Optional, Union
from typing_extensions import TypeAlias
from spinn_utilities.abstract_context_manager import AbstractContextManager
from spinn_front_end_common.data import FecDataView
from spinn_front_end_common.utilities.sqlite_db import SQLiteDB

Expand All @@ -31,7 +30,7 @@ def _timestamp():
return int(time.time() * _SECONDS_TO_MICRO_SECONDS_CONVERSION)


class BaseDatabase(SQLiteDB, AbstractContextManager):
class BaseDatabase(SQLiteDB):
"""
Specific implementation of the Database for SQLite 3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

import logging
from typing import List, Optional
from concurrent.futures import (
Future, ThreadPoolExecutor, wait)
from spinn_utilities.abstract_context_manager import AbstractContextManager
from concurrent.futures import Future
from concurrent.futures import ThreadPoolExecutor, wait # @UnresolvedImport
from spinn_utilities.config_holder import get_config_bool, get_config_int
from spinn_utilities.log import FormatAdapter
from spinnman.connections.udp_packet_connections import EIEIOConnection
Expand All @@ -33,7 +32,7 @@
logger = FormatAdapter(logging.getLogger(__name__))


class NotificationProtocol(AbstractContextManager):
class NotificationProtocol(object):
"""
The protocol which hand shakes with external devices about the
database and starting execution.
Expand Down
16 changes: 12 additions & 4 deletions spinn_front_end_common/utilities/sqlite_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import sqlite3
import struct
from typing import Optional, Type, Union
from spinn_utilities.abstract_context_manager import AbstractContextManager
from pacman.exceptions import PacmanValueError
from spinn_front_end_common.utilities.exceptions import DatabaseException

logger = logging.getLogger(__name__)


class SQLiteDB(AbstractContextManager):
class SQLiteDB(object):
"""
General support class for SQLite databases. This handles a lot of the
low-level detail of setting up a connection.
Expand Down Expand Up @@ -150,6 +149,12 @@ def __init__(
self.__pragma("trusted_schema", False)

def _context_entered(self):
"""
Work to do when then context is entered.
May be extended by super classes
"""
if self.__db is None:
raise DatabaseException("database has been closed")
if self.__cursor is not None:
Expand All @@ -158,15 +163,18 @@ def _context_entered(self):
self.__db.execute("BEGIN")
self.__cursor = self.__db.cursor()

def __enter__(self):
self._context_entered()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self.__db is not None:
if exc_type is None:
self.__db.commit()
else:
self.__db.rollback()
self.__cursor = None
# calls close
return super().__exit__(exc_type, exc_val, exc_tb)
self.close()

def __del__(self) -> None:
self.close()
Expand Down

0 comments on commit a965fb3

Please sign in to comment.