Re-add config folder

This commit is contained in:
Georges-Antoine Assi
2023-05-09 10:31:09 -04:00
parent dece49ba7c
commit ca21cebed5
4 changed files with 111 additions and 10 deletions

View File

@@ -2,15 +2,12 @@ import sys
from pathlib import Path
from logging.config import fileConfig
from sqlalchemy import create_engine
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
from config.config_loader import ConfigLoader
cl = ConfigLoader()
from alembic import context
cl = ConfigLoader()
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

View File

@@ -0,0 +1,31 @@
import os
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
# Uvicorn
DEV_PORT: int = 5000
DEV_HOST: str = "0.0.0.0"
# PATHS
LIBRARY_BASE_PATH: str = "/romm/library"
ROMM_USER_CONFIG_PATH: str = "/romm/config.yml"
SQLITE_DB_BASE_PATH: str = "/romm/database"
RESOURCES_BASE_PATH: str = "/romm/resources"
HIGH_PRIO_STRUCTURE_PATH: str = f"{LIBRARY_BASE_PATH}/roms"
# DEFAULT RESOURCES
DEFAULT_URL_COVER_L: str = "https://images.igdb.com/igdb/image/upload/t_cover_big/nocover.png"
DEFAULT_PATH_COVER_L: str = f"{RESOURCES_BASE_PATH}/default/default/cover/big.png"
DEFAULT_URL_COVER_S: str = "https://images.igdb.com/igdb/image/upload/t_cover_small/nocover.png"
DEFAULT_PATH_COVER_S: str = f"{RESOURCES_BASE_PATH}/default/default/cover/small.png"
# IGDB
CLIENT_ID: str = os.environ.get('CLIENT_ID')
CLIENT_SECRET: str = os.environ.get('CLIENT_SECRET')
# STEAMGRIDDB
STEAMGRIDDB_API_KEY: str = os.environ.get('STEAMGRIDDB_API_KEY')
# DB DRIVERS
SUPPORTED_DB_DRIVERS: list = ['sqlite', 'mariadb']
ROMM_DB_DRIVER: str = os.environ.get('ROMM_DB_DRIVER', 'sqlite')

View File

@@ -0,0 +1,77 @@
import os
import sys
import yaml
from yaml.loader import SafeLoader
from urllib.parse import quote_plus
from config import ROMM_DB_DRIVER, SUPPORTED_DB_DRIVERS, SQLITE_DB_BASE_PATH, ROMM_USER_CONFIG_PATH
from logger.logger import log
class ConfigLoader:
def __init__(self):
try:
with open(ROMM_USER_CONFIG_PATH) as config_file: self.config: dict = yaml.load(config_file, Loader=SafeLoader)
except FileNotFoundError:
self.config: dict = {}
self._parse_config()
@staticmethod
def get_db_engine():
if ROMM_DB_DRIVER in SUPPORTED_DB_DRIVERS:
if ROMM_DB_DRIVER == 'mariadb':
DB_HOST: str = os.environ.get('DB_HOST')
try:
DB_PORT: int = int(os.environ.get('DB_PORT'))
except TypeError:
log.critical("DB_PORT variable not set properly")
sys.exit(3)
DB_USER: str = os.environ.get('DB_USER')
DB_PASSWD: str = os.environ.get('DB_PASSWD')
DB_NAME: str = os.environ.get('DB_NAME', 'romm')
return f"mariadb+mariadbconnector://{DB_USER}:%s@{DB_HOST}:{DB_PORT}/{DB_NAME}" % quote_plus(DB_PASSWD)
elif ROMM_DB_DRIVER == 'sqlite':
if not os.path.exists(SQLITE_DB_BASE_PATH): os.makedirs(SQLITE_DB_BASE_PATH)
return f"sqlite:////{SQLITE_DB_BASE_PATH}/romm.db"
else:
log.critical(f"{ROMM_DB_DRIVER} database not supported")
sys.exit(3)
def _parse_config(self) -> dict:
try:
self.config['EXCLUDED_PLATFORMS'] = self.config['exclude']['platforms'] if self.config['exclude']['platforms'] else []
except KeyError:
self.config['EXCLUDED_PLATFORMS'] = []
try:
self.config['EXCLUDED_SINGLE_EXT'] = self.config['exclude']['roms']['single_file']['extensions'] if self.config['exclude']['roms']['single_file']['extensions'] else []
except KeyError:
self.config['EXCLUDED_SINGLE_EXT'] = []
try:
self.config['EXCLUDED_SINGLE_FILES'] = self.config['exclude']['roms']['single_file']['names'] if self.config['exclude']['roms']['single_file']['names'] else []
except KeyError:
self.config['EXCLUDED_SINGLE_FILES'] = []
try:
self.config['EXCLUDED_MULTI_FILES'] = self.config['exclude']['roms']['multi_file']['names'] if self.config['exclude']['roms']['multi_file']['names'] else []
except KeyError:
self.config['EXCLUDED_MULTI_FILES'] = []
try:
self.config['EXCLUDED_MULTI_PARTS_EXT'] = self.config['exclude']['roms']['multi_file']['parts']['extensions'] if self.config['exclude']['roms']['multi_file']['parts']['extensions'] else []
except KeyError:
self.config['EXCLUDED_MULTI_PARTS_EXT'] = []
try:
self.config['EXCLUDED_MULTI_PARTS_FILES'] = self.config['exclude']['roms']['multi_file']['parts']['names'] if self.config['exclude']['roms']['multi_file']['parts']['names'] else []
except KeyError:
self.config['EXCLUDED_MULTI_PARTS_FILES'] = []
try:
self.config['PLATFORMS_BINDING'] = self.config['system']['platforms'] if self.config['system']['platforms'] else {}
except KeyError:
self.config['PLATFORMS_BINDING'] = {}

View File

@@ -1,10 +1,8 @@
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
from config import DEV_PORT, DEV_HOST
from endpoints import scan, search, platform, rom
app = FastAPI()
@@ -26,6 +24,4 @@ def startup() -> None:
pass
if __name__ == '__main__':
from config import DEV_PORT, DEV_HOST
uvicorn.run("main:app", host=DEV_HOST, port=DEV_PORT, reload=True)