replace tomlib with tomlkit

tomlkit can read and write toml files
This commit is contained in:
Marc Koch 2025-03-21 13:22:23 +01:00
parent 3effd5558f
commit 0d4d6d3f46
Signed by: marc.koch
GPG key ID: 12406554CFB028B9
8 changed files with 129 additions and 117 deletions

View file

@ -1,16 +1,15 @@
import datetime
import json
import logging
import tomllib
from collections import deque
from datetime import datetime as dt, timezone
from pathlib import Path
import pytz
import tomli_w
from civifang import api
from httpx import post
from ms_active_directory import ADUser, ADGroup
from tomlkit.toml_file import TOMLFile
from .enums import Priority
from .exceptions import ScriptAlreadyRunningError
@ -56,38 +55,46 @@ class RecentRun:
:param is_running:
:return:
"""
rr = recent_run if recent_run else self._datetime
is_running = is_running if is_running is not None else self._already_running
new_data = {
'recent-run': rr,
'is-running': is_running,
}
# Return if no data is provided
if recent_run is None and is_running is None:
return
# Read the existing data
toml_file = TOMLFile(self._file_path)
toml = toml_file.read()
# Update the values
if recent_run:
toml['recent_run'] = recent_run
if is_running is not None:
toml['is-running'] = is_running
# Write the data to the file
with open(self._file_path, 'wb') as f:
tomli_w.dump(new_data, f)
toml_file.write(toml)
def _read_data_from_file(self):
"""
Read the recent run time from the file
:return:
"""
with open(self._file_path, 'rb') as f:
data = tomllib.load(f)
self._already_running = data.get('is-running', False)
recent_run = data.get('recent-run')
# Read the data from the file
toml_file = TOMLFile(self._file_path)
toml = toml_file.read()
# Set the datetime to the recent run time
if not recent_run:
return
if isinstance(recent_run, datetime.datetime):
self._datetime = recent_run
elif isinstance(recent_run, float):
self._datetime = dt.fromtimestamp(recent_run) \
.astimezone(self._timezone)
else:
raise ValueError(
f"Invalid recent_run '{recent_run}' in {self._file_path}.")
self._already_running = toml.get('is-running', False)
recent_run = toml.get('recent-run')
# Set the datetime to the recent run time
if not recent_run:
return
if isinstance(recent_run, datetime.datetime):
self._datetime = recent_run
elif isinstance(recent_run, float):
self._datetime = dt.fromtimestamp(recent_run) \
.astimezone(self._timezone)
else:
raise ValueError(
f"Invalid recent_run '{recent_run}' in {self._file_path}.")
@property
def datetime(self) -> dt | None: