53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Self, Any
|
|
from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface
|
|
from Interfaces.Owners.DatabasesInterface import DatabasesInterface
|
|
|
|
class DatabasesManager:
|
|
|
|
def __init__(self:Self, nucelar_monitor:NucelarMonitorInterface) -> None:
|
|
self.nucelar_monitor:NucelarMonitorInterface = nucelar_monitor
|
|
|
|
key:str
|
|
|
|
self.__databases:dict[str, DatabasesInterface] = {}
|
|
|
|
for key in (
|
|
"default_databases_files", "databases_files",
|
|
"default_databases", "databases"
|
|
):
|
|
self.add(self.nucelar_monitor.settings.get(key, None, []), True)
|
|
|
|
def get(self:Self, key:str) -> type[DatabasesInterface]|None:
|
|
return self.__databases.get(key, None)
|
|
|
|
def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
|
|
subinputs:dict[str, type[DatabasesInterface]|dict[str, Any|None]]
|
|
|
|
for subinputs in self.nucelar_monitor.files.load_json(inputs):
|
|
|
|
key:str
|
|
database:DatabasesInterface|dict[str, Any|None]
|
|
|
|
for key, database in subinputs.items():
|
|
if isinstance(database, dict):
|
|
database = self.nucelar_monitor.models.get(
|
|
DatabasesInterface,
|
|
self.nucelar_monitor.settings.get(("database_type", "type"), database, "sql_server")
|
|
)(self.nucelar_monitor, database)
|
|
if database is None:
|
|
continue
|
|
if database is not None and isinstance(database, DatabasesInterface) and (
|
|
overwrite or key not in self.__databases
|
|
):
|
|
self.__databases[key] = database
|
|
|
|
def close(self:Self) -> None:
|
|
|
|
database:type[DatabasesInterface]
|
|
|
|
for database in self.__databases.values():
|
|
database.close() |