2007-11-16 13:44:57 +05:30
# -*- coding: utf-8 -*-
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Martin Hawlisch, Donald N. Allingham
2008-05-19 00:54:28 +05:30
# Copyright (C) 2008 Brian G. Matherly
2010-05-01 09:42:42 +05:30
# Copyright (C) 2010 Jakim Friant
2007-11-16 13:44:57 +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
#
2008-01-22 14:47:46 +05:30
# $Id$
2007-11-16 13:44:57 +05:30
2008-02-25 13:16:32 +05:30
"""
Validate localized date parser and displayer .
2008-01-29 13:10:35 +05:30
2008-03-02 04:17:48 +05:30
Tools / Debug / Check Localized Date Parser and Displayer
2008-01-29 13:10:35 +05:30
"""
2007-11-16 13:44:57 +05:30
#-------------------------------------------------------------------------
#
# standard python modules
#
#-------------------------------------------------------------------------
import traceback
import sys
2010-01-18 10:12:17 +05:30
from gen . ggettext import gettext as _
2007-11-16 13:44:57 +05:30
#-------------------------------------------------------------------------
#
# GRAMPS modules
#
#-------------------------------------------------------------------------
import gen . lib
2011-02-20 16:22:06 +05:30
from gen . db import DbTxn
2010-05-01 09:42:42 +05:30
from gui . plug import tool
2009-06-19 20:53:58 +05:30
from gui . utils import ProgressMeter
2008-02-19 01:37:09 +05:30
from QuestionDialog import QuestionDialog
2007-11-16 13:44:57 +05:30
from DateHandler import parser as _dp
from DateHandler import displayer as _dd
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
2010-05-01 09:42:42 +05:30
class DateParserDisplayTest ( tool . Tool ) :
2007-11-16 13:44:57 +05:30
def __init__ ( self , dbstate , uistate , options_class , name , callback = None ) :
2010-05-01 09:42:42 +05:30
tool . Tool . __init__ ( self , dbstate , options_class , name )
2007-11-16 13:44:57 +05:30
if uistate :
# Running with gui -> Show message
2008-03-05 19:30:38 +05:30
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 )
2007-11-16 13:44:57 +05:30
else :
self . run_tool ( )
def run_tool ( self ) :
2009-06-19 20:53:58 +05:30
self . progress = ProgressMeter ( _ ( ' Running Date Test ' ) , ' ' )
2007-11-16 13:44:57 +05:30
self . progress . set_pass ( _ ( ' Generating dates ' ) ,
4 )
dates = [ ]
# first some valid dates
calendar = gen . lib . Date . CAL_GREGORIAN
for quality in ( gen . lib . Date . QUAL_NONE , gen . lib . Date . QUAL_ESTIMATED ,
gen . lib . Date . QUAL_CALCULATED ) :
for modifier in ( gen . lib . Date . MOD_NONE , gen . lib . Date . MOD_BEFORE ,
gen . lib . Date . MOD_AFTER , gen . lib . Date . MOD_ABOUT ) :
for slash1 in ( False , True ) :
for month in range ( 1 , 13 ) :
for day in ( 5 , 27 ) :
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , ( day , month , 1789 , slash1 ) , " Text comment " )
dates . append ( d )
for modifier in ( gen . lib . Date . MOD_RANGE , gen . lib . Date . MOD_SPAN ) :
for slash1 in ( False , True ) :
for slash2 in ( False , True ) :
for month in range ( 1 , 13 ) :
for day in ( 5 , 27 ) :
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , ( day , month , 1789 , slash1 , day , month , 1876 , slash2 ) , " Text comment " )
dates . append ( d )
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , ( day , month , 1789 , slash1 , day , 13 - month , 1876 , slash2 ) , " Text comment " )
dates . append ( d )
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , ( day , month , 1789 , slash1 , 32 - day , month , 1876 , slash2 ) , " Text comment " )
dates . append ( d )
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , ( day , month , 1789 , slash1 , 32 - day , 13 - month , 1876 , slash2 ) , " Text comment " )
dates . append ( d )
modifier = gen . lib . Date . MOD_TEXTONLY
d = gen . lib . Date ( )
d . set ( quality , modifier , calendar , gen . lib . Date . EMPTY ,
" This is a textual date " )
dates . append ( d )
self . progress . step ( )
# test invalid dates
#dateval = (4,7,1789,False,5,8,1876,False)
#for l in range(1,len(dateval)):
# d = gen.lib.Date()
# try:
# d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_NONE,
# gen.lib.Date.CAL_GREGORIAN,dateval[:l],"Text comment")
# dates.append( d)
# except Errors.DateError, e:
# d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
# dates.append( d)
# except:
# d = gen.lib.Date()
# d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
# dates.append( d)
#for l in range(1,len(dateval)):
# d = gen.lib.Date()
# try:
# d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_SPAN,gen.lib.Date.CAL_GREGORIAN,dateval[:l],"Text comment")
# dates.append( d)
# except Errors.DateError, e:
# d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
# dates.append( d)
# except:
# d = gen.lib.Date()
# d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
# dates.append( d)
#self.progress.step()
#d = gen.lib.Date()
#d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_NONE,
# gen.lib.Date.CAL_GREGORIAN,(44,7,1789,False),"Text comment")
#dates.append( d)
#d = gen.lib.Date()
#d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_NONE,
# gen.lib.Date.CAL_GREGORIAN,(4,77,1789,False),"Text comment")
#dates.append( d)
#d = gen.lib.Date()
#d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_SPAN,
# gen.lib.Date.CAL_GREGORIAN,
# (4,7,1789,False,55,8,1876,False),"Text comment")
#dates.append( d)
#d = gen.lib.Date()
#d.set(gen.lib.Date.QUAL_NONE,gen.lib.Date.MOD_SPAN,
# gen.lib.Date.CAL_GREGORIAN,
# (4,7,1789,False,5,88,1876,False),"Text comment")
#dates.append( d)
2011-04-17 00:33:41 +05:30
with DbTxn ( _ ( " Date Test Plugin " ) , self . db , batch = True ) as self . trans :
2011-02-01 03:24:58 +05:30
self . db . disable_signals ( )
self . progress . set_pass ( _ ( ' Generating dates ' ) ,
len ( dates ) )
2011-04-17 00:33:41 +05:30
# create pass and fail tags
pass_handle = self . create_tag ( _ ( ' Pass ' ) , ' #0000FFFF0000 ' )
fail_handle = self . create_tag ( _ ( ' Fail ' ) , ' #FFFF00000000 ' )
2011-02-01 03:24:58 +05:30
# now add them as birth to new persons
i = 1
for dateval in dates :
person = gen . lib . Person ( )
2011-04-17 00:33:41 +05:30
surname = gen . lib . Surname ( )
surname . set_surname ( " DateTest " )
2011-02-01 03:24:58 +05:30
name = gen . lib . Name ( )
2011-04-17 00:33:41 +05:30
name . add_surname ( surname )
2011-02-01 03:24:58 +05:30
name . set_first_name ( " Test %d " % i )
2011-04-17 00:33:41 +05:30
person . set_primary_name ( name )
self . db . add_person ( person , self . trans )
2011-02-01 03:24:58 +05:30
bevent = gen . lib . Event ( )
bevent . set_type ( gen . lib . EventType . BIRTH )
bevent . set_date_object ( dateval )
bevent . set_description ( " Date Test %d (source) " % i )
2011-04-17 00:33:41 +05:30
bevent_h = self . db . add_event ( bevent , self . trans )
2011-02-01 03:24:58 +05:30
bevent_ref = gen . lib . EventRef ( )
bevent_ref . set_reference_handle ( bevent_h )
# for the death event display the date as text and parse it back to a new date
ndate = None
2007-11-16 13:44:57 +05:30
try :
2011-02-01 03:24:58 +05:30
datestr = _dd . display ( dateval )
try :
ndate = _dp . parse ( datestr )
if not ndate :
ndate = gen . lib . Date ( )
ndate . set_as_text ( " DateParser None " )
2011-04-17 00:33:41 +05:30
person . add_tag ( fail_handle )
2011-02-01 03:24:58 +05:30
else :
2011-04-17 00:33:41 +05:30
person . add_tag ( pass_handle )
2011-02-01 03:24:58 +05:30
except :
2007-11-16 13:44:57 +05:30
ndate = gen . lib . Date ( )
2011-02-01 03:24:58 +05:30
ndate . set_as_text ( " DateParser Exception %s " % ( " " . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) , ) )
2011-04-17 00:33:41 +05:30
person . add_tag ( fail_handle )
else :
person . add_tag ( pass_handle )
2007-11-16 13:44:57 +05:30
except :
ndate = gen . lib . Date ( )
2011-02-01 03:24:58 +05:30
ndate . set_as_text ( " DateDisplay Exception: %s " % ( " " . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) , ) )
2011-04-17 00:33:41 +05:30
person . add_tag ( fail_handle )
2011-02-01 03:24:58 +05:30
if dateval . get_modifier ( ) != gen . lib . Date . MOD_TEXTONLY \
and ndate . get_modifier ( ) == gen . lib . Date . MOD_TEXTONLY :
# parser was unable to correctly parse the string
ndate . set_as_text ( " TEXTONLY: " + ndate . get_text ( ) )
2011-04-17 00:33:41 +05:30
person . add_tag ( fail_handle )
2011-02-01 03:24:58 +05:30
if dateval . get_modifier ( ) == gen . lib . Date . MOD_TEXTONLY \
and dateval . get_text ( ) . count ( " Traceback " ) \
2011-04-17 00:33:41 +05:30
and pass_handle in person . get_tag_list ( ) :
person . add_tag ( fail_handle )
2011-02-01 03:24:58 +05:30
devent = gen . lib . Event ( )
devent . set_type ( gen . lib . EventType . DEATH )
devent . set_date_object ( ndate )
devent . set_description ( " Date Test %d (result) " % i )
2011-04-17 00:33:41 +05:30
devent_h = self . db . add_event ( devent , self . trans )
2011-02-01 03:24:58 +05:30
devent_ref = gen . lib . EventRef ( )
devent_ref . set_reference_handle ( devent_h )
person . set_birth_ref ( bevent_ref )
person . set_death_ref ( devent_ref )
2011-04-17 00:33:41 +05:30
self . db . commit_person ( person , self . trans )
2011-02-01 03:24:58 +05:30
i = i + 1
self . progress . step ( )
2007-11-16 13:44:57 +05:30
self . db . enable_signals ( )
self . db . request_rebuild ( )
self . progress . close ( )
2011-04-17 00:33:41 +05:30
def create_tag ( self , tag_name , tag_color ) :
"""
Create a tag if it doesn ' t already exist.
"""
tag = self . db . get_tag_from_name ( tag_name )
if tag is None :
tag = gen . lib . Tag ( )
tag . set_name ( tag_name )
if tag_color is not None :
tag . set_color ( tag_color )
tag . set_priority ( self . db . get_number_of_tags ( ) )
tag_handle = self . db . add_tag ( tag , self . trans )
else :
tag_handle = tag . get_handle ( )
return tag_handle
2009-10-24 19:23:20 +05:30
#------------------------------------------------------------------------
#
# DateParserDisplayTestOptions
#
#------------------------------------------------------------------------
2010-05-01 09:42:42 +05:30
class DateParserDisplayTestOptions ( tool . ToolOptions ) :
2009-10-24 19:23:20 +05:30
"""
Defines options and provides handling interface .
"""
def __init__ ( self , name , person_id = None ) :
""" Initialize the options class """
2010-05-01 09:42:42 +05:30
tool . ToolOptions . __init__ ( self , name , person_id )