Took out env vars extracting to config.py

This commit is contained in:
norohind 2021-11-28 16:57:55 +03:00
parent 803b6cce7c
commit d30bc56c9e
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
7 changed files with 38 additions and 22 deletions

View File

@ -38,7 +38,6 @@ To utilise logging in a 'found' (third-party) plugin, include this:
import inspect import inspect
import logging import logging
import logging.handlers import logging.handlers
import os
from contextlib import suppress from contextlib import suppress
from fnmatch import fnmatch from fnmatch import fnmatch
# So that any warning about accessing a protected member is only in one place. # 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 traceback import print_exc
from typing import TYPE_CHECKING, Tuple, cast from typing import TYPE_CHECKING, Tuple, cast
import config
# TODO: Tests: # 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(appcmdname))
return cast('LoggerMixin', logging.getLogger(__name__)) return cast('LoggerMixin', logging.getLogger(__name__))
# Singleton # 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__ base_logger_name = __name__

17
config.py Normal file
View File

@ -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()

View File

@ -1,14 +1,13 @@
from model.postgres_model import PostgresModel from model.postgres_model import PostgresModel
from model.sqlite_model import Sqlite3Model from model.sqlite_model import Sqlite3Model
from model.abstract_model import AbstractModel from model.abstract_model import AbstractModel
import config
import utils import utils
from EDMCLogging import get_main_logger from EDMCLogging import get_main_logger
import os
logger = get_main_logger() logger = get_main_logger()
env_choose = os.getenv('DB_NAME') env_choose = config.DBMS_name
model: AbstractModel model: AbstractModel

View File

@ -1,9 +1,9 @@
import json import json
import os
import typing import typing
import psycopg2.extensions import psycopg2.extensions
import psycopg2.extras import psycopg2.extras
import config
from .sqlite_cache import cache from .sqlite_cache import cache
from . import postgres_sql_requests from . import postgres_sql_requests
@ -20,11 +20,11 @@ class PostgresModel(AbstractModel):
def open_model(self): def open_model(self):
self.db: psycopg2.extensions.connection = psycopg2.connect( self.db: psycopg2.extensions.connection = psycopg2.connect(
user=os.environ['DB_USERNAME'], user=config.postgres_username,
password=os.environ['DB_PASSWORD'], password=config.postgres_password,
host=os.environ['DB_HOSTNAME'], host=config.postgres_hostname,
port=os.environ['DB_PORT'], port=config.postgres_port,
database=os.environ['DB_DATABASE'], database=config.postgres_database_name,
cursor_factory=psycopg2.extras.DictCursor) cursor_factory=psycopg2.extras.DictCursor)
logger.info(f'Connected to {self.db.dsn}') logger.info(f'Connected to {self.db.dsn}')

View File

@ -1,6 +1,6 @@
import sqlite3 import sqlite3
from EDMCLogging import get_main_logger from EDMCLogging import get_main_logger
import config
from typing import Union from typing import Union
logger = get_main_logger() logger = get_main_logger()
@ -14,7 +14,7 @@ class Cache:
return return
try: 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: except Exception as e:
logger.warning('Cannot create cache DB due to', exc_info=e) logger.warning('Cannot create cache DB due to', exc_info=e)
@ -66,4 +66,4 @@ class Cache:
self.db.execute('delete from cache;') self.db.execute('delete from cache;')
cache = Cache(True) cache = Cache(disabled=config.cache_disabled)

View File

@ -2,6 +2,7 @@ import sqlite3
import typing import typing
import json import json
import config
from . import sqlite_sql_requests from . import sqlite_sql_requests
from .abstract_model import AbstractModel from .abstract_model import AbstractModel
from .sqlite_cache import cache from .sqlite_cache import cache
@ -16,7 +17,7 @@ class Sqlite3Model(AbstractModel):
db: sqlite3.Connection db: sqlite3.Connection
def open_model(self): 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') logger.debug(f'Connected to squads_stat.sqlite3')

View File

@ -7,7 +7,7 @@ import psycopg2.extras
import sqlite3 import sqlite3
from model import postgres_sql_requests from model import postgres_sql_requests
import time import time
import os import config
insert_pg = """insert into squads_stats_states (action_id, leaderboard_type, platform, squadron_id, score, insert_pg = """insert into squads_stats_states (action_id, leaderboard_type, platform, squadron_id, score,
percentile, rank, name, tag, timestamp) 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)) sqlite_conn.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
pg_conn: psycopg2.extensions.connection = psycopg2.connect( pg_conn: psycopg2.extensions.connection = psycopg2.connect(
user=os.environ['DB_USERNAME'], user=config.postgres_username,
password=os.environ['DB_PASSWORD'], password=config.postgres_password,
host=os.environ['DB_HOSTNAME'], host=config.postgres_hostname,
port=os.environ['DB_PORT'], port=config.postgres_port,
database=os.environ['DB_DATABASE'], database=config.postgres_database_name,
) )
with pg_conn: with pg_conn: