Files
rosetta/tools/fixtures/playlist.py
T
2026-10-31 08:55:44 -05:00

67 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Playlist fixtures (tools/fixtures.py): playlist/m3u, playlist/pls, playlist/xspf, codecs/Url, codecs/Text.
# tests/gen/playlist_url.luau random relative references resolved by Python's urllib.parse.urljoin (RFC 3986)
# tests/gen/playlist_cp1252.luau every Windows-1252 byte decoded by Python's cp1252 codec (undefined bytes: U+FFFD)
# tests/gen/playlist_cases.luau playlists written here (M3U8 with EXTINF, PLS via configparser-style lines, XSPF via
# xml.etree.ElementTree) with the entries they hold: { url, title?, duration? }
import random, urllib.parse, xml.etree.ElementTree as ET
BASES = ["http://a/b/c/d;p?q", "https://host.example/lists/m.m3u8", "http://h/x/", "https://cdn.example/a/b/c/list.pls",
"http://a"]
SEGS = ["g", "h", "..", ".", "music", "a b", "x.mp3", "s;p", "%41", "ü"]
def ref(rng):
parts = [rng.choice(SEGS) for _ in range(rng.randrange(1, 5))]
r = "/".join(parts)
k = rng.randrange(6)
if k == 0:
r = "/" + r
elif k == 1:
r = "./" + r
if rng.random() < 0.2:
r += "?y=" + rng.choice(SEGS)
if rng.random() < 0.15:
r += "#" + rng.choice(["s", "f/../x"])
return r
def generate(ctx):
rng = random.Random(8)
cases = []
for _ in range(400):
base = rng.choice(BASES)
r = ref(rng)
if " " in r or "ü" in r or (base == "http://a" and r.startswith(".")):
continue # raw spaces / UTF-8 are encoded first by the playlist code; urljoin keeps a leading "." here
cases.append([r, base, urllib.parse.urljoin(base, r)])
ctx.emit("playlist_url", cases)
ctx.emit("playlist_cp1252", [[b, bytes([b]).decode("cp1252", errors="replace")] for b in range(0x80, 0x100)])
out = []
for n in range(12):
ents = []
for i in range(rng.randrange(1, 9)):
e = {"url": "http://media.example/%d/track%d.mp3" % (n, i)}
if rng.random() < 0.8:
e["title"] = rng.choice(["Song %d" % i, "Björk – Jóga", "A, B & C", "x <y> \"z\""])
if rng.random() < 0.7:
e["duration"] = rng.randrange(1, 600)
ents.append(e)
m3u = "#EXTM3U\n" + "".join(("#EXTINF:%d,%s\n" % (e.get("duration", -1), e.get("title", "")) if ("title" in e or "duration" in e) else "") + e["url"] + "\n" for e in ents)
pls = "[playlist]\n" + "".join("File%d=%s\n" % (i + 1, e["url"]) + ("Title%d=%s\n" % (i + 1, e["title"]) if "title" in e else "") + ("Length%d=%d\n" % (i + 1, e.get("duration", -1))) for i, e in enumerate(ents)) + "NumberOfEntries=%d\nVersion=2\n" % len(ents)
root = ET.Element("playlist", {"version": "1", "xmlns": "http://xspf.org/ns/0/"})
ET.SubElement(root, "title").text = "List %d" % n
tl = ET.SubElement(root, "trackList")
for e in ents:
t = ET.SubElement(tl, "track")
ET.SubElement(t, "location").text = e["url"]
if "title" in e:
ET.SubElement(t, "title").text = e["title"]
if "duration" in e:
ET.SubElement(t, "duration").text = str(e["duration"] * 1000)
xspf = ET.tostring(root, encoding="UTF-8", xml_declaration=True)
expect = [{k: v for k, v in e.items()} for e in ents]
# M3U loses a title when there is no EXTINF text; PLS keeps only what it wrote
out.append({"name": "list%d" % n, "m3u": m3u, "pls": pls, "xspf": xspf, "title": "List %d" % n, "entries": expect})
ctx.emit("playlist_cases", out)