1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-12 19:09:56 +03:00
pmbootstrap/pmb/helpers/toml.py
2024-10-30 12:39:45 +01:00

23 lines
680 B
Python

# Copyright 2024 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
from pathlib import Path
from pmb.meta import Cache
from pmb.helpers.exceptions import NonBugError
try:
# Python >= 3.11
from tomllib import load, TOMLDecodeError # novermin
except ImportError:
# Python < 3.11
from tomli import load, TOMLDecodeError # type:ignore[import-not-found,no-redef,assignment]
@Cache("path")
def load_toml_file(path: Path) -> dict:
"""Read a toml file into a dict and show the path on error."""
with open(path, mode="rb") as f:
try:
return load(f)
except TOMLDecodeError as e:
raise NonBugError(f"{path}: {e}")