mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-04-16 05:42:31 -05:00
88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
"""Exporter for RetroBat batocera-systems.json format."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from .base_exporter import BaseExporter
|
|
|
|
|
|
class Exporter(BaseExporter):
|
|
"""Export truth data to RetroBat batocera-systems.json format."""
|
|
|
|
@staticmethod
|
|
def platform_name() -> str:
|
|
return "retrobat"
|
|
|
|
def export(
|
|
self,
|
|
truth_data: dict,
|
|
output_path: str,
|
|
scraped_data: dict | None = None,
|
|
) -> None:
|
|
native_map: dict[str, str] = {}
|
|
if scraped_data:
|
|
for sys_id, sys_data in scraped_data.get("systems", {}).items():
|
|
nid = sys_data.get("native_id")
|
|
if nid:
|
|
native_map[sys_id] = nid
|
|
|
|
output: dict[str, dict] = {}
|
|
|
|
systems = truth_data.get("systems", {})
|
|
for sys_id in sorted(systems):
|
|
sys_data = systems[sys_id]
|
|
files = sys_data.get("files", [])
|
|
if not files:
|
|
continue
|
|
|
|
native_id = native_map.get(sys_id, sys_id)
|
|
bios_files: list[dict] = []
|
|
|
|
for fe in files:
|
|
name = fe.get("name", "")
|
|
if name.startswith("_"):
|
|
continue
|
|
dest = fe.get("destination", name)
|
|
md5 = fe.get("md5", "")
|
|
if isinstance(md5, list):
|
|
md5 = md5[0] if md5 else ""
|
|
|
|
entry = {"file": f"bios/{dest}"}
|
|
if md5:
|
|
entry["md5"] = md5
|
|
bios_files.append(entry)
|
|
|
|
if bios_files:
|
|
output[native_id] = {"biosFiles": bios_files}
|
|
|
|
Path(output_path).write_text(
|
|
json.dumps(output, indent=2, ensure_ascii=False) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def validate(self, truth_data: dict, output_path: str) -> list[str]:
|
|
data = json.loads(Path(output_path).read_text(encoding="utf-8"))
|
|
|
|
exported_files: set[str] = set()
|
|
for sys_data in data.values():
|
|
for bf in sys_data.get("biosFiles", []):
|
|
path = bf.get("file", "")
|
|
# Strip bios/ prefix, index both full path and basename
|
|
stripped = path.removeprefix("bios/")
|
|
exported_files.add(stripped)
|
|
basename = path.split("/")[-1] if "/" in path else path
|
|
exported_files.add(basename)
|
|
|
|
issues: list[str] = []
|
|
for sys_data in truth_data.get("systems", {}).values():
|
|
for fe in sys_data.get("files", []):
|
|
name = fe.get("name", "")
|
|
if name.startswith("_"):
|
|
continue
|
|
dest = fe.get("destination", name)
|
|
if name not in exported_files and dest not in exported_files:
|
|
issues.append(f"missing: {name}")
|
|
return issues
|