30 lines
940 B
Python
30 lines
940 B
Python
import tomllib
|
|
from pathlib import Path
|
|
import logging
|
|
|
|
def read_toml_file(file_path: str) -> dict:
|
|
try:
|
|
with open(file_path, "rb") as file:
|
|
return tomllib.load(file)
|
|
except FileNotFoundError:
|
|
logging.error(f"File {file_path} not found.")
|
|
return {}
|
|
except tomllib.TOMLDecodeError as e:
|
|
logging.error(f"Error parsing {file_path}: {e}")
|
|
return {}
|
|
|
|
def write_toml_file(file_path: str, data: dict) -> None:
|
|
with open(file_path, "w") as file:
|
|
toml.dump(data, file)
|
|
|
|
def create_backup(file_path: str) -> str:
|
|
config_path = Path(file_path)
|
|
backup_dir = config_path.parent / "backups"
|
|
backup_dir.mkdir(exist_ok=True)
|
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
backup_file = backup_dir / f"{config_path.stem}_{timestamp}.toml"
|
|
shutil.copy2(file_path, backup_file)
|
|
logging.info(f"Backup created: {backup_file}")
|
|
return backup_file
|