Files
romm/backend/handler/filesystem/platforms_handler.py
Georges-Antoine Assi a7ecfd96a9 feat!: make filesystem.structure the single library layout
`filesystem.structure` now spells out the whole layout rather than only the part
below a platform folder. A template is relative to the library root, `{platform}`
marks the platform folder, and `{game}` marks where a game begins.

Merge `{gameFile}` and `{gameDir}` into one `{game}` terminal. The information a
terminal carried was the depth, not the kind: at a declared level a file is a
game of its own and a folder is one multi-file game, which a stat answers. That
drops the two-template idiom for a level holding both, and `_discover_default_roms`
with it, since the default scan is now just `roms/{platform}/{game}`.

Retire the auto-detected `{platform}/roms` layout, along with
`filesystem.roms_folder` and `filesystem.firmware_folder`, which named one path
segment each that a template now contains. Both are refused at startup with the
template that reproduces the layout, so an instance that relied on either says so
instead of scanning an empty library and marking every rom missing. Firmware gets
its own `structure.firmware` key rather than following the rom layout implicitly.

`get_roms_fs_structure` keeps meaning the folder a platform's games start in, so
the `.physical` sentinel paths already in the database still resolve. Per-platform
overrides have to agree with `default` on where the platform folder sits;
relocating one platform needs platform discovery and the watcher to union several
roots, which is left out.

The setup wizard reports `library_ready` plus the configured template instead of
a `struct_a`/`struct_b` discriminator, so the banner shows the layout in force
rather than guessing it client-side.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 08:39:21 -04:00

86 lines
2.9 KiB
Python

import fnmatch
import os
from config import LIBRARY_BASE_PATH
from config.config_manager import config_manager as cm
from exceptions.fs_exceptions import PlatformAlreadyExistsException
from logger.logger import log
from .base_handler import FSHandler
class FSPlatformsHandler(FSHandler):
def __init__(self) -> None:
super().__init__(base_path=LIBRARY_BASE_PATH)
def _exclude_platforms(self, platforms: list):
cnfg = cm.get_config()
return [
platform
for platform in platforms
if not any(
platform == excluded or fnmatch.fnmatch(platform, excluded)
for excluded in cnfg.EXCLUDED_PLATFORMS
)
]
def create_library_structure(self) -> None:
"""Creates the folder the configured structure enumerates platforms in."""
os.makedirs(
os.path.join(LIBRARY_BASE_PATH, self.get_platforms_directory()),
exist_ok=True,
)
def library_structure_exists(self) -> bool:
"""Whether the folder platforms are enumerated in is present on disk."""
return os.path.isdir(
os.path.join(LIBRARY_BASE_PATH, self.get_platforms_directory())
)
def get_platforms_directory(self) -> str:
return cm.get_config().platforms_dir
def get_platform_fs_structure(self, fs_slug: str) -> str:
"""The folder a platform's games start in, relative to the library root."""
return cm.get_config().default_structure.games_dir(fs_slug)
async def add_platform(self, fs_slug: str) -> None:
"""Adds platform to the filesystem
Args:
fs_slug: platform slug
"""
platform_path = self.get_platform_fs_structure(fs_slug)
try:
await self.make_directory(platform_path)
except FileNotFoundError as e:
raise PlatformAlreadyExistsException(fs_slug) from e
async def get_platforms(self) -> list[str]:
"""Retrieves all platforms from the filesystem.
Returns:
List of platform slugs.
"""
try:
platforms = await self.list_directories(path=self.get_platforms_directory())
except FileNotFoundError:
# Bootstrap the configured folder so the filesystem is in a valid
# state and report an empty library, rather than failing.
log.warning(
"No library structure found; creating the configured platforms "
"folder and starting with an empty library."
)
try:
self.create_library_structure()
except OSError:
log.error("Failed to create default library structure", exc_info=True)
return []
# Exclude before touching the directories so unreadable system folders
# (e.g. Synology's #recycle) are never stat'ed
platforms = self._exclude_platforms(platforms)
return platforms