This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyqgis_startup.py
87 lines (76 loc) · 3.47 KB
/
pyqgis_startup.py
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
import yaml
import codecs
import pathlib
import collections
import configparser
import PyQt5.QtCore
import qgis.core
def main():
application = qgis.core.QgsApplication.instance()
applicationSettings = qgis.core.QgsSettings(
application.organizationName(), application.applicationName()
)
globalSettingsPath = pathlib.Path(applicationSettings.globalSettingsPath())
globalSettingsDirPath = globalSettingsPath.parent
qgisConstrainedSettingsPath = globalSettingsDirPath / "qgis_constrained_settings.yml"
if not qgisConstrainedSettingsPath.is_file():
print("No file named {}".format(qgisConstrainedSettingsPath))
return
print("Load constrained settings from {}".format(qgisConstrainedSettingsPath))
with open(str(qgisConstrainedSettingsPath)) as f:
constrainedSettings = yaml.safe_load(f)
userSettings = PyQt5.QtCore.QSettings()
print("Process {}".format(userSettings.fileName()))
propertiesToRemove = constrainedSettings.get("propertiesToRemove", {})
for group, properties in propertiesToRemove.items():
userSettings.beginGroup(group)
if isinstance(properties, str):
if properties == "*":
userSettings.remove("")
else:
for prop in properties:
userSettings.remove(prop)
userSettings.endGroup()
globalSettings = configparser.ConfigParser()
with open(str(globalSettingsPath)) as f:
globalSettings.read_file(f)
propertiesToMerge = constrainedSettings.get("propertiesToMerge", {})
for group, properties in propertiesToMerge.items():
if not globalSettings.has_section(group):
continue
userSettings.beginGroup(group)
for prop in properties:
if not globalSettings.has_option(group, prop):
continue
userPropertyValues = userSettings.value(prop)
if not userPropertyValues:
continue
if not isinstance(userPropertyValues, list):
userPropertyValues = [userPropertyValues]
globalPropertyValues = globalSettings.get(group, prop)
globalPropertyValues = globalPropertyValues.split(",")
# codecs.decode(v, "unicode_espace") is used to convert the raw string into a normal
# string. This is required to avoid changing \\ sequences into \\\\ sequences
globalPropertyValues = list(
map(lambda v: codecs.decode(v.strip('" '), "unicode_escape"), globalPropertyValues)
)
userPropertyValues = globalPropertyValues + userPropertyValues
# remove duplicates
userPropertyValues = list(collections.OrderedDict.fromkeys(userPropertyValues))
userSettings.setValue(prop, userPropertyValues)
userSettings.endGroup()
propertyValuesToRemove = constrainedSettings.get("propertyValuesToRemove", {})
for group, properties in propertyValuesToRemove.items():
userSettings.beginGroup(group)
for prop in properties:
userPropertyValues = userSettings.value(prop)
if not userPropertyValues:
continue
valuesToRemove = list(map(lambda v: v.rstrip("/\\ "), properties[prop]))
userPropertyValues = [
v for v in userPropertyValues if v.rstrip("/\\ ") not in valuesToRemove
]
userSettings.setValue(prop, userPropertyValues)
userSettings.endGroup()
if __name__ == "__main__":
main()