From b9de7f605d97106dd83a3ecccf86fbe958448f2b Mon Sep 17 00:00:00 2001 From: zurdi zurdo Date: Wed, 29 Mar 2023 19:39:15 +0200 Subject: [PATCH] config file for exclude added --- .gitignore | 5 +++- backend/requirements.txt | 3 +- backend/src/config/config.py | 14 ++++++--- backend/src/models/platform.py | 4 +-- backend/src/utils/fs.py | 55 +++++++++++++--------------------- changelog.md | 4 ++- 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index ea62aef3b..a47e7fe79 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,7 @@ envs.env mariadb # data test -library \ No newline at end of file +library + +# config test +romm \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index 5e3007f53..d932e3c95 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -2,4 +2,5 @@ requests==2.28.2 fastapi==0.92.0 uvicorn==0.20.0 mariadb==1.1.6 -SQLAlchemy==2.0.7 \ No newline at end of file +SQLAlchemy==2.0.7 +PyYAML==6.0 \ No newline at end of file diff --git a/backend/src/config/config.py b/backend/src/config/config.py index f3ffa72e0..971e49294 100644 --- a/backend/src/config/config.py +++ b/backend/src/config/config.py @@ -1,8 +1,10 @@ import os import sys import pathlib - +import yaml +from yaml.loader import SafeLoader from urllib.parse import quote_plus + from logger.logger import log # Uvicorn @@ -11,9 +13,14 @@ DEV_HOST: str = "0.0.0.0" # PATHS LIBRARY_BASE_PATH: str = f"{pathlib.Path(__file__).parent.parent.parent.parent.resolve()}/library" +ROMM_USER_CONFIG_PATH: str = f"{pathlib.Path(__file__).parent.parent.parent.parent.resolve()}/romm/config.yml" +try: + with open(ROMM_USER_CONFIG_PATH) as config: config = yaml.load(config, Loader=SafeLoader) +except FileNotFoundError: + config = None +user_config: dict = {} if not config else config -DEFAULT_URL_LOGO: str = "https://images.igdb.com/igdb/image/upload/t_cover_big/nocover.png" -DEFAULT_PATH_LOGO: str = f"/assets/library/resources/default/logo_l.png" +RESERVED_FOLDERS: list = ['resources', 'database'] DEFAULT_URL_COVER_L: str = "https://images.igdb.com/igdb/image/upload/t_cover_big/nocover.png" DEFAULT_PATH_COVER_L: str = f"/assets/library/resources/default/cover_l.png" @@ -27,7 +34,6 @@ CLIENT_SECRET: str = os.getenv('CLIENT_SECRET') STEAMGRIDDB_API_KEY: str = os.getenv('STEAMGRIDDB_API_KEY') -RESERVED_FOLDERS: list = ['resources', 'database'] # DB DRIVERS diff --git a/backend/src/models/platform.py b/backend/src/models/platform.py index 113da4fd6..7e2bf1841 100644 --- a/backend/src/models/platform.py +++ b/backend/src/models/platform.py @@ -1,6 +1,6 @@ from sqlalchemy import Column, String, Integer, Text -from config.config import DEFAULT_PATH_LOGO +from config.config import DEFAULT_PATH_COVER from models.base import BaseModel @@ -10,5 +10,5 @@ class Platform(BaseModel): sgdb_id = Column(String(length=50), default="") slug = Column(String(length=100), primary_key=True) name = Column(String(length=350), default="") - path_logo = Column(Text, default=DEFAULT_PATH_LOGO) + path_logo = Column(Text, default=DEFAULT_PATH_COVER) n_roms = Column(Integer, default=0) diff --git a/backend/src/utils/fs.py b/backend/src/utils/fs.py index c91717c03..6f55e5b90 100644 --- a/backend/src/utils/fs.py +++ b/backend/src/utils/fs.py @@ -5,7 +5,7 @@ from pathlib import Path import requests from fastapi import HTTPException -from config.config import LIBRARY_BASE_PATH, RESERVED_FOLDERS, DEFAULT_URL_LOGO, DEFAULT_URL_COVER_L, DEFAULT_PATH_COVER_L, DEFAULT_URL_COVER_S, DEFAULT_PATH_COVER_S +from config.config import user_config, LIBRARY_BASE_PATH, RESERVED_FOLDERS, DEFAULT_URL_COVER_L, DEFAULT_PATH_COVER_L, DEFAULT_URL_COVER_S, DEFAULT_PATH_COVER_S from logger.logger import log @@ -16,8 +16,6 @@ def store_default_resources(overwrite: bool) -> None: Args: overwrite: flag to overwrite or not default resources """ - if overwrite or not p_logo_exists('default'): - store_p_logo('default', DEFAULT_URL_LOGO) if overwrite or not r_cover_exists('default', 'cover', 'l'): store_r_cover('default', 'cover', DEFAULT_URL_COVER_L, 'l') if overwrite or not r_cover_exists('default', 'cover', 's'): @@ -25,37 +23,6 @@ def store_default_resources(overwrite: bool) -> None: # ========= Platforms utils ========= -def p_logo_exists(slug: str) -> bool: - """Check if platform logo exists in filesystem - - Args: - slug: shor name of the platform - Returns - True if logo exists in filesystem else False - """ - logo_path: str = f"{LIBRARY_BASE_PATH}/resources/{slug}/logo.png" - return True if os.path.exists(logo_path) else False - - -def store_p_logo(slug: str, url_logo: str) -> None: - """Store platform resources in filesystem - - Args: - slug: shor name of the platform - url_logo: url to get logo - """ - logo_file: str = f"logo.png" - logo_path: str = f"{LIBRARY_BASE_PATH}/resources/{slug}" - res = requests.get(url_logo, stream=True) - if res.status_code == 200: - Path(logo_path).mkdir(parents=True, exist_ok=True) - with open(f"{logo_path}/{logo_file}", 'wb') as f: - shutil.copyfileobj(res.raw, f) - log.info(f"{slug} logo downloaded successfully!") - else: - log.warning(f"{slug} logo couldn't be downloaded") - - def get_platforms() -> list[str]: """Gets all filesystem platforms @@ -68,6 +35,14 @@ def get_platforms() -> list[str]: else: platforms: list[str] = list(os.walk(LIBRARY_BASE_PATH))[0][1] [platforms.remove(reserved) for reserved in RESERVED_FOLDERS if reserved in platforms] + try: + excluded_folders: list = user_config['exclude']['platforms'] + try: + [platforms.remove(excluded) for excluded in excluded_folders if excluded in platforms] + except TypeError: + pass + except KeyError: + pass log.info(f"filesystem platforms found: {platforms}") return platforms except IndexError: @@ -155,6 +130,18 @@ def get_roms(p_slug: str, only_amount: bool = False) -> list[dict]: else: roms_path: str = f"{LIBRARY_BASE_PATH}/{p_slug}/roms" roms_filename = list(os.walk(f"{LIBRARY_BASE_PATH}/{p_slug}/roms"))[0][2] + try: + try: + excluded_files: list = user_config['exclude']['rom_files'] + filtered_files: list = [] + for filename in roms_filename: + if filename.split('.')[-1] in excluded_files: + filtered_files.append(filename) + roms_filename = [f for f in roms_filename if f not in filtered_files] + except TypeError: + pass + except KeyError: + pass if only_amount: return len(roms_filename) [roms.append({'filename': rom, 'size': str(round(os.stat(f"{roms_path}/{rom}").st_size / (1024 * 1024), 2))}) for rom in roms_filename] log.info(f"filesystem roms found for {p_slug}: {roms}") diff --git a/changelog.md b/changelog.md index 6dce9140c..a1009c66e 100644 --- a/changelog.md +++ b/changelog.md @@ -6,7 +6,7 @@ In order to make the new folder structure to work, it is mandatory this time to I apologize for the inconveniences this may cause, as this is a new software, it may change a little bit the first weeks, at least until I can develop a proper way to migrate between versions. I hope you can understand these initial wipes in order to make a better tool. ## Added - - Now RomM folder structure is more flexible to match two different patrons by priority: + - Now RomM folder structure is more flexible to match two different patrons by priority. This change makes RomM Emudeck compatible at least with single file games platforms: - Structure 1 (priority high) - roms folder at root of library folder: ``` library/ @@ -42,6 +42,8 @@ I apologize for the inconveniences this may cause, as this is a new software, it │ ├─ rom_1.gb ``` + - Config file support to exclude folders and specific extension files to be scanned. To reload config file RomM reload is needed. Check config section. + # v1.4.1 (_29-03-2023_) ## Added