Merge pull request #2459 from kirigayakazima/codex/opus-tags-required-csv-schema

opus_tags: validate the CSV columns required by the requested operation
This commit is contained in:
MoonEyes
2026-11-28 16:16:02 +01:00
committed by GitHub
2 changed files with 19 additions and 1 deletions

View File

@ -411,6 +411,19 @@ class TestFolder(unittest.TestCase):
self.assertIn("ambiguous name", r.stdout)
self.assertIn("1", r.stdout.split("ambiguous name")[1].split()[0])
def test_cli_checks_operation_specific_csv_headers_before_tagging(self):
original = Path(self.opus, "story", "bank_01", "0ABCDEF1.ogg").read_bytes()
path = Path(self.tmp.name, "header_only.csv")
for header, args, required in [
("bank,stream_id,name", ["--names", str(path), "--source", "story"], "source"),
("label,text_en", ["--names", self.names, "--dialogue", str(path)], "wave")]:
with self.subTest(required=required):
path.write_text(header + "\n", encoding="utf-8", newline="\n")
proc = subprocess.run([sys.executable, "-I", TOOL, self.opus, *args], capture_output=True, text=True)
self.assertEqual(proc.returncode, 2)
self.assertIn(required, proc.stderr)
self.assertEqual(Path(self.opus, "story", "bank_01", "0ABCDEF1.ogg").read_bytes(), original)
def test_cli_checks_the_names_header_even_without_rows(self):
path = Path(self.names)
for header, code in (("", 2), ("wave,text_en", 2), ("bank,stream_id,name", 0)):

View File

@ -146,7 +146,10 @@ def load_names(path, source=None):
with open(path, encoding="utf-8-sig", newline="") as fh:
reader = csv.DictReader(fh)
fields = reader.fieldnames or ()
missing = [name for name in ("bank", "stream_id", "name") if name not in fields]
required = ["bank", "stream_id", "name"]
if source:
required.append("source")
missing = [name for name in required if name not in fields]
if missing:
raise ValueError(f"{path}: missing columns in names CSV header: {', '.join(missing)}")
reject_duplicate_headers(path, fields, {"bank", "stream_id", "name", "source"})
@ -173,6 +176,8 @@ def load_dialogue(path):
with open(path, encoding="utf-8-sig", newline="") as fh:
reader = csv.DictReader(fh)
fields = reader.fieldnames or ()
if "wave" not in fields:
raise ValueError(f"{path}: missing wave column in dialogue CSV header")
text_fields = [field for field in fields if field.startswith("text_")]
reject_duplicate_headers(path, fields, {"wave", "label", "speaker", *text_fields})
langs = [field[5:] for field in text_fields]