|
4 | 4 |
|
5 | 5 | from config import LIBRARY_BASE_PATH |
6 | 6 | from config.config_manager import config_manager as cm |
7 | | -from exceptions.fs_exceptions import ( |
8 | | - FolderStructureNotMatchException, |
9 | | - PlatformAlreadyExistsException, |
10 | | -) |
| 7 | +from exceptions.fs_exceptions import PlatformAlreadyExistsException |
| 8 | +from logger.logger import log |
11 | 9 |
|
12 | 10 | from .base_handler import FSHandler, LibraryStructure |
13 | 11 |
|
@@ -59,9 +57,9 @@ def get_platforms_directory(self) -> str: |
59 | 57 | cnfg = cm.get_config() |
60 | 58 |
|
61 | 59 | # Fallback to config hint when detection is inconclusive: default to |
62 | | - # Structure A (roms/{platform}) so a malformed library fails loudly |
63 | | - # (FolderStructureNotMatchException) rather than treating the bare |
64 | | - # library root as a flat list of platforms. |
| 60 | + # Structure A (roms/{platform}) so the bare library root is not treated |
| 61 | + # as a flat list of platforms. When the roms folder is missing entirely, |
| 62 | + # get_platforms() bootstraps it instead of failing. |
65 | 63 | return "" if cnfg.has_structure_path_b else cnfg.ROMS_FOLDER_NAME |
66 | 64 |
|
67 | 65 | def get_platform_fs_structure(self, fs_slug: str) -> str: |
@@ -90,15 +88,31 @@ async def add_platform(self, fs_slug: str) -> None: |
90 | 88 | async def get_platforms(self) -> list[str]: |
91 | 89 | """Retrieves all platforms from the filesystem. |
92 | 90 |
|
| 91 | + If no library structure exists yet (neither Structure A's top-level roms |
| 92 | + folder nor a Structure B {platform}/roms folder), defaults to Structure A |
| 93 | + by creating the roms folder and returns an empty list, so RomM starts |
| 94 | + cleanly with an empty library instead of failing. |
| 95 | +
|
93 | 96 | Returns: |
94 | 97 | List of platform slugs. |
95 | 98 | """ |
96 | 99 | cnfg = cm.get_config() |
97 | 100 |
|
98 | 101 | try: |
99 | 102 | platforms = await self.list_directories(path=self.get_platforms_directory()) |
100 | | - except FileNotFoundError as e: |
101 | | - raise FolderStructureNotMatchException() from e |
| 103 | + except FileNotFoundError: |
| 104 | + # The platforms directory does not exist, which means no library |
| 105 | + # structure has been set up yet. Bootstrap Structure A so the |
| 106 | + # filesystem is in a valid state and report an empty library. |
| 107 | + log.warning( |
| 108 | + "No library structure found; creating default Structure A " |
| 109 | + "(roms folder) and starting with an empty library." |
| 110 | + ) |
| 111 | + try: |
| 112 | + self.create_library_structure() |
| 113 | + except OSError: |
| 114 | + log.error("Failed to create default library structure", exc_info=True) |
| 115 | + return [] |
102 | 116 |
|
103 | 117 | # For Structure B, only include directories that have a roms subfolder |
104 | 118 | structure = self.detect_library_structure() |
|
0 commit comments