~ubuntu-branches/ubuntu/trusty/gramps/trusty-proposed

« back to all changes in this revision

Viewing changes to gramps/plugins/tool/dateparserdisplaytest.py

  • Committer: Package Import Robot
  • Author(s): Ross Gammon
  • Date: 2014-02-03 17:28:04 UTC
  • mfrom: (39.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140203172804-76y7nwxiw92zhlnj
Tags: 4.0.3+dfsg-1
* New upstream release (Closes: #720858)
* To-do notes improved and made persistent (Closes: #680692)
* Applied patch to setup.py to fix resource path problem
* Applied patch to disable the optional HTML View & prevent a crash
* Remove sourceless javascript files (Closes: #736436)
* Gramps uses Bat Mitzva internally (Closes: #502532)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Gramps - a GTK+/GNOME based genealogy program
 
4
#
 
5
# Copyright (C) 2000-2006  Martin Hawlisch, Donald N. Allingham
 
6
# Copyright (C) 2008       Brian G. Matherly
 
7
# Copyright (C) 2010       Jakim Friant
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
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
 
22
#
 
23
 
 
24
# $Id$
 
25
 
 
26
"""
 
27
Validate localized date parser and displayer.
 
28
 
 
29
Tools/Debug/Check Localized Date Parser and Displayer
 
30
"""
 
31
 
 
32
#-------------------------------------------------------------------------
 
33
#
 
34
# standard python modules
 
35
#
 
36
#-------------------------------------------------------------------------
 
37
import traceback
 
38
import sys
 
39
from gramps.gen.const import GRAMPS_LOCALE as glocale
 
40
_ = glocale.translation.gettext
 
41
 
 
42
#-------------------------------------------------------------------------
 
43
#
 
44
# GRAMPS modules
 
45
#
 
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
 
54
 
 
55
#-------------------------------------------------------------------------
 
56
#
 
57
#
 
58
#
 
59
#-------------------------------------------------------------------------
 
60
class DateParserDisplayTest(tool.Tool):
 
61
 
 
62
    def __init__(self, dbstate, uistate, options_class, name, callback=None):
 
63
 
 
64
        tool.Tool.__init__(self, dbstate, options_class, name)
 
65
        if uistate:
 
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)
 
68
        else:
 
69
            self.run_tool()
 
70
 
 
71
 
 
72
    def run_tool(self):
 
73
        self.progress = ProgressMeter(_('Running Date Test'),'')
 
74
        self.progress.set_pass(_('Generating dates'),
 
75
                               4)
 
76
        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):
 
85
                        for day in (5,27):
 
86
                            d = Date()
 
87
                            d.set(quality,modifier,calendar,(day,month,1789,slash1),"Text comment")
 
88
                            dates.append( d)
 
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):
 
93
                            for day in (5,27):
 
94
                                d = Date()
 
95
                                d.set(quality,modifier,calendar,(day,month,1789,slash1,day,month,1876,slash2),"Text comment")
 
96
                                dates.append( d)
 
97
                                d = Date()
 
98
                                d.set(quality,modifier,calendar,(day,month,1789,slash1,day,13-month,1876,slash2),"Text comment")
 
99
                                dates.append( d)
 
100
                                d = Date()
 
101
                                d.set(quality,modifier,calendar,(day,month,1789,slash1,32-day,month,1876,slash2),"Text comment")
 
102
                                dates.append( d)
 
103
                                d = Date()
 
104
                                d.set(quality,modifier,calendar,(day,month,1789,slash1,32-day,13-month,1876,slash2),"Text comment")
 
105
                                dates.append( d)
 
106
            modifier = Date.MOD_TEXTONLY
 
107
            d = Date()
 
108
            d.set(quality,modifier,calendar,Date.EMPTY,
 
109
                  "This is a textual date")
 
110
            dates.append( d)
 
111
            self.progress.step()
 
112
        
 
113
        # test invalid dates
 
114
        #dateval = (4,7,1789,False,5,8,1876,False)
 
115
        #for l in range(1,len(dateval)):
 
116
        #    d = Date()
 
117
        #    try:
 
118
        #        d.set(Date.QUAL_NONE,Date.MOD_NONE,
 
119
        #              Date.CAL_GREGORIAN,dateval[:l],"Text comment")
 
120
        #        dates.append( d)
 
121
        #    except DateError, e:
 
122
        #        d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
 
123
        #        dates.append( d)
 
124
        #    except:
 
125
        #        d = Date()
 
126
        #        d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
 
127
        #        dates.append( d)
 
128
        #for l in range(1,len(dateval)):
 
129
        #    d = Date()
 
130
        #    try:
 
131
        #        d.set(Date.QUAL_NONE,Date.MOD_SPAN,Date.CAL_GREGORIAN,dateval[:l],"Text comment")
 
132
        #        dates.append( d)
 
133
        #    except DateError, e:
 
134
        #        d.set_as_text("Date identified value correctly as invalid.\n%s" % e)
 
135
        #        dates.append( d)
 
136
        #    except:
 
137
        #        d = Date()
 
138
        #        d.set_as_text("Date.set Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
 
139
        #        dates.append( d)
 
140
        #self.progress.step()
 
141
        #d = Date()
 
142
        #d.set(Date.QUAL_NONE,Date.MOD_NONE,
 
143
        #      Date.CAL_GREGORIAN,(44,7,1789,False),"Text comment")
 
144
        #dates.append( d)
 
145
        #d = Date()
 
146
        #d.set(Date.QUAL_NONE,Date.MOD_NONE,
 
147
        #      Date.CAL_GREGORIAN,(4,77,1789,False),"Text comment")
 
148
        #dates.append( d)
 
149
        #d = Date()
 
150
        #d.set(Date.QUAL_NONE,Date.MOD_SPAN,
 
151
        #      Date.CAL_GREGORIAN,
 
152
        #      (4,7,1789,False,55,8,1876,False),"Text comment")
 
153
        #dates.append( d)
 
154
        #d = Date()
 
155
        #d.set(Date.QUAL_NONE,Date.MOD_SPAN,
 
156
        #      Date.CAL_GREGORIAN,
 
157
        #      (4,7,1789,False,5,88,1876,False),"Text comment")
 
158
        #dates.append( d)
 
159
        
 
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'),
 
163
                                   len(dates))
 
164
 
 
165
            # create pass and fail tags
 
166
            pass_handle = self.create_tag(_('Pass'), '#0000FFFF0000')
 
167
            fail_handle = self.create_tag(_('Fail'), '#FFFF00000000')
 
168
 
 
169
            # now add them as birth to new persons
 
170
            i = 1
 
171
            for dateval in dates:
 
172
                person = Person()
 
173
                surname = Surname()
 
174
                surname.set_surname("DateTest")
 
175
                name = Name()
 
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)
 
