diff --git a/EDMCLogging.py b/EDMCLogging.py index c54b060..e15f645 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -38,7 +38,6 @@ To utilise logging in a 'found' (third-party) plugin, include this: import inspect import logging import logging.handlers -import os from contextlib import suppress from fnmatch import fnmatch # So that any warning about accessing a protected member is only in one place. @@ -47,6 +46,7 @@ from threading import get_native_id as thread_native_id from traceback import print_exc from typing import TYPE_CHECKING, Tuple, cast +import config # TODO: Tests: # @@ -438,9 +438,8 @@ def get_main_logger(sublogger_name: str = '') -> 'LoggerMixin': # return cast('LoggerMixin', logging.getLogger(appcmdname)) return cast('LoggerMixin', logging.getLogger(__name__)) - # Singleton -loglevel = logging._nameToLevel.get(os.getenv('LOG_LEVEL', 'DEBUG').upper(), logging.DEBUG) # noqa: +loglevel = logging._nameToLevel.get(config.log_level, logging.DEBUG) # noqa: base_logger_name = __name__ diff --git a/config.py b/config.py new file mode 100644 index 0000000..c53bba3 --- /dev/null +++ b/config.py @@ -0,0 +1,17 @@ +import os + +cache_disabled: bool = bool(os.getenv('CACHE_DISABLED', True)) + +DBMS_name = os.getenv('DB_NAME') + +postgres_username = os.getenv('DB_USERNAME') +postgres_password = os.getenv('DB_PASSWORD') +postgres_hostname = os.getenv('DB_HOSTNAME') +postgres_port = os.getenv('DB_PORT') +postgres_database_name = os.getenv('DB_DATABASE') + +model_cache_db_path = os.getenv('CACHE_PATH', 'squads_stat_cache.sqlite3') + +sqlite_db_path = os.getenv('SQLITE_DB_PATH', 'squads_stat.sqlite3') + +log_level = os.getenv('LOG_LEVEL', 'DEBUG').upper() \ No newline at end of file diff --git a/model/__init__.py b/model/__init__.py index 5494971..542df85 100644 --- a/model/__init__.py +++ b/model/__init__.py @@ -1,14 +1,13 @@ from model.postgres_model import PostgresModel from model.sqlite_model import Sqlite3Model from model.abstract_model import AbstractModel - +import config import utils from EDMCLogging import get_main_logger -import os logger = get_main_logger() -env_choose = os.getenv('DB_NAME') +env_choose = config.DBMS_name model: AbstractModel diff --git a/model/postgres_model.py b/model/postgres_model.py index c93d814..faa30ef 100644 --- a/model/postgres_model.py +++ b/model/postgres_model.py @@ -1,9 +1,9 @@ import json -import os import typing import psycopg2.extensions import psycopg2.extras +import config from .sqlite_cache import cache from . import postgres_sql_requests @@ -20,11 +20,11 @@ class PostgresModel(AbstractModel): def open_model(self): self.db: psycopg2.extensions.connection = psycopg2.connect( - user=os.environ['DB_USERNAME'], - password=os.environ['DB_PASSWORD'], - host=os.environ['DB_HOSTNAME'], - port=os.environ['DB_PORT'], - database=os.environ['DB_DATABASE'], + user=config.postgres_username, + password=config.postgres_password, + host=config.postgres_hostname, + port=config.postgres_port, + database=config.postgres_database_name, cursor_factory=psycopg2.extras.DictCursor) logger.info(f'Connected to {self.db.dsn}') diff --git a/model/sqlite_cache.py b/model/sqlite_cache.py index 03e0f2e..17110f6 100644 --- a/model/sqlite_cache.py +++ b/model/sqlite_cache.py @@ -1,6 +1,6 @@ import sqlite3 from EDMCLogging import get_main_logger - +import config from typing import Union logger = get_main_logger() @@ -14,7 +14,7 @@ class Cache: return try: - self.db: sqlite3.Connection = sqlite3.connect('squads_stat_cache.sqlite3', check_same_thread=False) + self.db: sqlite3.Connection = sqlite3.connect(config.model_cache_db_path, check_same_thread=False) except Exception as e: logger.warning('Cannot create cache DB due to', exc_info=e) @@ -66,4 +66,4 @@ class Cache: self.db.execute('delete from cache;') -cache = Cache(True) +cache = Cache(disabled=config.cache_disabled) diff --git a/model/sqlite_model.py b/model/sqlite_model.py index e4a5c9f..f7175fb 100644 --- a/model/sqlite_model.py +++ b/model/sqlite_model.py @@ -2,6 +2,7 @@ import sqlite3 import typing import json +import config from . import sqlite_sql_requests from .abstract_model import AbstractModel from .sqlite_cache import cache @@ -16,7 +17,7 @@ class Sqlite3Model(AbstractModel): db: sqlite3.Connection def open_model(self): - self.db = sqlite3.connect('squads_stat.sqlite3', check_same_thread=False) + self.db = sqlite3.connect(config.sqlite_db_path, check_same_thread=False) logger.debug(f'Connected to squads_stat.sqlite3') diff --git a/sqlite2postgres.py b/sqlite2postgres.py index 0682d1d..b5df562 100644 --- a/sqlite2postgres.py +++ b/sqlite2postgres.py @@ -7,7 +7,7 @@ import psycopg2.extras import sqlite3 from model import postgres_sql_requests import time -import os +import config insert_pg = """insert into squads_stats_states (action_id, leaderboard_type, platform, squadron_id, score, percentile, rank, name, tag, timestamp) @@ -19,11 +19,11 @@ sqlite_conn: sqlite3.Connection = sqlite3.connect('squads_stat.sqlite3', check_s sqlite_conn.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r)) pg_conn: psycopg2.extensions.connection = psycopg2.connect( - user=os.environ['DB_USERNAME'], - password=os.environ['DB_PASSWORD'], - host=os.environ['DB_HOSTNAME'], - port=os.environ['DB_PORT'], - database=os.environ['DB_DATABASE'], + user=config.postgres_username, + password=config.postgres_password, + host=config.postgres_hostname, + port=config.postgres_port, + database=config.postgres_database_name, ) with pg_conn: