forked from lb803/access-aide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
362 lines (291 loc) · 13.2 KB
/
config.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python3
'''
Access Aide - A Calibre plugin to enhance accessibility features in epub files.
Copyright (C) 2020-2021 Luca Baffa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from PyQt5.Qt import QWidget, QHBoxLayout, QVBoxLayout, QFormLayout, QCheckBox, QGroupBox, QLabel, QLineEdit, QRadioButton, QGridLayout, QPushButton, QIcon, QPixmap, QCompleter
from PyQt5.QtCore import Qt
from calibre.utils.config import JSONConfig
import webbrowser
import json
prefs = JSONConfig('plugins/access_aide')
# Set defaults
prefs.defaults['force_override'] = False
prefs.defaults['heuristic'] = {
'title_override': False,
'type_footnotes': False
}
prefs.defaults['access'] = {
'accessibilitySummary': ['This publication conforms to WCAG 2.0 AA.'],
'accessMode': ['textual', 'visual'],
'accessModeSufficient': ['textual'],
'accessibilityFeature': ['structuralNavigation', 'alternativeText'],
'accessibilityHazard': ['unknown']
}
prefs.defaults['a11y'] = {
'enabled': False,
'certifiedBy': '',
'certifierCredential': '',
'certifierReport': ''
}
prefs.defaults['dcterms'] = {
'conformsTo': ''
}
class Completer(QCompleter):
def __init__(self, *args, **kwargs):
super(Completer, self).__init__(*args, **kwargs)
self.setFilterMode(Qt.MatchContains)
self.setCaseSensitivity(Qt.CaseInsensitive)
self.setCompletionMode(QCompleter.PopupCompletion)
# Add texts instead of replace
def pathFromIndex(self, index):
path = QCompleter.pathFromIndex(self, index)
lst = str(self.widget().text()).split(' ')
if len(lst) > 1:
path = '%s %s' % (' '.join(lst[:-1]), path)
return path
# Add operator to separate between texts
def splitPath(self, path):
path = str(path.split(' ')[-1]).lstrip(' ')
return [path]
class ConfigWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
grid = QGridLayout()
grid.addWidget(self.general_group(), 0, 0, 1, 1)
grid.addWidget(self.heuristic_group(), 0, 1, 1, 1)
grid.addWidget(self.access_group(), 1, 0, 1, 2)
grid.addWidget(self.conform_group(), 2, 0, 1, 2)
grid.addLayout(self.buttons_group(), 3, 0, 1, 2)
self.setLayout(grid)
def general_group(self):
group_box = QGroupBox('General Preferences', self)
self.force_override = QCheckBox('Force Override', self)
self.force_override.setToolTip('When checked, existing HTML '
'attributes and values will be '
'overwritten.')
self.force_override.setChecked(prefs['force_override'])
vbox = QVBoxLayout()
vbox.addWidget(self.force_override)
vbox.addStretch(1)
group_box.setLayout(vbox)
return group_box
def heuristic_group(self):
group_box = QGroupBox('Heuristic Options', self)
self.title_override = QCheckBox('Match <title> text with <h1>', self)
self.title_override.setToolTip('When checked, replaces '
'the existing <title> text with'
'the first <h1> found on the page')
try:
self.title_override \
.setChecked(prefs['heuristic']['title_override'])
except KeyError:
self.title_override.setChecked(False)
self.type_fn = QCheckBox('Add epub:type to footnote and endnote '
'marks', self)
self.type_fn.setToolTip('When checked, adds corresponding epub:type '
'to footnote and endnote marks.')
try:
self.type_fn.setChecked(prefs['heuristic']['type_footnotes'])
except KeyError:
self.type_fn.setChecked(False)
vbox = QVBoxLayout()
vbox.addWidget(self.title_override)
vbox.addWidget(self.type_fn)
vbox.addStretch(1)
group_box.setLayout(vbox)
return group_box
def access_group(self):
group_box = QGroupBox('Accessibility', self)
# accessibilitySummary
self.acc_summ = QLineEdit(self)
self.acc_summ.setText(prefs['access']['accessibilitySummary'][0])
# accessMode
self.acc_mode_t = QCheckBox('Textual', self)
if 'textual' in prefs['access']['accessMode']:
self.acc_mode_t.setChecked(True)
else:
self.acc_mode_t.setChecked(False)
self.acc_mode_v = QCheckBox('Visual', self)
if 'visual' in prefs['access']['accessMode']:
self.acc_mode_v.setChecked(True)
else:
self.acc_mode_v.setChecked(False)
acc_mode_box = QVBoxLayout()
acc_mode_box.addWidget(self.acc_mode_t)
acc_mode_box.addWidget(self.acc_mode_v)
# accessModeSufficient
self.acc_suff_t = QRadioButton('Textual')
if 'textual' in prefs['access']['accessModeSufficient']:
self.acc_suff_t.setChecked(True)
self.acc_suff_v = QRadioButton('Visual')
if 'visual' in prefs['access']['accessModeSufficient']:
self.acc_suff_v.setChecked(True)
acc_suff_box = QVBoxLayout()
acc_suff_box.addWidget(self.acc_suff_t)
acc_suff_box.addWidget(self.acc_suff_v)
# accessibilityFeature
self.acc_feat = QLineEdit(self)
acc_feat_list = prefs.get('access', {}).get('accessibilityFeature', [])
self.acc_feat.setText(' '.join(acc_feat_list))
self.acc_feat.setToolTip('schema:accessibilityFeature metadata '
'propriety. Separate values with space.')
self.acc_feat.setPlaceholderText('structuralNavigation '
'alternativeText')
feat_list = json.loads(get_resources('assets/acc_feature_values.json'))
completer = Completer(feat_list)
self.acc_feat.setCompleter(completer)
# accessibilityHazard
self.acc_hazard_none = QCheckBox('None', self)
if 'none' in prefs['access'].get('accessibilityHazard', []):
self.acc_hazard_none.setChecked(True)
else:
self.acc_hazard_none.setChecked(False)
self.acc_hazard_unknown = QCheckBox('Unknown', self)
if 'unknown' in prefs['access'].get('accessibilityHazard', []):
self.acc_hazard_unknown.setChecked(True)
else:
self.acc_hazard_unknown.setChecked(False)
self.acc_hazard_f = QCheckBox('Flashing', self)
if 'flashing' in prefs['access'].get('accessibilityHazard', []):
self.acc_hazard_f.setChecked(True)
else:
self.acc_hazard_f.setChecked(False)
self.acc_hazard_m = QCheckBox('Motion Simulation', self)
if 'motionSimulation' in prefs['access'] \
.get('accessibilityHazard', []):
self.acc_hazard_m.setChecked(True)
else:
self.acc_hazard_m.setChecked(False)
self.acc_hazard_s = QCheckBox('Sound', self)
if 'sound' in prefs['access'].get('accessibilityHazard', []):
self.acc_hazard_s.setChecked(True)
else:
self.acc_hazard_s.setChecked(False)
acc_hazard_box = QVBoxLayout()
acc_hazard_box.addWidget(self.acc_hazard_none)
acc_hazard_box.addWidget(self.acc_hazard_unknown)
acc_hazard_box.addWidget(self.acc_hazard_f)
acc_hazard_box.addWidget(self.acc_hazard_m)
acc_hazard_box.addWidget(self.acc_hazard_s)
fbox = QFormLayout()
fbox.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
fbox.addRow(QLabel('Accessibility Summary:'), self.acc_summ)
fbox.addRow(QLabel('Access Mode:'), acc_mode_box)
fbox.addRow(QLabel('Access Mode Sufficient:'), acc_suff_box)
fbox.addRow(QLabel('Accessibility Feature:'), self.acc_feat)
fbox.addRow(QLabel('Accessibility Hazard:'), acc_hazard_box)
group_box.setLayout(fbox)
return group_box
def conform_group(self):
self.conform_box = QGroupBox('Conformance Properties', self)
self.conform_box.setCheckable(True)
self.conform_box.setChecked(prefs.get('a11y', {}).get('enabled', False))
self.conform_box.setToolTip('Enable conformance metadata proprieties')
self.conform_to = QLineEdit(prefs.get('dcterms', {}) \
.get('conformsTo', ''))
self.conform_to.setToolTip('dcterms:conformsTo metadata propriety')
self.conform_to.setPlaceholderText('http://www.idpf.org/epub/a11y/'
'accessibility-20170105.html'
'#wcag-aa')
self.a11y_by = QLineEdit(prefs.get('a11y', {}).get('certifiedBy', ''))
self.a11y_by.setToolTip('a11y:certifiedBy metadata propriety')
self.a11y_by.setPlaceholderText('Book Company Ltd')
self.a11y_credential = QLineEdit(prefs.get('a11y', {}) \
.get('certifierCredential', ''))
self.a11y_credential.setToolTip('a11y:certifierCredential metadata '
'propriety')
self.a11y_credential.setPlaceholderText('DAISY OK')
self.a11y_report = QLineEdit(prefs.get('a11y', {}) \
.get('certifierReport', ''))
self.a11y_report.setToolTip('a11y:certifierReport metadata propriety')
self.a11y_report.setPlaceholderText('https://www.link.to/report.html')
fbox = QFormLayout()
fbox.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
fbox.addRow(QLabel('Conformance URL:'), self.conform_to)
fbox.addRow(QLabel('Certified by:'), self.a11y_by)
fbox.addRow(QLabel('Certifier Credential:'), self.a11y_credential)
fbox.addRow(QLabel('Report URL:'), self.a11y_report)
self.conform_box.setLayout(fbox)
return self.conform_box
def buttons_group(self):
github_button = QPushButton('Source code')
github_button.clicked.connect(self.github)
github_logo = QPixmap()
github_logo.loadFromData(get_resources('icon/GitHub-Mark-32px.png'))
github_button.setIcon(QIcon(github_logo))
forum_button = QPushButton('⌨ Calibre Forum')
forum_button.clicked.connect(self.forum)
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(github_button)
hbox.addWidget(forum_button)
hbox.addStretch(1)
return hbox
def github(self):
webbrowser.open('https://github.com/lb803/access-aide')
def forum(self):
webbrowser.open('https://www.mobileread.com/forums/showthread.php?t=337132')
def save_settings(self):
# accessMode
access_mode = []
if self.acc_mode_t.isChecked():
access_mode.append('textual')
if self.acc_mode_v.isChecked():
access_mode.append('visual')
# accessModeSufficient
access_mode_suff = ''
if self.acc_suff_t.isChecked():
access_mode_suff = 'textual'
else:
access_mode_suff = 'visual'
# accessibilityHazard
access_hazard = []
if self.acc_hazard_none.isChecked():
access_hazard.append('none')
elif self.acc_hazard_unknown.isChecked():
access_hazard.append('unknown')
else:
if self.acc_hazard_f.isChecked():
access_hazard.append('flashing')
else:
access_hazard.append('noFlashingHazard')
if self.acc_hazard_m.isChecked():
access_hazard.append('motionSimulation')
else:
access_hazard.append('noMotionSimulationHazard')
if self.acc_hazard_s.isChecked():
access_hazard.append('sound')
else:
access_hazard.append('noSoundHazard')
prefs['force_override'] = self.force_override.isChecked()
prefs['heuristic'] = {
'title_override': self.title_override.isChecked(),
'type_footnotes': self.type_fn.isChecked()
}
prefs['access'] = {
'accessibilitySummary': [self.acc_summ.text()],
'accessMode': access_mode,
'accessModeSufficient': [access_mode_suff],
'accessibilityFeature': self.acc_feat.text().split(),
'accessibilityHazard': access_hazard
}
prefs['a11y'] = {
'enabled': self.conform_box.isChecked(),
'certifiedBy': self.a11y_by.text(),
'certifierCredential': self.a11y_credential.text(),
'certifierReport': self.a11y_report.text()
}
prefs['dcterms'] = {
'conformsTo': self.conform_to.text()
}