Skip to content

Commit

Permalink
Settings: Default everything if no info.USER_PATH
Browse files Browse the repository at this point in the history
If the info.USER_PATH directory does not exist, load the settings
defaults, but don't try to merge the user's settings or to write
back the updated settings file immediately after loading.
  • Loading branch information
ferdnyc committed Dec 9, 2021
1 parent b065a87 commit db60cf1
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/classes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, parent=None):
# Set the data type name for logging clarity (base class functions use this variable)
self.data_type = "user settings"
self.settings_filename = "openshot.settings"
self.default_settings_filename = os.path.join(info.PATH, 'settings', '_default.settings')
self.defaults_path = os.path.join(info.PATH, 'settings', '_default.settings')

def get_all_settings(self):
""" Get the entire list of settings (with all metadata) """
Expand Down Expand Up @@ -78,38 +78,36 @@ def load(self):
""" Load user settings file from disk, merging with allowed settings in default settings file.
Creates user settings if missing. """

# Default and user settings objects
default_settings, user_settings = {}, {}

# try to load default settings, on failure will raise exception to caller
default_settings = self.read_from_file(self.default_settings_filename)
default_settings = self.read_from_file(self.defaults_path)
self._data = default_settings

# Try to find user settings file
file_path = os.path.join(info.USER_PATH, self.settings_filename)
# Try to find user settings dir, give up if it's not there
if not os.path.exists(info.USER_PATH):
return True

# Load user settings (if found)
# Load or create user settings
file_path = os.path.join(info.USER_PATH, self.settings_filename)
if os.path.exists(os.fsencode(file_path)):
# Will raise exception to caller on failure to read
try:
user_settings = self.read_from_file(file_path)
# Merge sources, excluding user settings not found in default
self._data = self.merge_settings(default_settings, user_settings)
except Exception as ex:
log.error("Error loading settings file: %s", ex)
user_settings = {}
if self.app:
# We have a parent, ask to show a message box
self.app.settings_load_error(file_path)

# Merge default and user settings, excluding settings not in default, Save settings
self._data = self.merge_settings(default_settings, user_settings)

# Return success of saving user settings file back after merge
return self.write_to_file(file_path, self._data)

def save(self):
""" Save user settings file to disk """

# Try to find user settings file
file_path = os.path.join(info.USER_PATH, self.settings_filename)
if os.path.exists(info.USER_PATH):
file_path = os.path.join(info.USER_PATH, self.settings_filename)
# try to save data to file, will raise exception on failure
self.write_to_file(file_path, self._data)

# try to save data to file, will raise exception on failure
self.write_to_file(file_path, self._data)

0 comments on commit db60cf1

Please sign in to comment.