OpenShot Video Editor  2.0.0
settings.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file loads and saves settings
5 # @author Noah Figg <eggmunkee@hotmail.com>
6 # @author Jonathan Thomas <jonathan@openshot.org>
7 # @author Olivier Girard <eolinwen@gmail.com>
8 #
9 # @section LICENSE
10 #
11 # Copyright (c) 2008-2018 OpenShot Studios, LLC
12 # (http://www.openshotstudios.com). This file is part of
13 # OpenShot Video Editor (http://www.openshot.org), an open-source project
14 # dedicated to delivering high quality video editing and animation solutions
15 # to the world.
16 #
17 # OpenShot Video Editor is free software: you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation, either version 3 of the License, or
20 # (at your option) any later version.
21 #
22 # OpenShot Video Editor is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 #
30 
31 # SettingStore - class which allows getting/storing of settings, loading and saving to json
32 import os
33 
34 from PyQt5.QtCore import QStandardPaths, QCoreApplication
35 from PyQt5.QtWidgets import QMessageBox
36 
37 from classes.logger import log
38 from classes import info
39 from classes.json_data import JsonDataStore
40 
41 
42 ##
43 # Get the current QApplication's settings instance
45  return QCoreApplication.instance().settings
46 
47 
48 ##
49 # This class only allows setting pre-existing keys taken from default settings file, and merges user settings
50 # on load, assumes default OS dir.
51 class SettingStore(JsonDataStore):
52 
53  def __init__(self):
54  JsonDataStore.__init__(self)
55  # Set the data type name for logging clarity (base class functions use this variable)
56  self.data_type = "user settings"
57  self.settings_filename = "openshot.settings"
58  self.default_settings_filename = os.path.join(info.PATH, 'settings', '_default.settings')
59 
60  ##
61  # Get the entire list of settings (with all metadata)
62  def get_all_settings(self):
63  return self._data
64 
65  ##
66  # Store setting, but adding isn't allowed. All possible settings must be in default settings file.
67  def set(self, key, value):
68  key = key.lower()
69 
70  # Load user setting's values (for easy merging)
71  user_values = {}
72  for item in self._data:
73  if "setting" in item and "value" in item:
74  user_values[item["setting"].lower()] = item
75 
76  # Save setting
77  if key in user_values:
78  user_values[key]["value"] = value
79  else:
80  log.warn("{} key '{}' not valid. The following are valid: {}".format(self.data_type, key,
81  list(self._data.keys())))
82 
83  ##
84  # Load user settings file from disk, merging with allowed settings in default settings file.
85  # Creates user settings if missing.
86  def load(self):
87 
88  # Default and user settings objects
89  default_settings, user_settings = {}, {}
90 
91  # try to load default settings, on failure will raise exception to caller
92  default_settings = self.read_from_file(self.default_settings_filename)
93 
94  # Try to find user settings file
95  file_path = os.path.join(info.USER_PATH, self.settings_filename)
96 
97  # Load user settings (if found)
98  if os.path.exists(file_path.encode('UTF-8')):
99 
100  # Will raise exception to caller on failure to read
101  try:
102  user_settings = self.read_from_file(file_path)
103  except Exception as ex:
104  log.error("Error loading settings file: %s" % ex)
105 
106  _ = QCoreApplication.instance()._tr
107  QMessageBox.warning(None, _("Settings Error"),
108  _("Error loading settings file: %(file_path)s. Settings will be reset.") % { "file_path": file_path})
109  user_settings = {}
110 
111  # Merge default and user settings, excluding settings not in default, Save settings
112  self._data = self.merge_settings(default_settings, user_settings)
113 
114  # Return success of saving user settings file back after merge
115  return self.write_to_file(file_path, self._data)
116 
117  ##
118  # Save user settings file to disk
119  def save(self):
120 
121  # Try to find user settings file
122  file_path = os.path.join(info.USER_PATH, self.settings_filename)
123 
124  # try to save data to file, will raise exception on failure
125  self.write_to_file(file_path, self._data)
def set(self, key, value)
Store setting, but adding isn&#39;t allowed.
Definition: settings.py:67
def load(self)
Load user settings file from disk, merging with allowed settings in default settings file...
Definition: settings.py:86
def get_all_settings(self)
Get the entire list of settings (with all metadata)
Definition: settings.py:62
def get_settings()
Get the current QApplication&#39;s settings instance.
Definition: settings.py:44
def __init__(self)
Definition: settings.py:53
This class only allows setting pre-existing keys taken from default settings file, and merges user settings on load, assumes default OS dir.
Definition: settings.py:51
def save(self)
Save user settings file to disk.
Definition: settings.py:119