diff --git a/stages/org.osbuild.bootc.install.config b/stages/org.osbuild.bootc.install.config index 10a2664c..3390b43e 100755 --- a/stages/org.osbuild.bootc.install.config +++ b/stages/org.osbuild.bootc.install.config @@ -2,12 +2,8 @@ import pathlib import sys -try: - import toml -except ModuleNotFoundError: - import pytoml as toml - import osbuild.api +from osbuild.util import toml def main(tree, options): @@ -17,8 +13,7 @@ def main(tree, options): path = pathlib.Path(tree) / "usr/lib/bootc/install" / filename path.parent.mkdir(parents=True, exist_ok=True) - with open(path, "w", encoding="utf8") as config_file: - toml.dump(config, config_file) + toml.dump_to_file(config, path) return 0 diff --git a/stages/org.osbuild.containers.storage.conf b/stages/org.osbuild.containers.storage.conf index f1c7ae7c..5827f06d 100755 --- a/stages/org.osbuild.containers.storage.conf +++ b/stages/org.osbuild.containers.storage.conf @@ -4,12 +4,8 @@ import os import sys from typing import Dict -try: - import toml -except ModuleNotFoundError: - import pytoml as toml - import osbuild.api +from osbuild.util import toml DEFAULT_LOCATION = "/etc/containers/storage.conf" @@ -50,14 +46,6 @@ def merge_config(section: str, data: Dict, config: Dict): have.update(want) -def write_comment(f, comment: list): - if not comment: - return - - data = "\n".join(map(lambda c: f"# {c}", comment)) - f.write(data + "\n\n") - - def main(tree, options): location = options.get("filename", DEFAULT_LOCATION) config = options["config"] @@ -69,24 +57,17 @@ def main(tree, options): # if a filebase was specified, we use it as base if filebase: - with open(filebase, "r", encoding="utf8") as f: - data = toml.load(f) + data = toml.load_from_file(filebase) # if the target exists, we merge it with contextlib.suppress(FileNotFoundError): - with open(path, "r", encoding="utf8") as f: - have = toml.load(f) - - merge_config("storage", data, have) + have = toml.load_from_file(path) + merge_config("storage", data, have) # now merge our configuration into data merge_config("storage", data, config) - with open(path, "w", encoding="utf8") as f: - write_comment(f, HEADER) - write_comment(f, comment) - - toml.dump(data, f) + toml.dump_to_file(data, path, header=HEADER + ["\n"] + comment) return 0 diff --git a/stages/test/test_containers_storage_conf.py b/stages/test/test_containers_storage_conf.py index 2aed0a5e..d98409e6 100644 --- a/stages/test/test_containers_storage_conf.py +++ b/stages/test/test_containers_storage_conf.py @@ -5,12 +5,8 @@ import re import pytest -try: - import toml -except ModuleNotFoundError: - import pytoml as toml - from osbuild import testutil +from osbuild.util import toml TEST_INPUT = [ ({}, {"additionalimagestores": ["/path/to/store"]}, [("storage.options.additionalimagestores", ["/path/to/store"])]), @@ -49,8 +45,7 @@ def test_containers_storage_conf_integration(tmp_path, stage_module, test_filena assert os.path.exists(confpath) conf = None - with open(confpath, 'r', encoding="utf-8") as f: - conf = toml.load(f) + conf = toml.load_from_file(confpath) assert conf is not None