Name the everyday media type extensions, refuse an emptied table, and cut the false register claim
Tests / vet + fmt + tests (pull_request) Successful in 1m26s
Tests / Gitea release from CHANGELOG.md (pull_request) Has been skipped

This commit is contained in:
2026-09-18 21:55:54 +02:00
parent 69d0db9431
commit ecc0b475ae
7 changed files with 24 additions and 25 deletions
+4 -2
View File
@@ -3,7 +3,7 @@
data-import/mimetype.py [--source URL_OR_FILE] [--cache DIR] [--out FILE]
Only IANA-registered types with a filename extension ship; the first extension is the one listed.
Only types mime-db records as IANA-registered, with a filename extension, ship.
"""
import argparse
import json
@@ -16,12 +16,14 @@ SOURCE = "https://raw.githubusercontent.com/jshttp/mime-db/master/db.json"
OUT = Path(__file__).resolve().parent.parent / "data" / "misc" / "mimetype.tsv"
CACHE = Path(__file__).resolve().parent / "cache"
COLUMNS = ["ext", "type"]
# mime-db lists extensions in no particular order, so where the everyday one is not first, name it.
FIXUPS = {"application/mp4": "mp4s", "audio/mpeg": "mp3", "audio/ogg": "ogg", "video/quicktime": "mov"}
def rows(db):
for mimetype, entry in db.items():
if entry.get("source") == "iana" and entry.get("extensions"):
yield {"ext": "." + entry["extensions"][0], "type": mimetype}
yield {"ext": "." + FIXUPS.get(mimetype, entry["extensions"][0]), "type": mimetype}
def main():
+3 -1
View File
@@ -5,12 +5,14 @@ from pathlib import Path
def write(path, columns, rows):
"""Write the rows as a TSV; every cell must be non-empty and free of tabs, newlines and braces."""
"""Write the rows as a TSV; the table needs a row, and every cell must be non-empty and free of tabs, newlines and braces."""
lines = ["\t".join(columns)]
for row in rows:
cells = [str(row[c]) for c in columns]
if not all(cells) or any(re.search(r"[\t\n{}]", c) for c in cells):
raise ValueError(f"{path}: a cell is empty or holds a tab, newline or brace: {row}")
lines.append("\t".join(cells))
if len(lines) < 2:
raise ValueError(f"{path}: has no rows below its header; a table is at least two rows")
Path(path).write_text("\n".join(lines) + "\n", encoding="utf-8")
print(f"{path}: {len(lines) - 1} rows", file=sys.stderr)