mirror of
https://github.com/EyeTrackVR/EyeTrackVR.git
synced 2025-11-04 14:39:42 +08:00
Prevents configuration files from failing to load.
This commit is contained in:
parent
5428ef6d53
commit
7faf4b2ae5
@ -3,7 +3,10 @@ from osc import EyeId
|
|||||||
import os.path
|
import os.path
|
||||||
import json
|
import json
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
import shutil
|
||||||
|
|
||||||
CONFIG_FILE_NAME: str = "eyetrack_settings.json"
|
CONFIG_FILE_NAME: str = "eyetrack_settings.json"
|
||||||
|
BACKUP_CONFIG_FILE_NAME: str = "eyetrack_settings.backup"
|
||||||
|
|
||||||
|
|
||||||
class EyeTrackCameraConfig(BaseModel):
|
class EyeTrackCameraConfig(BaseModel):
|
||||||
@ -61,9 +64,35 @@ class EyeTrackConfig(BaseModel):
|
|||||||
if not os.path.exists(CONFIG_FILE_NAME):
|
if not os.path.exists(CONFIG_FILE_NAME):
|
||||||
print("No settings file, using base settings")
|
print("No settings file, using base settings")
|
||||||
return EyeTrackConfig()
|
return EyeTrackConfig()
|
||||||
with open(CONFIG_FILE_NAME, "r") as settings_file:
|
try:
|
||||||
return EyeTrackConfig(**json.load(settings_file))
|
with open(CONFIG_FILE_NAME, "r") as settings_file:
|
||||||
|
return EyeTrackConfig(**json.load(settings_file))
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("Failed to load settings file.")
|
||||||
|
load_config = None
|
||||||
|
if os.path.exists(BACKUP_CONFIG_FILE_NAME):
|
||||||
|
try:
|
||||||
|
with open(BACKUP_CONFIG_FILE_NAME, "r") as settings_file:
|
||||||
|
load_config = EyeTrackConfig(**json.load(settings_file))
|
||||||
|
print("using backup settings")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
pass
|
||||||
|
if load_config is None:
|
||||||
|
print("using base settings")
|
||||||
|
load_config = EyeTrackConfig()
|
||||||
|
return load_config
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(CONFIG_FILE_NAME, "w+") as settings_file:
|
# todo: Write only if there is a difference between the saved configuration file and the current configuration.
|
||||||
|
if os.path.exists(CONFIG_FILE_NAME):
|
||||||
|
try:
|
||||||
|
# Verify existing configuration files.
|
||||||
|
with open(CONFIG_FILE_NAME, "r") as settings_file:
|
||||||
|
EyeTrackConfig(**json.load(settings_file))
|
||||||
|
shutil.copy(CONFIG_FILE_NAME, BACKUP_CONFIG_FILE_NAME)
|
||||||
|
# print("Backed up settings files.") # Comment out because it's too loud.
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# No backup because the saved settings file is broken.
|
||||||
|
pass
|
||||||
|
with open(CONFIG_FILE_NAME, "w") as settings_file:
|
||||||
json.dump(obj=self.dict(), fp=settings_file)
|
json.dump(obj=self.dict(), fp=settings_file)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user