Skip to content

Commit

Permalink
pupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesmadere committed Feb 6, 2025
1 parent 0090404 commit fa18f97
Show file tree
Hide file tree
Showing 26 changed files with 220 additions and 88 deletions.
12 changes: 6 additions & 6 deletions initCynanBotAll.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@
from src.starWars.starWarsQuotesRepository import StarWarsQuotesRepository
from src.starWars.starWarsQuotesRepositoryInterface import StarWarsQuotesRepositoryInterface
from src.storage.backingDatabase import BackingDatabase
from src.storage.backingPsqlDatabase import BackingPsqlDatabase
from src.storage.backingSqliteDatabase import BackingSqliteDatabase
from src.storage.databaseType import DatabaseType
from src.storage.jsonFileReader import JsonFileReader
from src.storage.linesFileReader import LinesFileReader
from src.storage.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.psql.psqlBackingDatabase import PsqlBackingDatabase
from src.storage.psql.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.sqlite.sqliteBackingDatabase import SqliteBackingDatabase
from src.storage.storageJsonMapper import StorageJsonMapper
from src.storage.storageJsonMapperInterface import StorageJsonMapperInterface
from src.storage.tempFileHelper import TempFileHelper
Expand Down Expand Up @@ -682,14 +682,14 @@
credentialsJsonReader = JsonFileReader('../config/psqlCredentials.json')
)

