|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class to store the QtHelp documentation settings before |
|
8 being applied to the help engine. |
|
9 """ |
|
10 |
|
11 import collections |
|
12 import copy |
|
13 |
|
14 from PyQt6.QtHelp import QCompressedHelpInfo |
|
15 |
|
16 |
|
17 class QtHelpDocumentationSettings: |
|
18 """ |
|
19 Class implementing a temporary store for QtHelp documentation settings. |
|
20 """ |
|
21 def __init__(self): |
|
22 """ |
|
23 Constructor |
|
24 """ |
|
25 self._namespaceToComponent = {} |
|
26 self._componentToNamespace = collections.defaultdict(list) |
|
27 |
|
28 self._namespaceToVersion = {} |
|
29 self._versionToNamespace = collections.defaultdict(list) |
|
30 |
|
31 self._namespaceToFilename = {} |
|
32 self._filenameToNamespace = {} |
|
33 |
|
34 def addDocumentation(self, filename): |
|
35 """ |
|
36 Public method to a add a documentation file to the list. |
|
37 |
|
38 @param filename name of the documentation file to add |
|
39 @type str |
|
40 @return flag indicating success |
|
41 @rtype bool |
|
42 """ |
|
43 info = QCompressedHelpInfo.fromCompressedHelpFile(filename) |
|
44 |
|
45 if info.isNull(): |
|
46 return False |
|
47 |
|
48 namespace = info.namespaceName() |
|
49 |
|
50 if namespace in self._namespaceToFilename: |
|
51 return False |
|
52 |
|
53 if filename in self._filenameToNamespace: |
|
54 return False |
|
55 |
|
56 component = info.component() |
|
57 version = info.version() |
|
58 |
|
59 self._namespaceToFilename[namespace] = filename |
|
60 self._filenameToNamespace[filename] = namespace |
|
61 |
|
62 self._namespaceToComponent[namespace] = component |
|
63 self._componentToNamespace[component].append(namespace) |
|
64 |
|
65 self._namespaceToVersion[namespace] = version |
|
66 self._versionToNamespace[version].append(namespace) |
|
67 |
|
68 return True |
|
69 |
|
70 def removeDocumentation(self, namespace): |
|
71 """ |
|
72 Public method to remove the documentation of a given namespace. |
|
73 |
|
74 @param namespace name of the namespace |
|
75 @type str |
|
76 @return flag indicating success |
|
77 @rtype bool |
|
78 """ |
|
79 if not namespace: |
|
80 return False |
|
81 |
|
82 try: |
|
83 filename = self._namespaceToFilename(namespace) |
|
84 except KeyError: |
|
85 return False |
|
86 |
|
87 component = self._namespaceToComponent[namespace] |
|
88 version = self._namespaceToVersion[namespace] |
|
89 |
|
90 del self._namespaceToComponent[namespace] |
|
91 del self._namespaceToVersion[namespace] |
|
92 del self._namespaceToFilename[namespace] |
|
93 del self._filenameToNamespace[filename] |
|
94 self._componentToNamespace[component].remove(namespace) |
|
95 if len(self._componentToNamespace[component]) == 0: |
|
96 del self._componentToNamespace[component] |
|
97 self._versionToNamespace[version].remove(namespace) |
|
98 if len(self._versionToNamespace[version]) == 0: |
|
99 del self._versionToNamespace[version] |
|
100 |
|
101 return True |
|
102 |
|
103 def namespace(self, filename): |
|
104 """ |
|
105 Public method to get the namespace defined by a QtHelp file. |
|
106 |
|
107 @param filename name of the QtHelp file |
|
108 @type str |
|
109 @return name of the namespace |
|
110 @rtype str |
|
111 """ |
|
112 return self._filenameToNamespace(filename) |
|
113 |
|
114 def components(self): |
|
115 """ |
|
116 Public method to get the list of components. |
|
117 |
|
118 @return list of components |
|
119 @rtype list of str |
|
120 """ |
|
121 return [k for k in self._componentToNamespace.keys()] |
|
122 |
|
123 def versions(self): |
|
124 """ |
|
125 Public method to get the list of versions. |
|
126 |
|
127 @return list of versions |
|
128 @rtype list of QVersionNumber |
|
129 """ |
|
130 return [k for k in self._versionToNamespace.keys()] |
|
131 |
|
132 def namespaces(self): |
|
133 """ |
|
134 Public method to get the list of namespaces. |
|
135 |
|
136 @return list of namespaces |
|
137 @rtype list of str |
|
138 """ |
|
139 return [k for k in self._namespaceToFilename.keys()] |
|
140 |
|
141 def namespaceToFilename(self): |
|
142 """ |
|
143 Public method to get the namespace to filename mapping. |
|
144 |
|
145 @return dictionary containing the namespace to filename mapping |
|
146 @rtype dict |
|
147 """ |
|
148 return copy.deepcopy(self._namespaceToFilename) |
|
149 |
|
150 @staticmethod |
|
151 def readSettings(helpEngine): |
|
152 """ |
|
153 Static method to read the QtHelp documentation configuration. |
|
154 |
|
155 @param helpEngine reference to the QtHelp engine |
|
156 @type QHelpEngineCore |
|
157 @return reference to the created QtHelpDocumentationSettings object |
|
158 @rtype QtHelpDocumentationSettings |
|
159 """ |
|
160 filterEngine = helpEngine.filterEngine() |
|
161 |
|
162 docSettings = QtHelpDocumentationSettings() |
|
163 docSettings._namespaceToComponent = filterEngine.namespaceToComponent() |
|
164 docSettings._namespaceToVersion = filterEngine.namespaceToVersion() |
|
165 |
|
166 for namespace, component in docSettings._namespaceToComponent.items(): |
|
167 filename = helpEngine.documentationFileName(namespace) |
|
168 docSettings._namespaceToFilename[namespace] = filename |
|
169 docSettings._filenameToNamespace[filename] = namespace |
|
170 docSettings._componentToNamespace[component].append(namespace) |
|
171 |
|
172 for namespace, version in docSettings._namespaceToVersion.items(): |
|
173 docSettings._versionToNamespace[version].append(namespace) |
|
174 |
|
175 return docSettings |
|
176 |
|
177 @staticmethod |
|
178 def applySettings(helpEngine, settings): |
|
179 """ |
|
180 Static method to apply the changed QtHelp documentation configuration. |
|
181 |
|
182 @param helpEngine reference to the QtHelp engine |
|
183 @type QHelpEngineCore |
|
184 @param settings reference to the created QtHelpDocumentationSettings |
|
185 object |
|
186 @type QtHelpDocumentationSettings |
|
187 @return flag indicating success |
|
188 @rtype bool |
|
189 """ |
|
190 currentSettings = QtHelpDocumentationSettings.readSettings(helpEngine) |
|
191 |
|
192 docsToRemove = [name for name in currentSettings._namespaceToFilename |
|
193 if name not in settings._namespaceToFilename] |
|
194 docsToAdd = [filename for filename in settings._filenameToNamespace |
|
195 if filename not in currentSettings._filenameToNamespace] |
|
196 |
|
197 changed = False |
|
198 for namespace in docsToRemove: |
|
199 helpEngine.unregisterDocumentation(namespace) |
|
200 changed = True |
|
201 |
|
202 for filename in docsToAdd: |
|
203 helpEngine.registerDocumentation(filename) |
|
204 changed = True |
|
205 |
|
206 return changed |