mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-22 17:47:48 -06:00
The wiki, the platforms README and two comments named common.py, and the installer's region table named scripts/region.py. tools.md lists core/, model/ and resolve/ where it listed common.py, and the testing guide names test_layers. install.py is rebuilt from installer/ and its pins rewritten.
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
"""Scraper plugin discovery module.
|
|
|
|
Auto-detects *_scraper.py files and exposes their scrapers.
|
|
Each scraper module must define:
|
|
PLATFORM_NAME: str
|
|
Scraper: class inheriting BaseScraper
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import pkgutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Scrapers run both as `python -m scripts.scraper.x` and as plain scripts, and
|
|
# they share helpers with the rest of scripts/. Putting that directory on the
|
|
# path once here is what lets every submodule say `from core.cli import ...`
|
|
# instead of carrying its own bootstrap.
|
|
if (_SCRIPTS_DIR := str(Path(__file__).resolve().parent.parent)) not in sys.path:
|
|
sys.path.insert(0, _SCRIPTS_DIR)
|
|
|
|
from .base_scraper import BaseScraper
|
|
|
|
_scrapers: dict[str, type] = {}
|
|
|
|
|
|
def discover_scrapers() -> dict[str, type]:
|
|
"""Auto-discover all *_scraper.py modules and return {platform_name: ScraperClass}."""
|
|
if _scrapers:
|
|
return _scrapers
|
|
|
|
package_dir = Path(__file__).parent
|
|
|
|
for _finder, name, _ispkg in pkgutil.iter_modules([str(package_dir)]):
|
|
if not name.endswith("_scraper"):
|
|
continue
|
|
|
|
module = importlib.import_module(f".{name}", package=__package__)
|
|
|
|
platform_name = getattr(module, "PLATFORM_NAME", None)
|
|
scraper_class = getattr(module, "Scraper", None)
|
|
|
|
if platform_name and scraper_class and issubclass(scraper_class, BaseScraper):
|
|
_scrapers[platform_name] = scraper_class
|
|
|
|
return _scrapers
|
|
|
|
|
|
def get_scraper(platform_name: str) -> BaseScraper | None:
|
|
"""Get an instantiated scraper for a platform."""
|
|
scrapers = discover_scrapers()
|
|
cls = scrapers.get(platform_name)
|
|
return cls() if cls else None
|