mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
Centralize the *_no_tags / *_no_ext / *_extension columns (derived from a file name) behind @validates hooks instead of computing them by hand at every write site: - Add pure helpers (compute_file_name_parts and friends) to models.base; the filesystem base handler now delegates to them. - Add @validates on Rom (fs_name), BaseAsset (file_name, inherited by all asset subclasses), and Firmware (file_name). - update_rom keeps the fs_name-derived columns in sync on bulk update(), which also fixes the rename path never updating fs_extension. - Drop the now-redundant computations at the scan/rename call sites. Also fix the migration backfill loop and a pre-existing list[str | None] type mismatch surfaced in scan_handler. Add tests for the helpers, the validators, and the update_rom bulk-sync path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
import re
|
|
from datetime import datetime, timezone
|
|
from typing import NamedTuple
|
|
|
|
from sqlalchemy import TIMESTAMP
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
FILE_NAME_MAX_LENGTH = 450
|
|
FILE_PATH_MAX_LENGTH = 1000
|
|
FILE_EXTENSION_MAX_LENGTH = 100
|
|
|
|
# Matches parenthesised/bracketed tag groups, e.g. "(USA)" or "[!]".
|
|
TAG_REGEX = re.compile(r"\(([^)]+)\)|\[([^]]+)\]")
|
|
# Matches a trailing file extension, including multi-part ones like ".tar.gz".
|
|
EXTENSION_REGEX = re.compile(r"\.(([a-z]+\.)*\w+)$")
|
|
|
|
|
|
def utc_now() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
def compute_file_name_no_ext(file_name: str) -> str:
|
|
"""Strip the trailing extension from a file name."""
|
|
return EXTENSION_REGEX.sub("", file_name).strip()
|
|
|
|
|
|
def compute_file_name_no_tags(file_name: str) -> str:
|
|
"""Strip the extension and any trailing tag groups from a file name."""
|
|
return TAG_REGEX.split(compute_file_name_no_ext(file_name))[0].strip()
|
|
|
|
|
|
def compute_file_extension(file_name: str) -> str:
|
|
"""Return the file's extension (without the leading dot), or ""."""
|
|
match = EXTENSION_REGEX.search(file_name)
|
|
return match.group(1) if match else ""
|
|
|
|
|
|
class FileNameParts(NamedTuple):
|
|
no_tags: str
|
|
no_ext: str
|
|
extension: str
|
|
|
|
|
|
def compute_file_name_parts(file_name: str) -> FileNameParts:
|
|
"""Precompute the derived columns stored alongside a file name.
|
|
|
|
These mirror the `*_no_tags` / `*_no_ext` / `*_extension` columns on
|
|
`Rom`, the asset models, and `Firmware`, which are kept in sync via a
|
|
`@validates` hook on the source name column.
|
|
"""
|
|
return FileNameParts(
|
|
no_tags=compute_file_name_no_tags(file_name),
|
|
no_ext=compute_file_name_no_ext(file_name),
|
|
extension=compute_file_extension(file_name),
|
|
)
|
|
|
|
|
|
class BaseModel(DeclarativeBase):
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
TIMESTAMP(timezone=True), default=utc_now
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
TIMESTAMP(timezone=True),
|
|
default=utc_now,
|
|
onupdate=utc_now,
|
|
)
|