[2796d7]: gramps / plugins / lib / libmixin.py Maximize Restore History

Download this file

libmixin.py    346 lines (284 with data), 14.2 kB

  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
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
Mixin for DbDir to enable find_from_handle and check_from_handle methods.
"""
#------------------------------------------------------------------------------
#
# Gramps Modules
#
#------------------------------------------------------------------------------
from gramps.gen.lib import (Person, Family, Event, Place, Source,
MediaObject, Repository, Note, Tag)
#------------------------------------------------------------------------------
#
# DbMixin class
#
#------------------------------------------------------------------------------
class DbMixin(object):
"""
DbMixin -- a collection of methods to be added to the main
gramps database class for use with import functions. To enable these
functions, add the following code to your module:
if DbMixin not in database.__class__.__bases__:
database.__class__.__bases__ = (DbMixin,) + \
database.__class__.__bases__
where "database" is the object name of your instance of the gramps
database.
"""
def __find_primary_from_handle(self, handle, transaction, class_type,
get_raw_obj_data, add_func):
"""
Find a primary object of class_type in the database from the passed
handle.
If no object exists, a new object is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
obj = class_type()
handle = str(handle)
new = True
raw = get_raw_obj_data(handle)
if raw is not None:
obj.unserialize(raw)
#references create object with id None before object is really made
if obj.gramps_id is not None:
new = False
else:
obj.set_handle(handle)
add_func(obj, transaction)
return obj, new
def __find_table_from_handle(self, handle, transaction, class_type,
get_raw_obj_data, add_func):
"""
Find a table object of class_type in the database from the passed
handle.
If no object exists, a new object is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
obj = class_type()
handle = str(handle)
raw = get_raw_obj_data(handle)
if raw is not None:
obj.unserialize(raw)
return obj, False
else:
obj.set_handle(handle)
add_func(obj, transaction)
return obj, True
def __check_primary_from_handle(self, handle, transaction, class_type,
has_handle_func, add_func, set_gid):
"""
Check whether a primary object of class_type with the passed handle
exists in the database.
If no such object exists, a new object is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
handle = str(handle)
if not has_handle_func(handle):
obj = class_type()
obj.set_handle(handle)
add_func(obj, transaction, set_gid=set_gid)
def __check_table_from_handle(self, handle, transaction, class_type,
has_handle_func, add_func):
"""
Check whether a table object of class_type with the passed handle exists
in the database.
If no such object exists, a new object is added to the database.
"""
handle = str(handle)
if not has_handle_func(handle):
obj = class_type()
obj.set_handle(handle)
add_func(obj, transaction)
def find_person_from_handle(self, handle, transaction):
"""
Find a Person in the database from the passed handle.
If no such Person exists, a new Person is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Person,
self.get_raw_person_data, self.add_person)
def find_source_from_handle(self, handle, transaction):
"""
Find a Source in the database from the passed handle.
If no such Source exists, a new Source is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Source,
self.get_raw_source_data, self.add_source)
def find_event_from_handle(self, handle, transaction):
"""
Find a Event in the database from the passed handle.
If no such Event exists, a new Event is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Event,
self.get_raw_event_data, self.add_event)
def find_object_from_handle(self, handle, transaction):
"""
Find a MediaObject in the database from the passed handle.
If no such MediaObject exists, a new Object is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, MediaObject,
self.get_raw_object_data, self.add_object)
def find_place_from_handle(self, handle, transaction):
"""
Find a Place in the database from the passed handle.
If no such Place exists, a new Place is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Place,
self.get_raw_place_data, self.add_place)
def find_family_from_handle(self, handle, transaction):
"""
Find a Family in the database from the passed handle.
If no such Family exists, a new Family is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Family,
self.get_raw_family_data, self.add_family)
def find_repository_from_handle(self, handle, transaction):
"""
Find a Repository in the database from the passed handle.
If no such Repository exists, a new Repository is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Repository,
self.get_raw_repository_data, self.add_repository)
def find_note_from_handle(self, handle, transaction):
"""
Find a Note in the database from the passed handle.
If no such Note exists, a new Note is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_primary_from_handle(handle, transaction, Note,
self.get_raw_note_data, self.add_note)
def find_tag_from_handle(self, handle, transaction):
"""
Find a Tag in the database from the passed handle.
If no such Tag exists, a new Tag is added to the database.
@return: Returns a tuple, first the object, second a bool which is True
if the object is new
@rtype: tuple
"""
return self.__find_table_from_handle(handle, transaction, Tag,
self.get_raw_tag_data, self.add_tag)
def check_person_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Person with the passed handle exists in the database.
If no such Person exists, a new Person is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Person,
self.has_person_handle, self.add_person,
set_gid = set_gid)
def check_source_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Source with the passed handle exists in the database.
If no such Source exists, a new Source is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Source,
self.has_source_handle, self.add_source,
set_gid=set_gid)
def check_event_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether an Event with the passed handle exists in the database.
If no such Event exists, a new Event is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Event,
self.has_event_handle, self.add_event,
set_gid=set_gid)
def check_object_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a MediaObject with the passed handle exists in the
database.
If no such MediaObject exists, a new Object is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, MediaObject,
self.has_object_handle, self.add_object,
set_gid=set_gid)
def check_place_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Place with the passed handle exists in the database.
If no such Place exists, a new Place is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Place,
self.has_place_handle, self.add_place,
set_gid=set_gid)
def check_family_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Family with the passed handle exists in the database.
If no such Family exists, a new Family is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Family,
self.has_family_handle, self.add_family,
set_gid=set_gid)
def check_repository_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Repository with the passed handle exists in the
database.
If no such Repository exists, a new Repository is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Repository,
self.has_repository_handle, self.add_repository,
set_gid=set_gid)
def check_note_from_handle(self, handle, transaction, set_gid=True):
"""
Check whether a Note with the passed handle exists in the database.
If no such Note exists, a new Note is added to the database.
If set_gid then a new gramps_id is created, if not, None is used.
"""
self.__check_primary_from_handle(handle, transaction, Note,
self.has_note_handle, self.add_note,
set_gid=set_gid)
def check_tag_from_handle(self, handle, transaction):
"""
Check whether a Tag with the passed handle exists in the database.
If no such Tag exists, a new Tag is added to the database.
"""
self.__check_table_from_handle(handle, transaction, Tag,
self.has_tag_handle, self.add_tag)