feat: replace TOML input with YAML

This commit is contained in:
Mikkeli Matlock
2026-08-26 15:54:41 +09:00
parent 68229d9d1f
commit 81de21e039
4 changed files with 18 additions and 33 deletions
+14 -26
View File
@@ -8,11 +8,7 @@ import re
from typing import Sequence
import qrcode
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import yaml
SVG_SUFFIX = ".svg"
@@ -71,24 +67,16 @@ def automatic_output_path(text: str, directory: Path = Path(".")) -> Path:
return directory / f"{filename[:stem_length]}{SVG_SUFFIX}"
def read_toml_items(input_path: Path) -> dict[str, str]:
content = input_path.read_text(encoding="utf-8")
def read_yaml_items(input_path: Path) -> dict[str, str]:
try:
items = tomllib.loads(content)
except tomllib.TOMLDecodeError:
items = {}
for line_number, line in enumerate(content.splitlines(), start=1):
if not line.strip() or line.lstrip().startswith("#"):
continue
key, separator, value = line.partition(":")
if not separator or not key.strip():
raise ValueError(
f"{input_path}:{line_number}: expected a flat TOML item"
) from None
items[key.strip()] = value.strip()
items = yaml.safe_load(input_path.read_text(encoding="utf-8")) or {}
except yaml.YAMLError as error:
raise ValueError(f"{input_path}: invalid YAML: {error}") from None
if not all(isinstance(key, str) and isinstance(value, str) for key, value in items.items()):
raise ValueError(f"{input_path}: TOML values must be top-level text strings")
if not isinstance(items, dict) or not all(
isinstance(key, str) and isinstance(value, str) for key, value in items.items()
):
raise ValueError(f"{input_path}: YAML must be a flat mapping of text values")
return items
@@ -98,7 +86,7 @@ def build_parser() -> argparse.ArgumentParser:
parser.add_argument("-t", "--text", dest="option_text", help="Text to encode.")
parser.add_argument("-o", "--output", type=Path, help="Path for the generated SVG.")
parser.add_argument("-i", "--input", type=Path, help="File containing one QR value per line.")
parser.add_argument("--toml", type=Path, help="Flat TOML file containing output names and text.")
parser.add_argument("--yaml", type=Path, help="Flat YAML file containing output names and text.")
return parser
@@ -106,9 +94,9 @@ def run(arguments: Sequence[str] | None = None) -> None:
parser = build_parser()
args = parser.parse_args(arguments)
if args.input is not None or args.toml is not None:
if args.input is not None and args.toml is not None:
parser.error("--input and --toml cannot be used together")
if args.input is not None or args.yaml is not None:
if args.input is not None and args.yaml is not None:
parser.error("--input and --yaml cannot be used together")
if args.text is not None or args.option_text is not None:
parser.error("text input cannot be used with batch input")
@@ -117,7 +105,7 @@ def run(arguments: Sequence[str] | None = None) -> None:
write_svg(line, automatic_output_path(line, Path("output")))
else:
try:
items = read_toml_items(args.toml)
items = read_yaml_items(args.yaml)
except ValueError as error:
parser.error(str(error))
for name, text in items.items():