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-2016 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 
36 from classes.logger import log
37 from classes import info
38 from classes.json_data import JsonDataStore
39 
40 
41 ##
42 # Get the current QApplication's settings instance
44  return QCoreApplication.instance().settings
45 
46 
47 ##
48 # This class only allows setting pre-existing keys taken from default settings file, and merges user settings
49 # on load, assumes default OS dir.
50 class SettingStore(JsonDataStore):
51 
52  def __init__(self):
53  JsonDataStore.__init__(self)
54  # Set the data type name for logging clarity (base class functions use this variable)
55  self.data_type = "user settings"
56  self.settings_filename = "openshot.settings"
57  self.default_settings_filename = os.path.join(info.PATH, 'settings', '_default.settings')
58 
59  ##
60  # Get the entire list of settings (with all metadata)
61  def get_all_settings(self):
62  return self._data
63 
64  ##
65  # Store setting, but adding isn't allowed. All possible settings must be in default settings file.
66  def set(self, key, value):
67  key = key.lower()
68 
69  # Load user setting's values (for easy merging)
70  user_values = {}
71  for item in self._data:
72  if "setting" in item and "value" in item:
73  user_values[item["setting"].lower()] = item
74 
75  # Save setting
76  if key in user_values:
77  user_values[key]["value"] = value
78  else:
79  log.warn("{} key '{}' not valid. The following are valid: {}".format(self.data_type, key,
80  list(self._data.keys())))
81 
82  ##
83  # Load user settings file from disk, merging with allowed settings in default settings file.
84  # Creates user settings if missing.
85  def load(self):
86 
87  # Default and user settings objects
88  default_settings, user_settings = {}, {}
89 
90  # try to load default settings, on failure will raise exception to caller
91  default_settings = self.read_from_file(self.default_settings_filename)
92 
93  # Try to find user settings file
94  file_path = os.path.join(info.USER_PATH, self.settings_filename)
95 
96  # Load user settings (if found)
97  if os.path.exists(file_path.encode('UTF-8')):
98 
99  # Will raise exception to caller on failure to read
100  user_settings = self.read_from_file(file_path)
101 
102  # Merge default and user settings, excluding settings not in default, Save settings
103  self._data = self.merge_settings(default_settings, user_settings)
104 
105  # Return success of saving user settings file back after merge
106  return self.write_to_file(file_path, self._data)
107 
108  ##
109  # Save user settings file to disk
110  def save(self):
111 
112  # Try to find user settings file
113  file_path = os.path.join(info.USER_PATH, self.settings_filename)
114 
115  # try to save data to file, will raise exception on failure
116  self.write_to_file(file_path, self._data)
def set(self, key, value)
Store setting, but adding isn&#39;t allowed.
Definition: settings.py:66
def load(self)
Load user settings file from disk, merging with allowed settings in default settings file...
Definition: settings.py:85
def get_all_settings(self)
Get the entire list of settings (with all metadata)
Definition: settings.py:61
def get_settings()
Get the current QApplication&#39;s settings instance.
Definition: settings.py:43
def __init__(self)
Definition: settings.py:52
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:50
def save(self)
Save user settings file to disk.
Definition: settings.py:110