1
# -*- coding: utf-8 -*-
3
# Gramps - a GTK+/GNOME based genealogy program
5
# Copyright (C) 2000-2006 Martin Hawlisch, Donald N. Allingham
6
# Copyright (C) 2008 Brian G. Matherly
7
# Copyright (C) 2010 Jakim Friant
9
# This program is free software; you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation; either version 2 of the License, or
12
# (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program; if not, write to the Free Software
21
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
Validate localized date parser and displayer.
29
Tools/Debug/Check Localized Date Parser and Displayer
32
#-------------------------------------------------------------------------
34
# standard python modules
36
#-------------------------------------------------------------------------
39
from gramps.gen.const import GRAMPS_LOCALE as glocale
40
_ = glocale.translation.gettext
42
#-------------------------------------------------------------------------
46
#-------------------------------------------------------------------------
47
from gramps.gen.lib import Date, Event, EventRef, EventType, Name, Person, Surname, Tag
48
from gramps.gen.db import DbTxn
49
from gramps.gui.plug import tool
50
from gramps.gui.utils import ProgressMeter
51
from gramps.gui.dialog import QuestionDialog
52
from gramps.gen.datehandler import parser as _dp
53
from gramps.gen.datehandler import displayer as _dd
55
#-------------------------------------------------------------------------
59
#-------------------------------------------------------------------------
60
class DateParserDisplayTest(tool.Tool):
62
def __init__(self, dbstate, uistate, options_class, name, callback=None):
64
tool.Tool.__init__(self, dbstate, options_class, name)
66
# Running with gui -> Show message
67
QuestionDialog(_("Start date test?"),_("This test will create many persons and events in the current database. Do you really want to run this test?"),_("Run test"),self.run_tool)
73
self.progress = ProgressMeter(_('Running Date Test'),'')
74
self.progress.set_pass(_('Generating dates'),
77
# first some valid dates
78
calendar = Date.CAL_GREGORIAN
79
for quality in (Date.QUAL_NONE, Date.QUAL_ESTIMATED,
80
Date.QUAL_CALCULATED):
81
for modifier in (Date.MOD_NONE, Date.MOD_BEFORE,
82
Date.MOD_AFTER, Date.MOD_ABOUT):
83
for slash1 in (False,True):
84
for month in range(1,13):
87
d.set(quality,modifier,calendar,(day,month,1789,slash1),"Text comment")
89
for modifier in (Date.MOD_RANGE, Date.MOD_SPAN):
90
for slash1 in (False,True):
91
for slash2 in (False,True):
92
for month in range(1,13):
95
d.set(quality,modifier,calendar,(day,month,1789,slash1,day,month,1876,slash2),"Text comment")
98
d.set(quality,modifier,calendar,(day,month,1789,slash1,day,13-month,1876,slash2),"Text comment")
101
d.set(quality,modifier,calendar,(day,month,1789,slash1,32-day,month,1876,slash2),"Text comment")
104
d.set(quality,modifier,calendar,(day,month,1789,slash1,32-day,13-month,1876,slash2),"Text comment")
106
modifier = Date.MOD_TEXTONLY
108
d.set(quality,modifier,calendar,Date.EMPTY,
109
"This is a textual date")
114
#dateval = (4,7,1789,False,5,8,1876,False)
115
#for l in range(1,len(dateval)):
118
# d.set(Date.QUAL_NONE,Date.MOD_NONE,
119
# Date.CAL_GREGORIAN,dateval[:l],"Text comment")
121
# except DateError, e:
122
# d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
126
# d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
128
#for l in range(1,len(dateval)):
131
# d.set(Date.QUAL_NONE,Date.MOD_SPAN,Date.CAL_GREGORIAN,dateval[:l],"Text comment")
133
# except DateError, e:
134
# d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
138
# d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
140
#self.progress.step()
142
#d.set(Date.QUAL_NONE,Date.MOD_NONE,
143
# Date.CAL_GREGORIAN,(44,7,1789,False),"Text comment")
146
#d.set(Date.QUAL_NONE,Date.MOD_NONE,
147
# Date.CAL_GREGORIAN,(4,77,1789,False),"Text comment")
150
#d.set(Date.QUAL_NONE,Date.MOD_SPAN,
151
# Date.CAL_GREGORIAN,
152
# (4,7,1789,False,55,8,1876,False),"Text comment")
155
#d.set(Date.QUAL_NONE,Date.MOD_SPAN,
156
# Date.CAL_GREGORIAN,
157
# (4,7,1789,False,5,88,1876,False),"Text comment")
160
with DbTxn(_("Date Test Plugin"), self.db, batch=True) as self.trans:
161
self.db.disable_signals()
162
self.progress.set_pass(_('Generating dates'),
165
# create pass and fail tags
166
pass_handle = self.create_tag(_('Pass'), '#0000FFFF0000')
167
fail_handle = self.create_tag(_('Fail'), '#FFFF00000000')
169
# now add them as birth to new persons
171
for dateval in dates:
174
surname.set_surname("DateTest")
176
name.add_surname(surname)
177
name.set_first_name("Test %d" % i)
178
person.set_primary_name(name)
179
self.db.add_person(person, self.trans)
181
bevent.set_type(EventType.BIRTH)
182
bevent.set_date_object(dateval)
183
bevent.set_description("Date Test %d (source)" % i)
184
bevent_h = self.db.add_event(bevent, self.trans)
185
bevent_ref = EventRef()
186
bevent_ref.set_reference_handle(bevent_h)
187
# for the death event display the date as text and parse it back to a new date
190
datestr = _dd.display( dateval)
192
ndate = _dp.parse( datestr)
195
ndate.set_as_text("DateParser None")
196
person.add_tag(fail_handle)
198
person.add_tag(pass_handle)
201
ndate.set_as_text("DateParser Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
202
person.add_tag(fail_handle)
204
person.add_tag(pass_handle)
207
ndate.set_as_text("DateDisplay Exception: %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
208
person.add_tag(fail_handle)
210
if dateval.get_modifier() != Date.MOD_TEXTONLY \
211
and ndate.get_modifier() == Date.MOD_TEXTONLY:
212
# parser was unable to correctly parse the string
213
ndate.set_as_text( "TEXTONLY: "+ndate.get_text())
214
person.add_tag(fail_handle)
215
if dateval.get_modifier() == Date.MOD_TEXTONLY \
216
and dateval.get_text().count("Traceback") \
217
and pass_handle in person.get_tag_list():
218
person.add_tag(fail_handle)
221
devent.set_type(EventType.DEATH)
222
devent.set_date_object(ndate)
223
devent.set_description("Date Test %d (result)" % i)
224
devent_h = self.db.add_event(devent, self.trans)
225
devent_ref = EventRef()
226
devent_ref.set_reference_handle(devent_h)
227
person.set_birth_ref(bevent_ref)
228
person.set_death_ref(devent_ref)
229
self.db.commit_person(person, self.trans)
232
self.db.enable_signals()
233
self.db.request_rebuild()
234
self.progress.close()
236
def create_tag(self, tag_name, tag_color):
238
Create a tag if it doesn't already exist.
240
tag = self.db.get_tag_from_name(tag_name)
243
tag.set_name(tag_name)
244
if tag_color is not None:
245
tag.set_color(tag_color)
246
tag.set_priority(self.db.get_number_of_tags())
247
tag_handle = self.db.add_tag(tag, self.trans)
249
tag_handle = tag.get_handle()
252
#------------------------------------------------------------------------
254
# DateParserDisplayTestOptions
256
#------------------------------------------------------------------------
257
class DateParserDisplayTestOptions(tool.ToolOptions):
259
Defines options and provides handling interface.
261
def __init__(self, name, person_id=None):
262
""" Initialize the options class """
263
tool.ToolOptions.__init__(self, name, person_id)