180
                bevent = Event()
 
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
 
188
                ndate = None
 
189
                try:
 
190
                    datestr = _dd.display( dateval)
 
191
                    try:
 
192
                        ndate = _dp.parse( datestr)
 
193
                        if not ndate:
 
194
                            ndate = Date()
 
195
                            ndate.set_as_text("DateParser None")
 
196
                            person.add_tag(fail_handle)
 
197
                        else:
 
198
                            person.add_tag(pass_handle)
 
199
                    except:
 
200
                        ndate = Date()
 
201
                        ndate.set_as_text("DateParser Exception %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
 
202
                        person.add_tag(fail_handle)
 
203
                    else:
 
204
                        person.add_tag(pass_handle)
 
205
                except:
 
206
                    ndate = Date()
 
207
                    ndate.set_as_text("DateDisplay Exception: %s" % ("".join(traceback.format_exception(*sys.exc_info())),))
 
208
                    person.add_tag(fail_handle)
 
209
                
 
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)
 
219
                
 
220
                devent = Event()
 
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)
 
230
                i = i + 1
 
231
                self.progress.step()
 
232
        self.db.enable_signals()
 
233
        self.db.request_rebuild()
 
234
        self.progress.close()
 
235
 
 
236
    def create_tag(self, tag_name, tag_color):
 
237
        """
 
238
        Create a tag if it doesn't already exist.
 
239
        """
 
240
        tag = self.db.get_tag_from_name(tag_name)
 
241
        if tag is None:
 
242
            tag = Tag()
 
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)
 
248
        else:
 
249
            tag_handle = tag.get_handle()
 
250
        return tag_handle
 
251
 
 
252
#------------------------------------------------------------------------
 
253
#
 
254
# DateParserDisplayTestOptions
 
255
#
 
256
#------------------------------------------------------------------------
 
257
class DateParserDisplayTestOptions(tool.ToolOptions):
 
258
    """
 
259
    Defines options and provides handling interface.
 
260
    """
 
261
    def __init__(self, name, person_id=None):
 
262
        """ Initialize the options class """
 
263
        tool.ToolOptions.__init__(self, name, person_id)