write default_config to json file

This commit is contained in:
onyx-and-iris 2023-09-04 05:11:45 +01:00
parent 7357cacc30
commit 5cc8f1ae3d
2 changed files with 12 additions and 11 deletions

2
.gitignore vendored
View File

@ -171,7 +171,7 @@ banana.py
potato.py potato.py
# persistent storage # persistent storage
default.bin settings.json
# quick test # quick test
quick.py quick.py

View File

@ -1,5 +1,5 @@
import json
import logging import logging
import pickle
from pathlib import Path from pathlib import Path
import PySimpleGUI as psg import PySimpleGUI as psg
@ -24,7 +24,7 @@ psg.theme("Dark Blue 3")
class NVDAVMWindow(psg.Window): class NVDAVMWindow(psg.Window):
"""Represents the main window of the Voicemeeter NVDA application""" """Represents the main window of the Voicemeeter NVDA application"""
DEFAULT_BIN = "default.bin" SETTINGS = "settings.json"
def __init__(self, title, vm): def __init__(self, title, vm):
self.vm = vm self.vm = vm
@ -49,11 +49,12 @@ class NVDAVMWindow(psg.Window):
self.register_events() self.register_events()
def __enter__(self): def __enter__(self):
default_config = Path.cwd() / self.DEFAULT_BIN default_config = Path.cwd() / self.SETTINGS
if default_config.exists(): if default_config.exists():
try: try:
with open(default_config, "rb") as f: with open(default_config, "r") as f:
config = pickle.load(f) data = json.load(f)
config = data["default_config"]
if Path(config).exists(): if Path(config).exists():
self.vm.set("command.load", config) self.vm.set("command.load", config)
self.logger.debug(f"config {config} loaded") self.logger.debug(f"config {config} loaded")
@ -62,8 +63,8 @@ class NVDAVMWindow(psg.Window):
self.nvda.speak, self.nvda.speak,
f"config {Path(config).stem} has been loaded", f"config {Path(config).stem} has been loaded",
) )
except EOFError: except json.JSONDecodeError:
self.logger.debug("no data in default bin. silently continuing...") self.logger.debug("no data in settings.json. silently continuing...")
return self return self
@ -305,15 +306,15 @@ class NVDAVMWindow(psg.Window):
file_types=(("XML", ".xml"),), file_types=(("XML", ".xml"),),
): ):
filepath = Path(filepath) filepath = Path(filepath)
with open(self.DEFAULT_BIN, "wb") as f: with open(self.SETTINGS, "w") as f:
pickle.dump(str(filepath), f) json.dump({"default_config": str(filepath)}, f)
self.TKroot.after( self.TKroot.after(
200, 200,
self.nvda.speak, self.nvda.speak,
f"config {filepath.stem} set as default on startup", f"config {filepath.stem} set as default on startup",
) )
else: else:
with open(self.DEFAULT_BIN, "wb") as f: with open(self.SETTINGS, "wb") as f:
f.truncate() f.truncate()
self.logger.debug("default bin was truncated") self.logger.debug("default bin was truncated")