backingDatabase = BackingPsqlDatabase(
backingDatabase = PsqlBackingDatabase(
eventLoop = eventLoop,
psqlCredentialsProvider = psqlCredentialsProvider,
timber = timber
)

case DatabaseType.SQLITE:
backingDatabase = BackingSqliteDatabase(
backingDatabase = SqliteBackingDatabase(
eventLoop = eventLoop
)

Expand Down
12 changes: 6 additions & 6 deletions initCynanBotBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@
from src.starWars.starWarsQuotesRepository import StarWarsQuotesRepository
from src.starWars.starWarsQuotesRepositoryInterface import StarWarsQuotesRepositoryInterface
from src.storage.backingDatabase import BackingDatabase
from src.storage.backingPsqlDatabase import BackingPsqlDatabase
from src.storage.backingSqliteDatabase import BackingSqliteDatabase
from src.storage.databaseType import DatabaseType
from src.storage.jsonFileReader import JsonFileReader
from src.storage.linesFileReader import LinesFileReader
from src.storage.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.psql.psqlBackingDatabase import PsqlBackingDatabase
from src.storage.psql.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.sqlite.sqliteBackingDatabase import SqliteBackingDatabase
from src.storage.storageJsonMapper import StorageJsonMapper
from src.storage.storageJsonMapperInterface import StorageJsonMapperInterface
from src.streamAlertsManager.streamAlertsManagerInterface import StreamAlertsManagerInterface
Expand Down Expand Up @@ -509,14 +509,14 @@
credentialsJsonReader = JsonFileReader('../config/psqlCredentials.json')
)

backingDatabase = BackingPsqlDatabase(
backingDatabase = PsqlBackingDatabase(
eventLoop = eventLoop,
psqlCredentialsProvider = psqlCredentialsProvider,
timber = timber
)

case DatabaseType.SQLITE:
backingDatabase = BackingSqliteDatabase(
backingDatabase = SqliteBackingDatabase(
eventLoop = eventLoop
)

Expand Down
12 changes: 6 additions & 6 deletions initCynanBotChatBand.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
from src.soundPlayerManager.settings.soundPlayerSettingsRepositoryInterface import \
SoundPlayerSettingsRepositoryInterface
from src.storage.backingDatabase import BackingDatabase
from src.storage.backingPsqlDatabase import BackingPsqlDatabase
from src.storage.backingSqliteDatabase import BackingSqliteDatabase
from src.storage.databaseType import DatabaseType
from src.storage.jsonFileReader import JsonFileReader
from src.storage.linesFileReader import LinesFileReader
from src.storage.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.psql.psqlBackingDatabase import PsqlBackingDatabase
from src.storage.psql.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.sqlite.sqliteBackingDatabase import SqliteBackingDatabase
from src.storage.storageJsonMapper import StorageJsonMapper
from src.storage.storageJsonMapperInterface import StorageJsonMapperInterface
from src.streamAlertsManager.streamAlertsManagerInterface import StreamAlertsManagerInterface
Expand Down Expand Up @@ -270,14 +270,14 @@
credentialsJsonReader = JsonFileReader('../config/psqlCredentials.json')
)

backingDatabase = BackingPsqlDatabase(
backingDatabase = PsqlBackingDatabase(
eventLoop = eventLoop,
psqlCredentialsProvider = psqlCredentialsProvider,
timber = timber
)

case DatabaseType.SQLITE:
backingDatabase = BackingSqliteDatabase(
backingDatabase = SqliteBackingDatabase(
eventLoop = eventLoop
)

Expand Down
12 changes: 6 additions & 6 deletions initCynanBotTts.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@
from src.soundPlayerManager.settings.soundPlayerSettingsRepositoryInterface import \
SoundPlayerSettingsRepositoryInterface
from src.storage.backingDatabase import BackingDatabase
from src.storage.backingPsqlDatabase import BackingPsqlDatabase
from src.storage.backingSqliteDatabase import BackingSqliteDatabase
from src.storage.databaseType import DatabaseType
from src.storage.jsonFileReader import JsonFileReader
from src.storage.linesFileReader import LinesFileReader
from src.storage.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.psql.psqlBackingDatabase import PsqlBackingDatabase
from src.storage.psql.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from src.storage.sqlite.sqliteBackingDatabase import SqliteBackingDatabase
from src.storage.storageJsonMapper import StorageJsonMapper
from src.storage.storageJsonMapperInterface import StorageJsonMapperInterface
from src.storage.tempFileHelper import TempFileHelper
Expand Down Expand Up @@ -484,14 +484,14 @@
credentialsJsonReader = JsonFileReader('../config/psqlCredentials.json')
)

backingDatabase = BackingPsqlDatabase(
backingDatabase = PsqlBackingDatabase(
eventLoop = eventLoop,
psqlCredentialsProvider = psqlCredentialsProvider,
timber = timber
)

case DatabaseType.SQLITE:
backingDatabase = BackingSqliteDatabase(
backingDatabase = SqliteBackingDatabase(
eventLoop = eventLoop
)

Expand Down
10 changes: 5 additions & 5 deletions initGlacialQuestionTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from src.soundPlayerManager.jsonMapper.soundPlayerJsonMapper import SoundPlayerJsonMapper
from src.soundPlayerManager.jsonMapper.soundPlayerJsonMapperInterface import SoundPlayerJsonMapperInterface
from src.storage.backingDatabase import BackingDatabase
from src.storage.backingPsqlDatabase import BackingPsqlDatabase
from src.storage.backingSqliteDatabase import BackingSqliteDatabase
from src.storage.databaseType import DatabaseType
from src.storage.jsonFileReader import JsonFileReader
from src.storage.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.psql.psqlBackingDatabase import PsqlBackingDatabase
from src.storage.psql.psqlCredentialsProvider import PsqlCredentialsProvider
from src.storage.sqlite.sqliteBackingDatabase import SqliteBackingDatabase
from src.storage.storageJsonMapper import StorageJsonMapper
from src.storage.storageJsonMapperInterface import StorageJsonMapperInterface
from src.timber.timber import Timber
Expand Down Expand Up @@ -100,7 +100,7 @@
backingDatabase: BackingDatabase
match generalSettingsSnapshot.requireDatabaseType():
case DatabaseType.POSTGRESQL:
backingDatabase = BackingPsqlDatabase(
backingDatabase = PsqlBackingDatabase(
eventLoop = eventLoop,
psqlCredentialsProvider = PsqlCredentialsProvider(
credentialsJsonReader = JsonFileReader('../config/psqlCredentials.json')
Expand All @@ -109,7 +109,7 @@
)

case DatabaseType.SQLITE:
backingDatabase = BackingSqliteDatabase(
backingDatabase = SqliteBackingDatabase(
eventLoop = eventLoop
)

Expand Down
2 changes: 1 addition & 1 deletion src/chatCommands/clearCachesChatCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ..soundPlayerManager.randomizerHelper.soundPlayerRandomizerHelperInterface import \
SoundPlayerRandomizerHelperInterface
from ..soundPlayerManager.settings.soundPlayerSettingsRepositoryInterface import SoundPlayerSettingsRepositoryInterface
from ..storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from ..storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from ..streamAlertsManager.streamAlertsSettingsRepositoryInterface import StreamAlertsSettingsRepositoryInterface
from ..streamElements.settings.streamElementsSettingsRepositoryInterface import \
StreamElementsSettingsRepositoryInterface
Expand Down
2 changes: 1 addition & 1 deletion src/cynanBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
from .soundPlayerManager.randomizerHelper.soundPlayerRandomizerHelper import SoundPlayerRandomizerHelperInterface
from .soundPlayerManager.settings.soundPlayerSettingsRepositoryInterface import SoundPlayerSettingsRepositoryInterface
from .starWars.starWarsQuotesRepositoryInterface import StarWarsQuotesRepositoryInterface
from .storage.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from .storage.psql.psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from .streamAlertsManager.streamAlertsManagerInterface import StreamAlertsManagerInterface
from .streamAlertsManager.streamAlertsSettingsRepositoryInterface import StreamAlertsSettingsRepositoryInterface
from .streamElements.settings.streamElementsSettingsRepositoryInterface import StreamElementsSettingsRepositoryInterface
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions src/glacialTtsStorage/mapper/glacialTtsDataMapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from .glacialTtsDataMapperInterface import GlacialTtsDataMapperInterface
from ...misc import utils as utils
from ...tts.ttsProvider import TtsProvider


class GlacialTtsDataMapper(GlacialTtsDataMapperInterface):

async def fromDatabaseName(self, ttsProvider: str) -> TtsProvider:
if not utils.isValidStr(ttsProvider):
raise TypeError(f'ttsProvider argument is malformed: \"{ttsProvider}\"')

ttsProvider = ttsProvider.lower()

match ttsProvider:
case 'dec_talk': return TtsProvider.DEC_TALK
case 'google': return TtsProvider.GOOGLE
case 'half_life': return TtsProvider.HALF_LIFE
case 'microsoft_sam': return TtsProvider.MICROSOFT_SAM
case 'singing_dec_talk': return TtsProvider.SINGING_DEC_TALK
case 'stream_elements': return TtsProvider.STREAM_ELEMENTS
case 'tts_monster': return TtsProvider.TTS_MONSTER
case _: raise ValueError(f'Encountered unknown TtsProvider value: \"{ttsProvider}\"')

async def toDatabaseName(self, ttsProvider: TtsProvider) -> str:
match ttsProvider:
case TtsProvider.DEC_TALK: return 'dec_talk'
case TtsProvider.GOOGLE: return 'google'
case TtsProvider.HALF_LIFE: return 'half_life'
case TtsProvider.MICROSOFT_SAM: return 'microsoft_sam'
case TtsProvider.SINGING_DEC_TALK: return 'singing_dec_talk'
case TtsProvider.STREAM_ELEMENTS: return 'stream_elements'
case TtsProvider.TTS_MONSTER: return 'tts_monster'
case _: raise ValueError(f'Encountered unknown TtsProvider value: \"{ttsProvider}\"')
14 changes: 14 additions & 0 deletions src/glacialTtsStorage/mapper/glacialTtsDataMapperInterface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from abc import ABC, abstractmethod

from ...tts.ttsProvider import TtsProvider


class GlacialTtsDataMapperInterface(ABC):

@abstractmethod
async def fromDatabaseName(self, ttsProvider: str) -> TtsProvider:
pass

@abstractmethod
async def toDatabaseName(self, ttsProvider: TtsProvider) -> str:
pass
Empty file.
Empty file added src/storage/psql/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import traceback
from asyncio import AbstractEventLoop
from dataclasses import dataclass

import asyncpg

from .backingDatabase import BackingDatabase
from .databaseConnection import DatabaseConnection
from .databaseType import DatabaseType
from .psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from .psqlDatabaseConnection import PsqlDatabaseConnection
from ..timber.timberInterface import TimberInterface
from ..backingDatabase import BackingDatabase
from ..databaseConnection import DatabaseConnection
from ..databaseType import DatabaseType
from ...timber.timberInterface import TimberInterface


class BackingPsqlDatabase(BackingDatabase):
class PsqlBackingDatabase(BackingDatabase):

@dataclass(frozen = True)
class ConnectionPoolData:
connectionPool: asyncpg.Pool
wasConnectionPoolNewlyCreated: bool

def __init__(
self,
Expand Down Expand Up @@ -43,11 +49,25 @@ def databaseType(self) -> DatabaseType:
return DatabaseType.POSTGRESQL

async def getConnection(self) -> DatabaseConnection:
connectionPoolCreated = False
connectionPoolData = await self.__getConnectionPool()
connection = await connectionPoolData.connectionPool.acquire()

databaseConnection: DatabaseConnection = PsqlDatabaseConnection(
connection = connection,
pool = connectionPoolData.connectionPool
)

if connectionPoolData.wasConnectionPoolNewlyCreated:
await self.__createCollations(databaseConnection)

return databaseConnection

async def __getConnectionPool(self) -> ConnectionPoolData:
wasConnectionPoolNewlyCreated = False
connectionPool = self.__connectionPool

if connectionPool is None:
connectionPoolCreated = True
wasConnectionPoolNewlyCreated = True
databaseName = await self.__psqlCredentialsProvider.requireDatabaseName()
maxConnections = await self.__psqlCredentialsProvider.requireMaxConnections()
password = await self.__psqlCredentialsProvider.getPassword()
Expand All @@ -61,23 +81,16 @@ async def getConnection(self) -> DatabaseConnection:
user = user
)

self.__connectionPool = connectionPool

if not isinstance(connectionPool, asyncpg.Pool):
# this scenario should definitely be impossible, but the Python type checking was
# getting angry without this check
exception = RuntimeError(f'Failed to instantiate asyncpg.Pool: \"{connectionPool}\"')
self.__timber.log('BackingPsqlDatabase', f'Failed to instantiate asyncpg.Pool: \"{connectionPool}\" ({exception=})', exception, traceback.format_exc())
raise exception
if not isinstance(connectionPool, asyncpg.Pool):
# this scenario should definitely be impossible, but the Python type checking was
# getting angry without this check
exception = RuntimeError(f'Failed to instantiate asyncpg.Pool ({connectionPool=}) ({databaseName=}) ({maxConnections=}) ({user=})')
self.__timber.log('BackingPsqlDatabase', f'Failed to instantiate asyncpg.Pool ({connectionPool=}) ({databaseName=}) ({maxConnections=}) ({user=})', exception, traceback.format_exc())
raise exception

connection = await connectionPool.acquire()
self.__connectionPool = connectionPool

databaseConnection: DatabaseConnection = PsqlDatabaseConnection(
connection = connection,
pool = connectionPool
return PsqlBackingDatabase.ConnectionPoolData(
connectionPool = connectionPool,
wasConnectionPoolNewlyCreated = wasConnectionPoolNewlyCreated
)

if connectionPoolCreated:
await self.__createCollations(databaseConnection)

return databaseConnection
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Any

from .jsonReaderInterface import JsonReaderInterface
from .psqlCredentialsProviderInterface import PsqlCredentialsProviderInterface
from ..misc import utils as utils
from ..jsonReaderInterface import JsonReaderInterface
from ...misc import utils as utils


class PsqlCredentialsProvider(PsqlCredentialsProviderInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod

from ..misc.clearable import Clearable
from ...misc.clearable import Clearable


class PsqlCredentialsProviderInterface(Clearable):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import asyncpg

from .databaseConnection import DatabaseConnection
from .databaseType import DatabaseType
from .exceptions import DatabaseConnectionIsClosedException
from ..misc import utils as utils
from ..databaseConnection import DatabaseConnection
from ..databaseType import DatabaseType
from ..exceptions import DatabaseConnectionIsClosedException
from ...misc import utils as utils


class PsqlDatabaseConnection(DatabaseConnection):
Expand Down
Empty file added src/storage/sqlite/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import aiosqlite

from .backingDatabase import BackingDatabase
from .databaseConnection import DatabaseConnection
from .databaseType import DatabaseType
from .sqliteDatabaseConnection import SqliteDatabaseConnection
from ..misc import utils as utils
from ..backingDatabase import BackingDatabase
from ..databaseConnection import DatabaseConnection
from ..databaseType import DatabaseType
from ...misc import utils as utils


class BackingSqliteDatabase(BackingDatabase):
class SqliteBackingDatabase(BackingDatabase):

def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import aiosqlite

from .databaseConnection import DatabaseConnection
from .databaseType import DatabaseType
from .exceptions import DatabaseConnectionIsClosedException, DatabaseOperationalError
from ..misc import utils as utils
from ..databaseConnection import DatabaseConnection
from ..databaseType import DatabaseType
from ..exceptions import DatabaseConnectionIsClosedException, DatabaseOperationalError
from ...misc import utils as utils


class SqliteDatabaseConnection(DatabaseConnection):
Expand Down
Loading

0 comments on commit fa18f97

Please sign in to comment.