use pathlib everywhere

This commit is contained in:
Jörg Thalheim
2024-09-02 18:25:17 +02:00
parent 9de48de991
commit 1fa0e72bea
28 changed files with 88 additions and 113 deletions

View File

@@ -2,7 +2,6 @@
import argparse
import json
import logging
import os
import re
from pathlib import Path
from typing import Any, get_origin
@@ -285,8 +284,8 @@ def set_option(
current[option_path_store[-1]] = casted
# check if there is an existing config file
if os.path.exists(settings_file):
with open(settings_file) as f:
if settings_file.exists():
with settings_file.open() as f:
current_config = json.load(f)
else:
current_config = {}
@@ -294,7 +293,7 @@ def set_option(
# merge and save the new config file
new_config = merge(current_config, result)
settings_file.parent.mkdir(parents=True, exist_ok=True)
with open(settings_file, "w") as f:
with settings_file.open("w") as f:
json.dump(new_config, f, indent=2)
print(file=f) # add newline at the end of the file to make git happy

View File

@@ -82,7 +82,7 @@ def config_for_machine(flake_dir: Path, machine_name: str) -> dict:
settings_path = machine_settings_file(flake_dir, machine_name)
if not settings_path.exists():
return {}
with open(settings_path) as f:
with settings_path.open() as f:
return json.load(f)
@@ -102,7 +102,7 @@ def set_config_for_machine(flake_dir: Path, machine_name: str, config: dict) ->
# write the config to a json file located at {flake}/machines/{machine_name}/settings.json
settings_path = machine_settings_file(flake_dir, machine_name)
settings_path.parent.mkdir(parents=True, exist_ok=True)
with open(settings_path, "w") as f:
with settings_path.open("w") as f:
json.dump(config, f)
if flake_dir is not None: