Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[1ef95d]: gramps / plugins / textreport / placereport.py Maximize Restore History

Download this file

placereport.py    534 lines (468 with data), 21.5 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2008,2011 Gary Burton
# Copyright (C) 2010 Jakim Friant
# Copyright (C) 2011 Heinz Brinker
# Copyright (C) 2013-2014 Paul Franklin
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Place Report"""
#------------------------------------------------------------------------
#
# python modules
#
#------------------------------------------------------------------------
#------------------------------------------------------------------------
#
# gramps modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from gramps.gen.plug.menu import (FilterOption, PlaceListOption,
EnumeratedListOption, BooleanOption)
from gramps.gen.plug.report import Report
from gramps.gen.plug.report import MenuReportOptions
from gramps.gen.plug.report import stdoptions
from gramps.gen.plug.docgen import (IndexMark, FontStyle, ParagraphStyle,
TableStyle, TableCellStyle,
FONT_SANS_SERIF, FONT_SERIF,
INDEX_TYPE_TOC, PARA_ALIGN_CENTER)
from gramps.gen.proxy import PrivateProxyDb
from gramps.gen.sort import Sort
from gramps.gen.utils.location import get_main_location
from gramps.gen.lib import PlaceType
class PlaceReport(Report):
"""
Place Report class
"""
def __init__(self, database, options, user):
"""
Create the PlaceReport object produces the Place report.
The arguments are:
database - the GRAMPS database instance
options - instance of the Options class for this report
user - instance of a gen.user.User class
This report needs the following parameters (class variables)
that come in the options class.
places - List of places to report on.
center - Center of report, person or event
incl_private - Whether to include private data
name_format - Preferred format to display names
"""
Report.__init__(self, database, options, user)
self._user = user
menu = options.menu
places = menu.get_option_by_name('places').get_value()
self.center = menu.get_option_by_name('center').get_value()
incl_private = menu.get_option_by_name('incl_private').get_value()
self.set_locale(menu.get_option_by_name('trans').get_value())
stdoptions.run_name_format_option(self, menu)
self._nd = self._name_display
if incl_private:
self.database = database
else:
self.database = PrivateProxyDb(database)
filter_option = menu.get_option_by_name('filter')
self.filter = filter_option.get_filter()
self.sort = Sort(self.database)
if self.filter.get_name() != '':
# Use the selected filter to provide a list of place handles
plist = self.database.iter_place_handles()
self.place_handles = self.filter.apply(self.database, plist)
else:
# Use the place handles selected without a filter
self.place_handles = self.__get_place_handles(places)
self.place_handles.sort(key=self.sort.by_place_title_key)
def write_report(self):
"""
The routine that actually creates the report.
At this point, the document is opened and ready for writing.
"""
# Write the title line. Set in INDEX marker so that this section will be
# identified as a major category if this is included in a Book report.
title = self._("Place Report")
mark = IndexMark(title, INDEX_TYPE_TOC, 1)
self.doc.start_paragraph("PLC-ReportTitle")
self.doc.write_text(title, mark)
self.doc.end_paragraph()
self.__write_all_places()
def __write_all_places(self):
"""
This procedure writes out each of the selected places.
"""
place_nbr = 1
with self._user.progress(_("Place Report"),
_("Generating report"),
len(self.place_handles)) as step:
for handle in self.place_handles:
self.__write_place(handle, place_nbr)
if self.center == "Event":
self.__write_referenced_events(handle)
elif self.center == "Person":
self.__write_referenced_persons(handle)
else:
raise AttributeError("no such center: '%s'" % self.center)
place_nbr += 1
# increment progress bar
step()
def __write_place(self, handle, place_nbr):
"""
This procedure writes out the details of a single place
"""
place = self.database.get_place_from_handle(handle)
location = get_main_location(self.database, place)
place_details = [
self._("Gramps ID: %s ") % place.get_gramps_id(),
self._("Street: %s ") % location.get(PlaceType.STREET, ''),
self._("Parish: %s ") % location.get(PlaceType.PARISH, ''),
self._("Locality: %s ") % location.get(PlaceType.LOCALITY, ''),
self._("City: %s ") % location.get(PlaceType.CITY, ''),
self._("County: %s ") % location.get(PlaceType.COUNTY, ''),
self._("State: %s") % location.get(PlaceType.STATE, ''),
self._("Country: %s ") % location.get(PlaceType.COUNTRY, '')]
self.doc.start_paragraph("PLC-PlaceTitle")
self.doc.write_text(("%(nbr)s. %(place)s") %
{'nbr' : place_nbr,
'place' : place.get_title()})
self.doc.end_paragraph()
for item in place_details:
self.doc.start_paragraph("PLC-PlaceDetails")
self.doc.write_text(item)
self.doc.end_paragraph()
def __write_referenced_events(self, handle):
"""
This procedure writes out each of the events related to the place
"""
event_handles = [event_handle for (object_type, event_handle) in
self.database.find_backlink_handles(handle,['Event'])]
event_handles.sort(key=self.sort.by_date_key)
if event_handles:
self.doc.start_paragraph("PLC-Section")
title = self._("Events that happened at this place")
self.doc.write_text(title)
self.doc.end_paragraph()
self.doc.start_table("EventTable", "PLC-EventTable")
column_titles = [self._("Date"), self._("Type of Event"),
self._("Person"), self._("Description")]
self.doc.start_row()
for title in column_titles:
self.doc.start_cell("PLC-TableColumn")
self.doc.start_paragraph("PLC-ColumnTitle")
self.doc.write_text(title)
self.doc.end_paragraph()
self.doc.end_cell()
self.doc.end_row()
for evt_handle in event_handles:
event = self.database.get_event_from_handle(evt_handle)
if event:
date = self._get_date(event.get_date_object())
descr = event.get_description()
event_type = self._(self._get_type(event.get_type()))
person_list = []
ref_handles = [x for x in
self.database.find_backlink_handles(evt_handle)]
for (ref_type, ref_handle) in ref_handles:
if ref_type == 'Person':
person_list.append(ref_handle)
else:
family = self.database.get_family_from_handle(ref_handle)
father = family.get_father_handle()
if father:
person_list.append(father)
mother = family.get_mother_handle()
if mother:
person_list.append(mother)
people = ""
person_list = list(set(person_list))
for p_handle in person_list:
person = self.database.get_person_from_handle(p_handle)
if person:
if people == "":
people = "%(name)s (%(id)s)" \
% {'name': self._nd.display(person),
'id': person.get_gramps_id()}
else:
people = self._("%(persons)s and %(name)s "
"(%(id)s)") \
% {'persons': people,
'name': self._nd.display(person),
'id': person.get_gramps_id()}
event_details = [date, event_type, people, descr]
self.doc.start_row()
for detail in event_details:
self.doc.start_cell("PLC-Cell")
self.doc.start_paragraph("PLC-Details")
self.doc.write_text("%s " % detail)
self.doc.end_paragraph()
self.doc.end_cell()
self.doc.end_row()
if event_handles:
self.doc.end_table()
def __write_referenced_persons(self, handle):
"""
This procedure writes out each of the people related to the place
"""
event_handles = [event_handle for (object_type, event_handle) in
self.database.find_backlink_handles(handle)]
if event_handles:
self.doc.start_paragraph("PLC-Section")
title = self._("People associated with this place")
self.doc.write_text(title)
self.doc.end_paragraph()
self.doc.start_table("EventTable", "PLC-PersonTable")
column_titles = [self._("Person"), self._("Type of Event"), \
self._("Description"), self._("Date")]
self.doc.start_row()
for title in column_titles:
self.doc.start_cell("PLC-TableColumn")
self.doc.start_paragraph("PLC-ColumnTitle")
self.doc.write_text(title)
self.doc.end_paragraph()
self.doc.end_cell()
self.doc.end_row()
person_dict = {}
for evt_handle in event_handles:
ref_handles = [x for x in
self.database.find_backlink_handles(evt_handle)]
for (ref_type, ref_handle) in ref_handles:
if ref_type == 'Person':
person = self.database.get_person_from_handle(ref_handle)
nameEntry = "%s (%s)" % (self._nd.display(person),
person.get_gramps_id())
if nameEntry in person_dict:
person_dict[nameEntry].append(evt_handle)
else:
person_dict[nameEntry] = []
person_dict[nameEntry].append(evt_handle)
else:
family = self.database.get_family_from_handle(ref_handle)
f_handle = family.get_father_handle()
m_handle = family.get_mother_handle()
if f_handle and m_handle:
father = self.database.get_person_from_handle(f_handle)
mother = self.database.get_person_from_handle(m_handle)
nameEntry = self._("%(father)s (%(father_id)s) and "
"%(mother)s (%(mother_id)s)") % \
{ 'father' : self._nd.display(father),
'father_id' : father.get_gramps_id(),
'mother' : self._nd.display(mother),
'mother_id' : mother.get_gramps_id()}
else:
if f_handle:
p_handle = f_handle
else:
p_handle = m_handle
person = self.database.get_person_from_handle(p_handle)
nameEntry = "%s (%s)" % \
(self._nd.display(person),
person.get_gramps_id())
if nameEntry in person_dict:
person_dict[nameEntry].append(evt_handle)
else:
person_dict[nameEntry] = []
person_dict[nameEntry].append(evt_handle)
keys = list(person_dict.keys())
keys.sort()
for entry in keys:
people = entry
person_dict[entry].sort(key=self.sort.by_date_key)
for evt_handle in person_dict[entry]:
event = self.database.get_event_from_handle(evt_handle)
if event:
date = self._get_date(event.get_date_object())
descr = event.get_description()
event_type = self._(self._get_type(event.get_type()))
event_details = [people, event_type, descr, date]
self.doc.start_row()
for detail in event_details:
self.doc.start_cell("PLC-Cell")
self.doc.start_paragraph("PLC-Details")
self.doc.write_text("%s " % detail)
self.doc.end_paragraph()
self.doc.end_cell()
people = "" # do not repeat the name on the next event
self.doc.end_row()
if event_handles:
self.doc.end_table()
def __get_place_handles(self, places):
"""
This procedure converts a string of place GIDs to a list of handles
"""
place_handles = []
for place_gid in places.split():
place = self.database.get_place_from_gramps_id(place_gid)
if place is not None:
#place can be None if option is gid of other fam tree
place_handles.append(place.get_handle())
return place_handles
#------------------------------------------------------------------------
#
# PlaceOptions
#
#------------------------------------------------------------------------
class PlaceOptions(MenuReportOptions):
"""
Defines options and provides handling interface.
"""
def __init__(self, name, dbase):
MenuReportOptions.__init__(self, name, dbase)
def add_menu_options(self, menu):
"""
Add options to the menu for the place report.
"""
category_name = _("Report Options")
# Reload filters to pick any new ones
CustomFilters = None
from gramps.gen.filters import CustomFilters, GenericFilter
opt = FilterOption(_("Select using filter"), 0)
opt.set_help(_("Select places using a filter"))
filter_list = []
filter_list.append(GenericFilter())
filter_list.extend(CustomFilters.get_filters('Place'))
opt.set_filters(filter_list)
menu.add_option(category_name, "filter", opt)
stdoptions.add_name_format_option(menu, category_name)
places = PlaceListOption(_("Select places individually"))
places.set_help(_("List of places to report on"))
menu.add_option(category_name, "places", places)
center = EnumeratedListOption(_("Center on"), "Event")
center.set_items([
("Event", _("Event")),
("Person", _("Person"))])
center.set_help(_("If report is event or person centered"))
menu.add_option(category_name, "center", center)
stdoptions.add_private_data_option(menu, category_name)
stdoptions.add_localization_option(menu, category_name)
def make_default_style(self, default_style):
"""
Make the default output style for the Place report.
"""
self.default_style = default_style
self.__report_title_style()
self.__place_title_style()
self.__place_details_style()
self.__column_title_style()
self.__section_style()
self.__event_table_style()
self.__details_style()
self.__cell_style()
self.__table_column_style()
def __report_title_style(self):
"""
Define the style used for the report title
"""
font = FontStyle()
font.set(face=FONT_SANS_SERIF, size=16, bold=1)
para = ParagraphStyle()
para.set_font(font)
para.set_header_level(1)
para.set_top_margin(0.25)
para.set_bottom_margin(0.25)
para.set_alignment(PARA_ALIGN_CENTER)
para.set_description(_('The style used for the title of the report.'))
self.default_style.add_paragraph_style("PLC-ReportTitle", para)
def __place_title_style(self):
"""
Define the style used for the place title
"""
font = FontStyle()
font.set(face=FONT_SERIF, size=12, italic=0, bold=1)
para = ParagraphStyle()
para.set_font(font)
para.set(first_indent=-1.5, lmargin=1.5)
para.set_top_margin(0.75)
para.set_bottom_margin(0.25)
para.set_description(_('The style used for place title.'))
self.default_style.add_paragraph_style("PLC-PlaceTitle", para)
def __place_details_style(self):
"""
Define the style used for the place details
"""
font = FontStyle()
font.set(face=FONT_SERIF, size=10)
para = ParagraphStyle()
para.set_font(font)
para.set(first_indent=0.0, lmargin=1.5)
para.set_description(_('The style used for place details.'))
self.default_style.add_paragraph_style("PLC-PlaceDetails", para)
def __column_title_style(self):
"""
Define the style used for the event table column title
"""
font = FontStyle()
font.set(face=FONT_SERIF, size=10, bold=1)
para = ParagraphStyle()
para.set_font(font)
para.set(first_indent=0.0, lmargin=0.0)
para.set_description(_('The style used for a column title.'))
self.default_style.add_paragraph_style("PLC-ColumnTitle", para)
def __section_style(self):
"""
Define the style used for each section
"""
font = FontStyle()
font.set(face=FONT_SERIF, size=10, italic=0, bold=0)
para = ParagraphStyle()
para.set_font(font)
para.set(first_indent=-1.5, lmargin=1.5)
para.set_top_margin(0.5)
para.set_bottom_margin(0.25)
para.set_description(_('The style used for each section.'))
self.default_style.add_paragraph_style("PLC-Section", para)
def __event_table_style(self):
"""
Define the style used for event table
"""
table = TableStyle()
table.set_width(100)
table.set_columns(4)
table.set_column_width(0, 25)
table.set_column_width(1, 15)
table.set_column_width(2, 35)
table.set_column_width(3, 25)
self.default_style.add_table_style("PLC-EventTable", table)
table.set_width(100)
table.set_columns(4)
table.set_column_width(0, 35)
table.set_column_width(1, 15)
table.set_column_width(2, 25)
table.set_column_width(3, 25)
self.default_style.add_table_style("PLC-PersonTable", table)
def __details_style(self):
"""
Define the style used for person and event details
"""
font = FontStyle()
font.set(face=FONT_SERIF, size=10)
para = ParagraphStyle()
para.set_font(font)
para.set_description(_('The style used for event and person details.'))
self.default_style.add_paragraph_style("PLC-Details", para)
def __cell_style(self):
"""
Define the style used for cells in the event table
"""
cell = TableCellStyle()
self.default_style.add_cell_style("PLC-Cell", cell)
def __table_column_style(self):
"""
Define the style used for event table columns
"""
cell = TableCellStyle()
cell.set_bottom_border(1)
self.default_style.add_cell_style('PLC-TableColumn', cell)