Skip to content

Commit 6e96b13

Browse files
committed
cleanups: use get/set methods instead of direct access
1 parent e19f1a1 commit 6e96b13

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/diffuse/preferences.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ def __init__(self, path: str) -> None:
208208
if len(a) > 0:
209209
p = a[0]
210210
if len(a) == 2 and p in self.bool_prefs:
211-
self.bool_prefs[p] = (a[1] == 'True')
211+
self.setBool(p, a[1] == 'True')
212212
elif len(a) == 2 and p in self.int_prefs:
213-
self.int_prefs[p] = max(
213+
self.setInt(p, max(
214214
self.int_prefs_min[p],
215-
min(int(a[1]), self.int_prefs_max[p]))
215+
min(int(a[1]), self.int_prefs_max[p])))
216216
elif len(a) == 2 and p in self.string_prefs:
217-
self.string_prefs[p] = a[1]
217+
self.setString(p, a[1])
218218
else:
219219
raise ValueError()
220220
except ValueError:
@@ -237,13 +237,13 @@ def _initFromTemplate(self, template):
237237
self._initFromTemplate(template[i])
238238
i += 1
239239
elif template[0] == 'Boolean':
240-
self.bool_prefs[template[1]] = template[2]
240+
self.setBool(template[1], template[2])
241241
elif template[0] == 'Integer':
242-
self.int_prefs[template[1]] = template[2]
242+
self.setInt(template[1], template[2])
243243
self.int_prefs_min[template[1]] = template[4]
244244
self.int_prefs_max[template[1]] = template[5]
245245
elif template[0] in ['String', 'File', 'Font', 'Encoding']:
246-
self.string_prefs[template[1]] = template[2]
246+
self.setString(template[1], template[2])
247247

248248
# callback used when a preference is toggled
249249
def _toggled_cb(self, widget, widgets, name):
@@ -273,12 +273,12 @@ def runDialog(self, parent: Gtk.Widget) -> None:
273273
accept = (dialog.run() == Gtk.ResponseType.OK)
274274
if accept:
275275
for k in self.bool_prefs:
276-
self.bool_prefs[k] = widgets[k].get_active()
276+
self.setBool(k, widgets[k].get_active())
277277
for k in self.int_prefs:
278-
self.int_prefs[k] = widgets[k].get_value_as_int()
278+
self.setInt(k, widgets[k].get_value_as_int())
279279
for k in self.string_prefs:
280280
text = self._getWidgetText(widgets[k])
281-
self.string_prefs[k] = utils.null_to_empty(text)
281+
self.setString(k, utils.null_to_empty(text))
282282
try:
283283
ss = []
284284
for k, bool_value in self.bool_prefs.items():
@@ -330,7 +330,7 @@ def _buildPrefsDialog(self, parent, widgets, template):
330330
w.show()
331331
elif tpl_section == 'Boolean':
332332
button = Gtk.CheckButton.new_with_mnemonic(tpl[3])
333-
button.set_active(self.bool_prefs[tpl[1]])
333+
button.set_active(self.getBool(tpl[1]))
334334
widgets[tpl[1]] = button
335335
table.attach(button, 1, i, 1, 1)
336336
button.connect('toggled', self._toggled_cb, widgets, tpl[1])
@@ -341,14 +341,14 @@ def _buildPrefsDialog(self, parent, widgets, template):
341341
label.set_yalign(0.5)
342342
table.attach(label, 0, i, 1, 1)
343343
label.show()
344-
if tpl[0] in ['Font', 'Integer']:
344+
if tpl_section in ['Font', 'Integer']:
345345
entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
346-
if tpl[0] == 'Font':
346+
if tpl_section == 'Font':
347347
button = Gtk.FontButton()
348-
button.set_font(self.string_prefs[tpl[1]])
348+
button.set_font(self.getString(tpl[1]))
349349
else:
350350
adj = Gtk.Adjustment(
351-
value=self.int_prefs[tpl[1]],
351+
value=self.getInt(tpl[1]),
352352
lower=tpl[4],
353353
upper=tpl[5],
354354
step_increment=1,
@@ -362,15 +362,15 @@ def _buildPrefsDialog(self, parent, widgets, template):
362362
entry.pack_start(button, False, False, 0)
363363
button.show()
364364
else:
365-
if tpl[0] == 'Encoding':
365+
if tpl_section == 'Encoding':
366366
entry = utils.EncodingMenu(self)
367367
entry.set_text(tpl[3])
368-
elif tpl[0] == 'File':
368+
elif tpl_section == 'File':
369369
entry = _FileEntry(parent, tpl[3])
370370
else:
371371
entry = Gtk.Entry()
372372
widgets[tpl[1]] = entry
373-
entry.set_text(self.string_prefs[tpl[1]])
373+
entry.set_text(self.getString(tpl[1]))
374374
table.attach(entry, 1, i, 1, 1)
375375
entry.show()
376376
table.show()
@@ -400,6 +400,9 @@ def setBool(self, name: str, value: bool) -> None:
400400
def getInt(self, name: str) -> int:
401401
return self.int_prefs[name]
402402

403+
def setInt(self, name: str, value: int) -> None:
404+
self.int_prefs[name] = value
405+
403406
def getString(self, name: str) -> str:
404407
return self.string_prefs[name]
405408

@@ -410,10 +413,10 @@ def getEncodings(self) -> List[Optional[str]]:
410413
return self.encodings
411414

412415
def _getDefaultEncodings(self) -> List[str]:
413-
return self.string_prefs['encoding_auto_detect_codecs'].split()
416+
return self.getString('encoding_auto_detect_codecs').split()
414417

415418
def getDefaultEncoding(self) -> str:
416-
return self.string_prefs['encoding_default_codec']
419+
return self.getString('encoding_default_codec')
417420

418421
# attempt to convert a string to unicode from an unknown encoding
419422
def convertToUnicode(self, s):

0 commit comments

Comments
 (0)