Skip to content

Commit da7f410

Browse files
committed
Gracefully handle invalid preference/propellant files
1 parent 49a59b5 commit da7f410

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

uilib/fileManager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ def checkPropellant(self, motor):
251251
def loadRecentlyOpenedFilesList(self):
252252
try:
253253
self.recentFilesList = loadFile(self.recentFilesPath, fileTypes.RECENT_FILES)['recentFilesList']
254-
except FileNotFoundError:
254+
except:
255+
# No need to back up any existing file because a user likely doesn't care about the contents
255256
logger.warn('Unable to load recent files, creating new file at {}'.format(self.recentFilesPath))
256257
self.recentFilesList = []
257258
saveFile(self.recentFilesPath, {'recentFilesList': self.recentFilesList}, fileTypes.RECENT_FILES)

uilib/preferencesManager.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from os.path import join
2+
from os import replace
3+
14
from PyQt6.QtCore import QObject, pyqtSignal
5+
from PyQt6.QtWidgets import QApplication
26

37
from motorlib.properties import PropertyCollection, EnumProperty
48
from motorlib.units import unitLabels, getAllConversions
@@ -54,12 +58,19 @@ def newPreferences(self, prefDict):
5458
self.publishPreferences()
5559

5660
def loadPreferences(self):
61+
preferencesPath = join(getConfigPath(), 'preferences.yaml')
5762
try:
58-
prefDict = loadFile(getConfigPath() + 'preferences.yaml', fileTypes.PREFERENCES)
63+
prefDict = loadFile(preferencesPath, fileTypes.PREFERENCES)
5964
self.preferences.applyDict(prefDict)
6065
self.publishPreferences()
6166
except FileNotFoundError:
62-
logger.warn('Unable to load preferences, creating new file')
67+
logger.warn('Preferences file does not exist, creating new file')
68+
self.savePreferences()
69+
except Exception as error:
70+
backupPath = join(getConfigPath(), 'preferences_backup.yaml')
71+
logger.warn('Error loading preferences: {}'.format(error))
72+
QApplication.instance().outputException(error, "Failed to load preferences. Backing up file to '{}' and starting fresh.".format(backupPath))
73+
replace(preferencesPath, backupPath)
6374
self.savePreferences()
6475

6576
def savePreferences(self):

uilib/propellantManager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from os.path import join
2+
from os import replace
3+
14
from PyQt6.QtCore import QObject, pyqtSignal
5+
from PyQt6.QtWidgets import QApplication
26

37
import motorlib
48

@@ -20,6 +24,7 @@ def __init__(self):
2024
self.propMenu.closed.connect(self.updated.emit)
2125

2226
def loadPropellants(self):
27+
propellantsPath = join(getConfigPath(), 'propellants.yaml')
2328
try:
2429
propList = loadFile(getConfigPath() + 'propellants.yaml', fileTypes.PROPELLANTS)
2530
for propDict in propList:
@@ -30,6 +35,13 @@ def loadPropellants(self):
3035
logger.warn('No propellant file found, saving defaults')
3136
self.propellants = [motorlib.propellant.Propellant(prop) for prop in DEFAULT_PROPELLANTS]
3237
self.savePropellants()
38+
except Exception as error:
39+
backupPath = join(getConfigPath(), 'propellants_backup.yaml')
40+
logger.warn('Error loading propellants: {}'.format(error))
41+
QApplication.instance().outputException(error, "Failed to load propellants. Backing up file to '{}' and starting fresh.".format(backupPath))
42+
replace(propellantsPath, backupPath)
43+
self.propellants = [motorlib.propellant.Propellant(prop) for prop in DEFAULT_PROPELLANTS]
44+
self.savePropellants()
3345

3446
def savePropellants(self):
3547
propellants = [prop.getProperties() for prop in self.propellants]

0 commit comments

Comments
 (0)