34 lines
757 B
Python
34 lines
757 B
Python
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
def _parse_tag_line(tag_line: str) -> tuple[str, str | None]:
|
|
"""
|
|
Parse a tag line of the format:
|
|
|
|
#KEY:Value
|
|
|
|
or
|
|
|
|
#KEY:
|
|
|
|
Returns a tuple of (key, value), where the value might be None.
|
|
"""
|
|
|
|
key_and_potentially_value = tuple(
|
|
tag_line.strip().removeprefix("#").split(":", maxsplit=1)
|
|
)
|
|
|
|
return (
|
|
key_and_potentially_value
|
|
if len(key_and_potentially_value) == 2
|
|
else (key_and_potentially_value[0], None)
|
|
)
|
|
|
|
|
|
def parse_song_txt(song_txt: Path) -> dict[str, Any]:
|
|
with song_txt.open(encoding="utf-8", errors="ignore") as f:
|
|
tags = dict(_parse_tag_line(line) for line in f if line.startswith("#"))
|
|
|
|
return tags
|