19
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
20
#
21
21
22
+ """
23
+ Base class for Gramplet plugins
24
+ """
25
+
22
26
# -------------------------------------------------------------------------
23
27
#
24
28
# Python modules
27
31
import types
28
32
import logging
29
33
30
- LOG = logging .getLogger (".Gramplets" )
31
-
32
34
# ------------------------------------------------------------------------
33
35
#
34
36
# Gramps modules
37
39
from ...gui .dbguielement import DbGUIElement
38
40
from ..const import GRAMPS_LOCALE as glocale
39
41
42
+ LOG = logging .getLogger (".Gramplets" )
43
+
40
44
_ = glocale .translation .gettext
41
45
42
46
47
+ # ------------------------------------------------------------------------
48
+ #
49
+ # Gramplet class
50
+ #
51
+ # ------------------------------------------------------------------------
43
52
class Gramplet :
44
53
"""
45
54
Base class for non-graphical gramplet code.
@@ -94,17 +103,17 @@ def init(self): # once, constructor
94
103
External constructor for developers to put their initialization
95
104
code. Designed to be overridden.
96
105
"""
97
- pass
98
106
99
107
def post_init (self ):
100
- pass
108
+ """
109
+ Another for developers to hook into post initialization phase.
110
+ """
101
111
102
112
def build_options (self ):
103
113
"""
104
114
External constructor for developers to put code for building
105
115
options.
106
116
"""
107
- pass
108
117
109
118
def main (self ): # return false finishes
110
119
"""
@@ -118,14 +127,12 @@ def on_load(self):
118
127
Gramplets should override this to take care of loading previously
119
128
their special data.
120
129
"""
121
- pass
122
130
123
131
def on_save (self ):
124
132
"""
125
133
Gramplets should override this to take care of saving their
126
134
special data.
127
135
"""
128
- return
129
136
130
137
def get_active (self , nav_type ):
131
138
"""
@@ -138,7 +145,7 @@ def get_active_object(self, nav_type):
138
145
Return the object of the active handle for the given navigation type.
139
146
"""
140
147
handle = self .uistate .get_active (nav_type , self .nav_group )
141
- handle_func = getattr (self .dbstate .db , "get_%s_from_handle" % nav_type .lower ())
148
+ handle_func = getattr (self .dbstate .db , f "get_{ nav_type .lower ()} _from_handle" )
142
149
if handle :
143
150
return handle_func (handle )
144
151
return None
@@ -154,7 +161,6 @@ def active_changed(self, handle):
154
161
Developers should put their code that occurs when the active
155
162
person is changed.
156
163
"""
157
- pass
158
164
159
165
def _active_changed (self , handle ):
160
166
"""
@@ -166,7 +172,6 @@ def db_changed(self):
166
172
"""
167
173
Method executed when the database is changed.
168
174
"""
169
- pass
170
175
171
176
def link (self , text , link_type , data , size = None , tooltip = None ):
172
177
"""
@@ -260,7 +265,7 @@ def set_wrap(self, value):
260
265
textview .set_wrap_mode (Gtk .WrapMode .WORD_CHAR )
261
266
else :
262
267
raise ValueError (
263
- "Unknown wrap mode: '%s ': use 0,1,'char' or 'word char')" % value
268
+ f "Unknown wrap mode: '{ value } ': use 0,1,'char' or 'word char')"
264
269
)
265
270
266
271
def no_wrap (self ):
@@ -290,7 +295,7 @@ def save_text_to_data(self):
290
295
text = text .replace (chr (10 ), "\\ n" )
291
296
self .gui .data .append (text )
292
297
293
- def update (self , * args ):
298
+ def update (self , * _ ):
294
299
"""
295
300
The main interface for running the :meth:`main` method.
296
301
"""
@@ -306,7 +311,7 @@ def update(self, *args):
306
311
return
307
312
# print " %s is UPDATING" % self.gui.gname
308
313
self .dirty = False
309
- LOG .debug ("gramplet updater: %s: running" % self .gui .title )
314
+ LOG .debug ("gramplet updater: %s: running" , self .gui .title )
310
315
if self ._idle_id != 0 :
311
316
self .interrupt ()
312
317
self ._generator = self .main ()
@@ -317,29 +322,29 @@ def _updater(self):
317
322
"""
318
323
Runs the generator.
319
324
"""
320
- LOG .debug ("gramplet updater: %s" % self .gui .title )
325
+ LOG .debug ("gramplet updater: %s" , self .gui .title )
321
326
if not isinstance (self ._generator , types .GeneratorType ):
322
327
self ._idle_id = 0
323
- LOG .debug ("gramplet updater: %s : One time, done!" % self .gui .title )
328
+ LOG .debug ("gramplet updater: %s : One time, done!" , self .gui .title )
324
329
return False
325
330
try :
326
331
retval = next (self ._generator )
327
332
if not retval :
328
333
self ._idle_id = 0
329
334
if self ._pause :
330
- LOG .debug ("gramplet updater: %s: return False" % self .gui .title )
335
+ LOG .debug ("gramplet updater: %s: return False" , self .gui .title )
331
336
return False
332
- LOG .debug ("gramplet updater: %s: return %s" % ( self .gui .title , retval ) )
337
+ LOG .debug ("gramplet updater: %s: return %s" , self .gui .title , retval )
333
338
return retval
334
339
except StopIteration :
335
340
self ._idle_id = 0
336
341
self ._generator .close ()
337
- LOG .debug ("gramplet updater: %s: Done!" % self .gui .title )
342
+ LOG .debug ("gramplet updater: %s: Done!" , self .gui .title )
338
343
return False
339
- except Exception as e :
344
+ except Exception :
340
345
import traceback
341
346
342
- LOG .warning ("Gramplet gave an error: %s" % self .gui .title )
347
+ LOG .warning ("Gramplet gave an error: %s" , self .gui .title )
343
348
traceback .print_exc ()
344
349
print ("Continuing after gramplet error..." )
345
350
self ._idle_id = 0
@@ -348,13 +353,13 @@ def _updater(self):
348
353
)
349
354
return False
350
355
351
- def pause (self , * args ):
356
+ def pause (self , * _ ):
352
357
"""
353
358
Pause the :meth:`main` method.
354
359
"""
355
360
self ._pause = True
356
361
357
- def resume (self , * args ):
362
+ def resume (self , * _ ):
358
363
"""
359
364
Resume the :meth:`main` method that has previously paused.
360
365
"""
@@ -363,17 +368,17 @@ def resume(self, *args):
363
368
self ._pause = False
364
369
self ._idle_id = GLib .idle_add (self ._updater , priority = GLib .PRIORITY_LOW - 10 )
365
370
366
- def update_all (self , * args ):
371
+ def update_all (self , * _ ):
367
372
"""
368
373
Force the main loop to run right now (as opposed to running in
369
374
background).
370
375
"""
371
376
self ._generator = self .main ()
372
377
if isinstance (self ._generator , types .GeneratorType ):
373
- for step in self ._generator :
378
+ for _ in self ._generator :
374
379
pass
375
380
376
- def interrupt (self , * args ):
381
+ def interrupt (self , * _ ):
377
382
"""
378
383
Force the generator to stop running.
379
384
"""
@@ -421,15 +426,15 @@ def get_option_widget(self, label):
421
426
422
427
def get_option (self , label ):
423
428
"""
424
- Retireve an option by its label text.
429
+ Retrieve an option by its label text.
425
430
"""
426
431
return self .option_dict [label ][1 ]
427
432
428
433
def add_option (self , option ):
429
434
"""
430
435
Add an option to the GUI gramplet.
431
436
"""
432
- widget , label = self .gui .add_gui_option (option )
437
+ widget , _ = self .gui .add_gui_option (option )
433
438
self .option_dict .update ({option .get_label (): [widget , option ]})
434
439
self .option_order .append (option .get_label ())
435
440
@@ -441,29 +446,37 @@ def save_update_options(self, obj):
441
446
self .update ()
442
447
443
448
def save_options (self ):
444
- pass
449
+ """
450
+ Save a gramplet's options.
451
+ """
445
452
446
453
def connect (self , signal_obj , signal , method ):
447
- id = signal_obj .connect (signal , method )
454
+ """
455
+ Connect signals.
456
+ """
457
+ signal_id = signal_obj .connect (signal , method )
448
458
signal_list = self ._signal .get (signal , [])
449
- signal_list .append ((id , signal_obj ))
459
+ signal_list .append ((signal_id , signal_obj ))
450
460
self ._signal [signal ] = signal_list
451
461
452
462
def disconnect (self , signal ):
463
+ """
464
+ Disconnect signals.
465
+ """
453
466
if signal in self ._signal :
454
- for id , signal_obj in self ._signal [signal ]:
455
- signal_obj .disconnect (id )
467
+ for signal_id , signal_obj in self ._signal [signal ]:
468
+ signal_obj .disconnect (signal_id )
456
469
self ._signal [signal ] = []
457
470
else :
458
- raise AttributeError ("unknown signal: '%s'" % signal )
471
+ raise AttributeError (f "unknown signal: '{ signal } '" )
459
472
460
473
def disconnect_all (self ):
461
474
"""
462
475
Used to disconnect all the signals for this specific gramplet
463
476
"""
464
- for signal in self ._signal :
465
- for sig_id , signal_obj in self . _signal [ signal ] :
466
- signal_obj .disconnect (sig_id )
477
+ for signal , signal_data in self ._signal . items () :
478
+ for signal_id , signal_obj in signal_data :
479
+ signal_obj .disconnect (signal_id )
467
480
self ._signal [signal ] = []
468
481
469
482
def hidden_widgets (self ):
@@ -495,4 +508,3 @@ def set_orientation(self, orientation):
495
508
:param orientation: A Gtk.Orientation (VERTCIAL or HORIZONTAL)
496
509
:type orientation: int
497
510
"""
498
- pass
0 commit comments