/usr/lib/python3/dist-packages/UM/Preferences.py is in python3-uranium 3.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | # Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
import configparser
from UM.Signal import Signal, signalemitter
from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType #To register the MIME type of the preference file.
from UM.SaveFile import SaveFile
MimeTypeDatabase.addMimeType(
MimeType(
name = "application/x-uranium-preferences",
comment = "Uranium Preferences File",
suffixes = ["cfg"],
preferred_suffix = "cfg"
)
)
## Preferences are application based settings that are saved for future use.
# Typical preferences would be window size, standard machine, etc.
@signalemitter
class Preferences:
Version = 5
def __init__(self):
super().__init__()
self._file = None
self._parser = None
self._preferences = {}
def addPreference(self, key, default_value):
preference = self._findPreference(key)
if preference:
preference.setDefault(default_value)
return
group, key = self._splitKey(key)
if group not in self._preferences:
self._preferences[group] = {}
self._preferences[group][key] = _Preference(key, default_value)
## Changes the default value of a preference.
#
# If the preference is currently set to the old default, the value of the
# preference will be set to the new default.
#
# \param key The key of the preference to set the default of.
# \param default_value The new default value of the preference.
def setDefault(self, key, default_value):
preference = self._findPreference(key)
if not preference: #Key not found.
Logger.log("w", "Tried to set the default value of non-existing setting %s.", key)
return
if preference.getValue() == preference.getDefault():
self.setValue(key, default_value)
preference.setDefault(default_value)
def setValue(self, key, value):
preference = self._findPreference(key)
if preference:
preference.setValue(value)
self.preferenceChanged.emit(key)
else:
Logger.log("w", "Tried to set the value of non-existing setting %s.", key)
def getValue(self, key):
preference = self._findPreference(key)
if preference:
value = preference.getValue()
if value == "True":
value = True
elif value == "False":
value = False
return value
Logger.log("w", "Tried to get the value of non-existing setting %s.", key)
return None
def resetPreference(self, key):
preference = self._findPreference(key)
if preference:
preference.setValue(preference.getDefault())
self.preferenceChanged.emit(key)
def readFromFile(self, file):
self._loadFile(file)
if not self._parser:
return
self.__initializeSettings()
def __initializeSettings(self):
for group, group_entries in self._parser.items():
if group == "DEFAULT":
continue
if group not in self._preferences:
self._preferences[group] = {}
for key, value in group_entries.items():
if key not in self._preferences[group]:
self._preferences[group][key] = _Preference(key)
self._preferences[group][key].setValue(value)
self.preferenceChanged.emit("{0}/{1}".format(group, key))
def writeToFile(self, file):
parser = configparser.ConfigParser(interpolation = None) #pylint: disable=bad-whitespace
for group, group_entries in self._preferences.items():
parser[group] = {}
for key, pref in group_entries.items():
if pref.getValue() != pref.getDefault():
parser[group][key] = str(pref.getValue())
parser["general"]["version"] = str(Preferences.Version)
try:
if hasattr(file, "read"): # If it already is a stream like object, write right away
parser.write(file)
else:
with SaveFile(file, "wt") as save_file:
parser.write(save_file)
except Exception as e:
Logger.log("e", "Failed to write preferences to %s: %s", file, str(e))
preferenceChanged = Signal()
@classmethod
def getInstance(cls) -> "Preferences":
if not cls._instance:
cls._instance = Preferences()
return cls._instance
def _splitKey(self, key):
group = "general"
key = key
if "/" in key:
parts = key.split("/")
group = parts[0]
key = parts[1]
return (group, key)
def _findPreference(self, key):
group, key = self._splitKey(key)
if group in self._preferences:
if key in self._preferences[group]:
return self._preferences[group][key]
return None
def _loadFile(self, file):
if self._file and self._file == file:
return self._parser
try:
self._parser = configparser.ConfigParser(interpolation = None) #pylint: disable=bad-whitespace
if hasattr(file, "read"):
self._parser.read_file(file)
else:
self._parser.read(file, encoding = "utf-8")
if self._parser["general"]["version"] != str(Preferences.Version):
Logger.log("w", "Old config file found, ignoring")
self._parser = None
return
except Exception:
Logger.logException("e", "An exception occured while trying to read preferences file")
self._parser = None
return
del self._parser["general"]["version"]
_instance = None # type: Preferences
## Extract data from string and store it in the Configuration parser.
def deserialize(self, serialized: str) -> str:
updated_preferences = self.__updateSerialized(serialized)
self._parser = configparser.ConfigParser(interpolation=None)
self._parser.read_string(updated_preferences)
has_version = "version" in self._parser["general"]
if has_version:
if self._parser["general"]["version"] != str(Preferences.Version):
Logger.log("w", "Could not deserialize preferences from loaded project")
self._parser = None
return
else:
return
self.__initializeSettings()
## Updates the given serialized data to the latest version.
def __updateSerialized(self, serialized: str) -> str:
configuration_type = "preferences"
version = None
try:
import UM.VersionUpgradeManager
version = UM.VersionUpgradeManager.VersionUpgradeManager.getInstance().getFileVersion(configuration_type,
serialized)
if version is not None:
from UM.VersionUpgradeManager import VersionUpgradeManager
result = VersionUpgradeManager.getInstance().updateFilesData(configuration_type, version,
[serialized], [""])
if result is not None:
serialized = result.files_data[0]
return serialized
except Exception as e:
Logger.log("d", "Could not get version from serialized: %s", e)
pass
class _Preference:
def __init__(self, name, default = None, value = None): #pylint: disable=bad-whitespace
self._name = name
self._default = default
self._value = default if value is None else value
def getName(self):
return self._name
def getValue(self):
return self._value
def getDefault(self):
return self._default
def setDefault(self, default):
self._default = default
def setValue(self, value):
self._value = value
|