2003-07-31 17:28:08 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2008-01-15 14:46:45 +05:30
|
|
|
# Copyright (C) 2003-2006, 2008 Donald N. Allingham
|
2003-07-31 17:28:08 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2003-12-07 04:35:52 +05:30
|
|
|
# $Id$
|
|
|
|
|
2003-07-31 17:28:08 +05:30
|
|
|
"Export to Web Family Tree"
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
from cStringIO import StringIO
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2006-03-05 10:01:24 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".WriteFtree")
|
|
|
|
|
2003-07-31 17:28:08 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME/GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
import gtk.glade
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import Utils
|
2006-05-03 06:32:54 +05:30
|
|
|
from Filters import GenericFilter, Rules, build_filter_menu
|
2003-08-02 07:52:34 +05:30
|
|
|
import Errors
|
2004-01-18 07:29:30 +05:30
|
|
|
from QuestionDialog import ErrorDialog
|
2006-03-11 06:42:06 +05:30
|
|
|
from PluginUtils import register_export
|
2003-07-31 17:28:08 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# writeData
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-01-15 14:46:45 +05:30
|
|
|
def writeData(database, filename, person, option_box, callback=None):
|
|
|
|
writer = FtreeWriter(database, person, 0, filename, option_box, callback)
|
|
|
|
return writer.export_data()
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
class FtreeWriterOptionBox:
|
|
|
|
"""
|
|
|
|
Create a VBox with the option widgets and define methods to retrieve
|
|
|
|
the options.
|
|
|
|
"""
|
2008-01-15 14:46:45 +05:30
|
|
|
def __init__(self, person):
|
2003-09-16 07:05:09 +05:30
|
|
|
self.person = person
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
def get_option_box(self):
|
|
|
|
self.restrict = True
|
|
|
|
base = os.path.dirname(__file__)
|
|
|
|
glade_file = "%s/%s" % (base,"writeftree.glade")
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
self.top = gtk.glade.XML(glade_file, "top", "gramps")
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
filter_obj = self.top.get_widget("filter")
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
all = GenericFilter()
|
2005-02-20 00:35:48 +05:30
|
|
|
all.set_name(_("Entire Database"))
|
2006-05-03 11:59:07 +05:30
|
|
|
all.add_rule(Rules.Person.Everyone([]))
|
2006-10-11 10:29:26 +05:30
|
|
|
|
|
|
|
the_filters = [all]
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2005-07-09 01:54:54 +05:30
|
|
|
if self.person:
|
2006-05-03 06:32:54 +05:30
|
|
|
des = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
des.set_name(_("Descendants of %s") %
|
|
|
|
self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
des.add_rule(Rules.Person.IsDescendantOf(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id(),1]))
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
ans = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
ans.set_name(_("Ancestors of %s")
|
|
|
|
% self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
ans.add_rule(Rules.Person.IsAncestorOf(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id(),1]))
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
com = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
com.set_name(_("People with common ancestor with %s") %
|
|
|
|
self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
com.add_rule(Rules.Person.HasCommonAncestorWith(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id()]))
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
the_filters += [all, des, ans, com]
|
2006-10-11 10:29:26 +05:30
|
|
|
|
|
|
|
from Filters import CustomFilters
|
|
|
|
the_filters.extend(CustomFilters.get_filters('Person'))
|
|
|
|
self.filter_menu = build_filter_menu(the_filters)
|
2005-02-20 00:35:48 +05:30
|
|
|
filter_obj.set_menu(self.filter_menu)
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
the_box = self.top.get_widget("vbox1")
|
|
|
|
the_parent = self.top.get_widget('dialog-vbox1')
|
|
|
|
the_parent.remove(the_box)
|
|
|
|
self.top.get_widget("top").destroy()
|
|
|
|
return the_box
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
def parse_options(self):
|
|
|
|
self.restrict = self.top.get_widget("restrict").get_active()
|
|
|
|
self.cfilter = self.filter_menu.get_active().get_data("filter")
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# FtreeWriter
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class FtreeWriter:
|
2003-12-07 04:35:52 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def __init__(self, database, person, cl=0, filename="", option_box=None,
|
|
|
|
callback = None):
|
2005-02-20 00:35:48 +05:30
|
|
|
self.db = database
|
|
|
|
self.person = person
|
|
|
|
self.option_box = option_box
|
|
|
|
self.cl = cl
|
|
|
|
self.filename = filename
|
2006-05-10 01:15:18 +05:30
|
|
|
self.callback = callback
|
|
|
|
if '__call__' in dir(self.callback): # callback is really callable
|
|
|
|
self.update = self.update_real
|
|
|
|
else:
|
|
|
|
self.update = self.update_empty
|
2005-02-20 00:35:48 +05:30
|
|
|
|
|
|
|
self.plist = {}
|
2003-08-02 07:52:34 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
if not option_box:
|
|
|
|
self.cl_setup()
|
2003-08-02 07:52:34 +05:30
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
self.option_box.parse_options()
|
|
|
|
|
|
|
|
self.restrict = self.option_box.restrict
|
|
|
|
if self.option_box.cfilter == None:
|
|
|
|
for p in self.db.get_person_handles(sort_handles=False):
|
|
|
|
self.plist[p] = 1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for p in self.option_box.cfilter.apply(self.db, self.db.get_person_handles(sort_handles=False)):
|
|
|
|
self.plist[p] = 1
|
|
|
|
except Errors.FilterError, msg:
|
2008-01-15 14:46:45 +05:30
|
|
|
(m1, m2) = msg.messages()
|
|
|
|
ErrorDialog(m1, m2)
|
2005-02-20 00:35:48 +05:30
|
|
|
return
|
|
|
|
|
2006-05-10 01:15:18 +05:30
|
|
|
def update_empty(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update_real(self):
|
|
|
|
self.count += 1
|
|
|
|
newval = int(100*self.count/self.total)
|
|
|
|
if newval != self.oldval:
|
|
|
|
self.callback(newval)
|
|
|
|
self.oldval = newval
|
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
def cl_setup(self):
|
|
|
|
self.restrict = True
|
|
|
|
for p in self.db.get_person_handles(sort_handles=False):
|
|
|
|
self.plist[p] = 1
|
|
|
|
|
|
|
|
def export_data(self):
|
2003-07-31 17:28:08 +05:30
|
|
|
name_map = {}
|
|
|
|
id_map = {}
|
|
|
|
id_name = {}
|
2006-05-10 01:15:18 +05:30
|
|
|
self.count = 0
|
|
|
|
self.oldval = 0
|
|
|
|
self.total = 2*len(self.plist)
|
|
|
|
|
2003-08-02 07:52:34 +05:30
|
|
|
for key in self.plist:
|
2006-05-10 01:15:18 +05:30
|
|
|
self.update()
|
2004-08-07 10:46:57 +05:30
|
|
|
pn = self.db.get_person_from_handle(key).get_primary_name()
|
2004-02-14 11:10:30 +05:30
|
|
|
sn = pn.get_surname()
|
|
|
|
items = pn.get_first_name().split()
|
2003-07-31 17:28:08 +05:30
|
|
|
if len(items) > 0:
|
2008-01-15 14:46:45 +05:30
|
|
|
n = "%s %s" % (items[0], sn)
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
|
|
|
n = sn
|
|
|
|
|
|
|
|
count = -1
|
|
|
|
if name_map.has_key(n):
|
|
|
|
count = 0
|
|
|
|
while 1:
|
2008-01-15 14:46:45 +05:30
|
|
|
nn = "%s%d" % (n, count)
|
2003-07-31 17:28:08 +05:30
|
|
|
if not name_map.has_key(nn):
|
|
|
|
break;
|
2003-09-25 04:19:51 +05:30
|
|
|
count += 1
|
2003-07-31 17:28:08 +05:30
|
|
|
name_map[nn] = key
|
|
|
|
id_map[key] = nn
|
|
|
|
else:
|
|
|
|
name_map[n] = key
|
|
|
|
id_map[key] = n
|
2008-01-15 14:46:45 +05:30
|
|
|
id_name[key] = get_name(pn, count)
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
f = open(self.filename,"w")
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2003-08-02 07:52:34 +05:30
|
|
|
for key in self.plist:
|
2006-05-10 01:15:18 +05:30
|
|
|
self.update()
|
2004-08-07 10:46:57 +05:30
|
|
|
p = self.db.get_person_from_handle(key)
|
2003-07-31 17:28:08 +05:30
|
|
|
name = id_name[key]
|
|
|
|
father = ""
|
|
|
|
mother = ""
|
|
|
|
email = ""
|
|
|
|
web = ""
|
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
family_handle = p.get_main_parents_family_handle()
|
|
|
|
if family_handle:
|
|
|
|
family = self.db.get_family_from_handle(family_handle)
|
|
|
|
if family.get_father_handle() and id_map.has_key(family.get_father_handle()):
|
|
|
|
father = id_map[family.get_father_handle()]
|
|
|
|
if family.get_mother_handle() and id_map.has_key(family.get_mother_handle()):
|
|
|
|
mother = id_map[family.get_mother_handle()]
|
2003-07-31 17:28:08 +05:30
|
|
|
|
|
|
|
#
|
|
|
|
# Calculate Date
|
|
|
|
#
|
2006-05-10 01:15:18 +05:30
|
|
|
birth_ref = p.get_birth_ref()
|
|
|
|
death_ref = p.get_death_ref()
|
|
|
|
if birth_ref:
|
|
|
|
birth_event = self.db.get_event_from_handle(birth_ref.ref)
|
2005-02-20 00:35:48 +05:30
|
|
|
birth = birth_event.get_date_object()
|
|
|
|
else:
|
|
|
|
birth = None
|
2006-05-10 01:15:18 +05:30
|
|
|
if death_ref:
|
|
|
|
death_event = self.db.get_event_from_handle(death_ref.ref)
|
2005-02-20 00:35:48 +05:30
|
|
|
death = death_event.get_date_object()
|
|
|
|
else:
|
|
|
|
death = None
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
if self.restrict:
|
2008-01-15 14:46:45 +05:30
|
|
|
alive = Utils.probably_alive(p, self.db)
|
2003-08-02 07:52:34 +05:30
|
|
|
else:
|
|
|
|
alive = 0
|
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
if birth and not alive:
|
|
|
|
if death and not alive :
|
2008-01-15 14:46:45 +05:30
|
|
|
dates = "%s-%s" % (fdate(birth), fdate(death))
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
|
|
|
dates = fdate(birth)
|
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
if death and not alive:
|
2003-07-31 17:28:08 +05:30
|
|
|
dates = fdate(death)
|
|
|
|
else:
|
|
|
|
dates = ""
|
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
f.write('%s;%s;%s;%s;%s;%s\n' % (name, father, mother, email, web,
|
|
|
|
dates))
|
2003-07-31 17:28:08 +05:30
|
|
|
|
|
|
|
f.close()
|
2008-01-15 14:46:45 +05:30
|
|
|
return True
|
2003-07-31 17:28:08 +05:30
|
|
|
|
|
|
|
def fdate(val):
|
2005-02-20 00:35:48 +05:30
|
|
|
if val.get_year_valid():
|
|
|
|
if val.get_month_valid():
|
|
|
|
if val.get_day_valid():
|
2008-01-15 14:46:45 +05:30
|
|
|
return "%d/%d/%d" % (val.get_day(), val.get_month(),
|
|
|
|
val.get_year())
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
2008-01-15 14:46:45 +05:30
|
|
|
return "%d/%d" % (val.get_month(), val.get_year())
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
return "%d" % val.get_year()
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def get_name(name, count):
|
2003-07-31 17:28:08 +05:30
|
|
|
"""returns a name string built from the components of the Name
|
|
|
|
instance, in the form of Firstname Surname"""
|
|
|
|
if count == -1:
|
|
|
|
val = ""
|
|
|
|
else:
|
|
|
|
val = str(count)
|
|
|
|
|
2005-02-20 00:35:48 +05:30
|
|
|
if (name.suffix == ""):
|
|
|
|
if name.prefix:
|
|
|
|
return "%s %s %s%s" % (name.first_name, name.prefix, name.surname, val)
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
return "%s %s%s" % (name.first_name, name.surname, val)
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
if name.prefix:
|
|
|
|
return "%s %s %s%s, %s" % (name.first_name, name.prefix, name.surname, val, name.Suffix)
|
2003-07-31 17:28:08 +05:30
|
|
|
else:
|
2005-02-20 00:35:48 +05:30
|
|
|
return "%s %s%s, %s" % (name.first_name, name.surname, val, name.suffix)
|
2003-07-31 17:28:08 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Register the plugin
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2005-02-20 00:35:48 +05:30
|
|
|
_title = _('_Web Family Tree')
|
|
|
|
_description = _('Web Family Tree format.')
|
2008-01-15 14:46:45 +05:30
|
|
|
_config = (_('Web Family Tree export options'), FtreeWriterOptionBox)
|
2005-02-20 00:35:48 +05:30
|
|
|
_filename = 'wft'
|
2003-07-31 17:28:08 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
register_export(writeData, _title, _description, _config,_filename)
|