* src/plugins/TestcaseGenerator.py: Add some date events

svn: r5301
This commit is contained in:
Martin Hawlisch 2005-10-11 11:02:15 +00:00
parent ad49a9e202
commit c6de44d0de
2 changed files with 143 additions and 10 deletions

View File

@ -1,3 +1,6 @@
2005-10-11 Martin Hawlisch <Martin.Hawlisch@gmx.de>
* src/plugins/TestcaseGenerator.py: Add some date events
2005-10-10 Don Allingham <don@gramps-project.org>
* src/GrampsDBCallback.py: doc changes
* src/NameDisplay.py: remove nickname support

View File

@ -70,6 +70,7 @@ class TestcaseGenerator(Tool.Tool):
self.person_count = 0
self.persons_todo = []
self.parents_todo = []
self.person_dates = {}
if person:
self.persons_todo.append(person.get_handle())
self.parents_todo.append(person.get_handle())
@ -693,7 +694,7 @@ class TestcaseGenerator(Tool.Tool):
self.db.commit_person(person,self.trans)
self.commit_transaction() # COMMIT TRANSACTION STEP
def generate_person(self,gender=None,lastname=None,note=None):
def generate_person(self,gender=None,lastname=None,note=None, alive_in_year=None):
if not self.cli:
self.progress.set_fraction(min(1.0,max(0.0, 1.0*self.person_count/self.options.handler.options_dict['person_count'])))
if self.person_count % 10 == 0:
@ -713,7 +714,10 @@ class TestcaseGenerator(Tool.Tool):
# Gender
if gender == None:
gender = randint(0,1)
np.set_gender(gender)
if randint(0,10) == 1: # Set some persons to unknown gender
np.set_gender(RelLib.Person.UNKNOWN)
else:
np.set_gender(gender)
# Name
syllables1 = ["sa","li","na","ma","no","re","mi","cha","ki","du","ba","ku","el"]
@ -761,19 +765,64 @@ class TestcaseGenerator(Tool.Tool):
alt_name.set_note("Generated by TestcaseGenerator")
np.add_alternate_name( alt_name)
if not alive_in_year:
alive_in_year = randint(1700,2000)
by = alive_in_year - randint(0,60)
dy = alive_in_year + randint(0,60)
# birth
if randint(0,1) == 1:
(birth_year, e) = self.rand_event( "Birth", by,by)
self.db.add_event(e, self.trans)
np.set_birth_handle(e.get_handle())
# baptism
if randint(0,1) == 1:
(bapt_year, e) = self.rand_event( choice( ("Baptism", "Christening")), by, by+2)
self.db.add_event(e, self.trans)
np.add_event_handle(e.get_handle())
# death
death_year = None
if randint(0,1) == 1:
(death_year, e) = self.rand_event( "Death", dy,dy)
self.db.add_event(e, self.trans)
np.set_death_handle(e.get_handle())
# burial
if randint(0,1) == 1:
(bur_year, e) = self.rand_event( choice( ("Burial", "Cremation")), dy, dy+2)
self.db.add_event(e, self.trans)
np.add_event_handle(e.get_handle())
person_handle = self.db.add_person(np,self.trans)
self.person_count = self.person_count+1
self.person_dates[person_handle] = (by,dy)
self.commit_transaction() # COMMIT TRANSACTION STEP
return( self.db.add_person(np,self.trans))
return( person_handle)
def generate_family(self,person1_h):
person1 = self.db.get_person_from_handle(person1_h)
alive_in_year = None
if person1_h in self.person_dates:
(born, died) = self.person_dates[person1_h]
alive_in_year = min( born+randint(10,50), died + randint(-10,10))
if person1.get_gender() == 1:
person2_h = self.generate_person(0)
if alive_in_year:
person2_h = self.generate_person(0, alive_in_year = alive_in_year)
else:
person2_h = self.generate_person(0)
else:
person2_h = person1_h
person1_h = self.generate_person(1)
if alive_in_year:
person1_h = self.generate_person(1, alive_in_year = alive_in_year)
else:
person1_h = self.generate_person(1)
if randint(0,2) > 0:
self.parents_todo.append(person1_h)
@ -797,7 +846,12 @@ class TestcaseGenerator(Tool.Tool):
lastname = person1.get_primary_name().get_surname()
for i in range(0,randint(1,10)):
child_h = self.generate_person(None, lastname)
if alive_in_year:
child_h = self.generate_person(None, lastname, alive_in_year = alive_in_year + randint( 16+2*i, 30 + 2*i))
else:
child_h = self.generate_person(None, lastname)
(born,died) = self.person_dates[child_h]
alive_in_year = born
fam = self.db.get_family_from_handle(fam_h)
fam.add_child_handle(child_h)
self.db.commit_family(fam,self.trans)
@ -813,10 +867,15 @@ class TestcaseGenerator(Tool.Tool):
if child.get_parent_family_handle_list():
return
lastname = child.get_primary_name().get_surname()
person1_h = self.generate_person(1,lastname)
person2_h = self.generate_person(0)
lastname = child.get_primary_name().get_surname()
if child_h in self.person_dates:
(born,died) = self.person_dates[child_h]
person1_h = self.generate_person(1,lastname, alive_in_year=born)
person2_h = self.generate_person(0, alive_in_year=born)
else:
person1_h = self.generate_person(1,lastname)
person2_h = self.generate_person(0)
if randint(0,2) > 1:
self.parents_todo.append(person1_h)
@ -852,6 +911,8 @@ class TestcaseGenerator(Tool.Tool):
e.add_source_reference(self.default_sourceref)
e.add_media_reference(self.default_mediaref)
e.set_place_handle(self.default_place.get_handle())
(year,d) = self.rand_date()
e.set_date_object( d)
self.db.add_event(e, self.trans)
object.add_event_handle(e.get_handle())
object.add_attribute( self.default_attribute)
@ -861,7 +922,76 @@ class TestcaseGenerator(Tool.Tool):
except AttributeError:
pass # family does not have an url
object.set_note("Generated by TestcaseGenerator")
def rand_date( self, start=None, end=None):
"""
Generates a random date object between the given years start and end
"""
if not start and not end:
start = randint(1700,2000)
if start and not end:
end = start + randint(0,100)
if end and not start:
start = end - randint(0,100)
year = randint(start,end)
ndate = Date.Date()
if randint(0,10) == 1:
# Some get a textual date
ndate.set_as_text( choice(("Unknown","??","Don't know","TODO!")))
else:
if randint(0,10) == 1:
# some get an empty date
pass
else:
# regular dates
calendar = Date.CAL_GREGORIAN
quality = choice( (Date.QUAL_NONE, Date.QUAL_ESTIMATED, Date.QUAL_CALCULATED))
modifier = choice( (Date.MOD_NONE, Date.MOD_BEFORE, Date.MOD_AFTER,\
Date.MOD_ABOUT, Date.MOD_RANGE, Date.MOD_SPAN))
day = randint(0,28)
if day > 0: # avoid days without month
month = randint(1,12)
else:
month = randint(0,12)
if modifier in (Date.MOD_RANGE, Date.MOD_SPAN):
day2 = randint(0,28)
if day2 > 0:
month2 = randint(1,12)
else:
month2 = randint(0,12)
year2 = year + randint(1,5)
ndate.set(quality,modifier,calendar,(day,month,year,False,day2,month2,year2,False),"")
else:
ndate.set(quality,modifier,calendar,(day,month,year,False),"")
return (year, ndate)
def rand_event( self, type=None, start=None, end=None):
e = RelLib.Event()
if not type:
type = choice( ("Census", "Degree", "Emigration", "Immigration"))
e.set_name( type)
if randint(0,1) == 1:
e.set_note("Generated by TestcaseGenerator")
if randint(0,1) == 1:
e.set_cause("Test cause")
if randint(0,1) == 1:
e.set_description("Test description")
if randint(0,1) == 1:
e.add_source_reference(self.default_sourceref)
if randint(0,1) == 1:
e.add_media_reference(self.default_mediaref)
if randint(0,1) == 1:
e.set_place_handle(self.default_place.get_handle())
(year, d) = self.rand_date( start, end)
e.set_date_object( d)
return (year, e)
def commit_transaction(self):
if self.options.handler.options_dict['no_trans']:
self.db.transaction_commit(self.trans,_("Testcase generator step %d") % self.transaction_count)