Skip to content

Commit

Permalink
Merge pull request #1346 from rommapp/misc/fix-mypy-issues
Browse files Browse the repository at this point in the history
misc: Fix some typing issues
  • Loading branch information
adamantike authored Dec 8, 2024
2 parents 7624b0e + 743ecd8 commit f1ab581
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 69 deletions.
2 changes: 2 additions & 0 deletions backend/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def str_to_bool(value: str) -> bool:

# LOGGING
LOGLEVEL: Final = os.environ.get("LOGLEVEL", "INFO")
FORCE_COLOR: Final = str_to_bool(os.environ.get("FORCE_COLOR", "false"))
NO_COLOR: Final = str_to_bool(os.environ.get("NO_COLOR", "false"))

# TESTING
IS_PYTEST_RUN: Final = bool(os.environ.get("PYTEST_VERSION", False))
19 changes: 10 additions & 9 deletions backend/logger/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from colorama import Fore, Style, init
from config import FORCE_COLOR, NO_COLOR

RED = Fore.RED
GREEN = Fore.GREEN
Expand All @@ -10,12 +11,12 @@
BLUE = Fore.BLUE


def should_strip_ansi():
def should_strip_ansi() -> bool:
"""Determine if ANSI escape codes should be stripped."""
# Check if an explicit environment variable is set to control color behavior
if os.getenv("FORCE_COLOR", "false").lower() == "true":
if FORCE_COLOR:
return False
if os.getenv("NO_COLOR", "false").lower() == "true":
if NO_COLOR:
return True

# For other environments, strip colors if not a TTY
Expand All @@ -31,7 +32,7 @@ class Formatter(logging.Formatter):
Logger formatter.
"""

def format(self, record):
def format(self, record: logging.LogRecord) -> str:
"""
Formats a log record with color-coded output based on the log level.
Expand All @@ -41,8 +42,8 @@ def format(self, record):
Returns:
The formatted log record as a string.
"""
level: str = "%(levelname)s"
dots: str = f"{Fore.RESET}:"
level = "%(levelname)s"
dots = f"{Fore.RESET}:"
identifier = (
f"\t {Fore.BLUE}[RomM]{Fore.LIGHTMAGENTA_EX}[{str(record.module_name).lower()}]"
if hasattr(record, "module_name")
Expand All @@ -58,9 +59,9 @@ def format(self, record):
if hasattr(record, "module_name")
else f" {Fore.BLUE}[RomM]{Fore.LIGHTMAGENTA_EX}[%(module)s]"
)
msg: str = f"{Style.RESET_ALL}%(message)s"
date: str = f"{Fore.CYAN}[%(asctime)s] "
formats: dict = {
msg = f"{Style.RESET_ALL}%(message)s"
date = f"{Fore.CYAN}[%(asctime)s] "
formats = {
logging.DEBUG: f"{Fore.LIGHTMAGENTA_EX}{level}{dots}{identifier}{date}{msg}",
logging.INFO: f"{Fore.GREEN}{level}{dots}{identifier}{date}{msg}",
logging.WARNING: f"{Fore.YELLOW}{level}{dots}{identifier_warning}{date}{msg}",
Expand Down
45 changes: 31 additions & 14 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ def has_cover(self) -> bool:

@cached_property
def merged_screenshots(self) -> list[str]:
return [s.download_path for s in self.screenshots] + [
f"{FRONTEND_RESOURCES_PATH}/{s}" for s in self.path_screenshots
]
screenshots = [s.download_path for s in self.screenshots]
if self.path_screenshots:
screenshots += [
f"{FRONTEND_RESOURCES_PATH}/{s}" for s in self.path_screenshots
]
return screenshots

def get_collections(self) -> list[Collection]:
from handler.database import db_rom_handler
Expand All @@ -132,47 +135,61 @@ def get_collections(self) -> list[Collection]:
# Metadata fields
@property
def youtube_video_id(self) -> str:
return self.igdb_metadata.get("youtube_video_id", "")
if self.igdb_metadata:
return self.igdb_metadata.get("youtube_video_id", "")
return ""

@property
def alternative_names(self) -> list[str]:
return (
self.igdb_metadata.get("alternative_names", None)
or self.moby_metadata.get("alternate_titles", None)
(self.igdb_metadata or {}).get("alternative_names", None)
or (self.moby_metadata or {}).get("alternate_titles", None)
or []
)

@property
def first_release_date(self) -> int:
return self.igdb_metadata.get("first_release_date", 0)
if self.igdb_metadata:
return self.igdb_metadata.get("first_release_date", 0)
return 0

@property
def genres(self) -> list[str]:
return (
self.igdb_metadata.get("genres", None)
or self.moby_metadata.get("genres", None)
(self.igdb_metadata or {}).get("genres", None)
or (self.moby_metadata or {}).get("genres", None)
or []
)

@property
def franchises(self) -> list[str]:
return self.igdb_metadata.get("franchises", [])
if self.igdb_metadata:
return self.igdb_metadata.get("franchises", [])
return []

@property
def collections(self) -> list[str]:
return self.igdb_metadata.get("collections", [])
if self.igdb_metadata:
return self.igdb_metadata.get("collections", [])
return []

@property
def companies(self) -> list[str]:
return self.igdb_metadata.get("companies", [])
if self.igdb_metadata:
return self.igdb_metadata.get("companies", [])
return []

@property
def game_modes(self) -> list[str]:
return self.igdb_metadata.get("game_modes", [])
if self.igdb_metadata:
return self.igdb_metadata.get("game_modes", [])
return []

@property
def age_ratings(self) -> list[str]:
return [r["rating"] for r in self.igdb_metadata.get("age_ratings", [])]
if self.igdb_metadata:
return [r["rating"] for r in self.igdb_metadata.get("age_ratings", [])]
return []

@property
def fs_resources_path(self) -> str:
Expand Down
55 changes: 12 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ pydash = "^7.0.7"
mariadb = "1.1.10"
rq = "^1.16.1"
redis = "^5.0"
types-pyyaml = "^6.0.12.20240311"
types-redis = "^4.6.0.20240311 "
passlib = { extras = ["bcrypt"], version = "^1.7.4" }
itsdangerous = "^2.1.2"
rq-scheduler = "^0.13.1"
starlette-csrf = "^3.0.0"
httpx = "^0.27.0"
python-multipart = "^0.0.18"
types-passlib = "^1.7.7.20240311"
watchdog = "^4.0.0"
yarl = "^1.14"
joserfc = "^0.9.0"
Expand All @@ -47,6 +44,10 @@ py7zr = { git = "https://github.com/adamantike/py7zr.git", rev = "54b68426" }
streaming-form-data = "^1.16.0"
zipfile-deflate64 = "^0.2.0"
colorama = "^0.4.6"
types-colorama = "^0.4"
types-passlib = "^1.7.7.20240311"
types-pyyaml = "^6.0.12.20240311"
types-redis = "^4.6.0.20240311 "

[tool.poetry.group.test.dependencies]
fakeredis = "^2.21.3"
Expand Down

0 comments on commit f1ab581

Please sign in to comment.