* src/GrampsDb/_ReadGedcomp.py: preselect gramps ids for events

svn: r5843
This commit is contained in:
Don Allingham 2006-01-27 18:53:22 +00:00
parent 25c2210169
commit 3b1f96013a
2 changed files with 75 additions and 41 deletions

View File

@ -1,3 +1,6 @@
2006-01-27 Don Allingham <don@gramps-project.org>
* src/GrampsDb/_ReadGedcomp.py: preselect gramps ids for events
2006-01-26 Don Allingham <don@gramps-project.org> 2006-01-26 Don Allingham <don@gramps-project.org>
* src/DisplayModels.py: optimization * src/DisplayModels.py: optimization
* src/DisplayTabs.py: improved docs * src/DisplayTabs.py: improved docs

View File

@ -69,6 +69,7 @@ from bsddb import db
from _GedcomInfo import * from _GedcomInfo import *
from _GedTokens import * from _GedTokens import *
from QuestionDialog import ErrorDialog, WarningDialog from QuestionDialog import ErrorDialog, WarningDialog
from GrampsDbBase import EVENT_KEY
addr_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)\s*(.*)') addr_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)\s*(.*)')
addr2_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)') addr2_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)')
@ -245,6 +246,26 @@ class GedcomDateParser(DateParser.DateParser):
'sep' : 9, 'oct' : 10, 'nov' : 11, 'dec' : 12, 'sep' : 9, 'oct' : 10, 'nov' : 11, 'dec' : 12,
} }
class IdFinder:
def __init__(self,keys,prefix):
self.ids = set(keys)
self.index = 0
self.prefix = prefix
def find_next(self):
"""
Returns the next available GRAMPS' ID for a Event object based
off the person ID prefix.
"""
index = self.prefix % self.index
while str(index) in self.ids:
self.index += 1
index = self.prefix % self.index
self.ids.add(index)
self.index += 1
return index
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# #
@ -402,9 +423,14 @@ class GedcomParser:
def __init__(self, dbase, filename, callback, codeset, note_map, lines): def __init__(self, dbase, filename, callback, codeset, note_map, lines):
self.maxlines = lines self.maxlines = lines
self.interval = lines/100
self.percent = 0
self.callback = callback self.callback = callback
self.dp = GedcomDateParser() self.dp = GedcomDateParser()
self.db = dbase self.db = dbase
self.emapper = IdFinder(dbase.get_gramps_ids(EVENT_KEY),
dbase.eprefix)
self.person = None self.person = None
self.inline_srcs = {} self.inline_srcs = {}
self.media_map = {} self.media_map = {}
@ -495,7 +521,7 @@ class GedcomParser:
self.lexer = Reader(filename) self.lexer = Reader(filename)
self.filename = filename self.filename = filename
self.backoff = 0 self.backoff = False
self.override = codeset self.override = codeset
if self.db.get_number_of_people() == 0: if self.db.get_number_of_people() == 0:
@ -513,8 +539,7 @@ class GedcomParser:
self.error_count = 0 self.error_count = 0
amap = Utils.personalConstantAttributes amap = Utils.personalConstantAttributes
self.current = 0 self.current = self.interval
self.oldval = 0
self.attrs = amap.values() self.attrs = amap.values()
self.gedattr = {} self.gedattr = {}
@ -571,43 +596,44 @@ class GedcomParser:
return (0,tries) return (0,tries)
def track_lines(self): def track_lines(self):
self.current += 1 if self.current == 1:
newval = int((100*self.current)/self.maxlines) self.current = self.interval
if self.callback and newval != self.oldval: self.percent += 1
self.callback(newval) if self.callback:
self.oldval = newval self.callback(self.percent)
else:
self.current -= 1
def get_next(self): def get_next(self):
if self.backoff == 0: if self.backoff == False:
next_line = self.lexer.read() self.groups = self.lexer.read()
self.track_lines() self.track_lines()
# EOF ? # EOF ?
if next_line == None: if not self.groups:
self.text = ""; self.text = "";
self.backoff = 0 self.backoff = False
msg = _("Premature end of file at line %d.\n") % next_line[4] msg = _("Premature end of file at line %d.\n") % next_line[4]
self.errmsg(msg) self.errmsg(msg)
self.error_count = self.error_count + 1 self.error_count += 1
self.groups = (-1, TOKEN_UNKNOWN, "","") self.groups = (-1, TOKEN_UNKNOWN, "","")
return self.groups return self.groups
self.groups = next_line self.backoff = False
self.backoff = 0
return self.groups return self.groups
def barf(self,level): def barf(self,level):
msg = _("Line %d was not understood, so it was ignored.") % self.groups[4] msg = _("Line %d was not understood, so it was ignored.") % self.groups[4]
self.errmsg(msg) self.errmsg(msg)
self.error_count = self.error_count + 1 self.error_count += 1
self.ignore_sub_junk(level) self.ignore_sub_junk(level)
def warn(self,msg): def warn(self,msg):
self.errmsg(msg) self.errmsg(msg)
self.error_count = self.error_count + 1 self.error_count += 1
def backup(self): def backup(self):
self.backoff = 1 self.backoff = True
def parse_gedcom_file(self,use_trans=False): def parse_gedcom_file(self,use_trans=False):
@ -748,7 +774,7 @@ class GedcomParser:
self.db.commit_person(child, self.trans) self.db.commit_person(child, self.trans)
if len(self.family.get_source_references()) == 0: if len(self.family.get_source_references()) == 0:
sref = RelLib.SourceRef() sref = RelLib.SourceRef()
sref.set_base_handle(self.def_src.get_handle()) sref.set_base_handle(self.def_src.handle)
self.family.add_source_reference(sref) self.family.add_source_reference(sref)
self.db.commit_family(self.family, self.trans) self.db.commit_family(self.family, self.trans)
del self.family del self.family
@ -757,11 +783,11 @@ class GedcomParser:
gid = matches[3] gid = matches[3]
gid = gid[1:-1] gid = gid[1:-1]
self.person = self.find_or_create_person(self.map_gid(gid)) self.person = self.find_or_create_person(self.map_gid(gid))
self.added.add(self.person.get_handle()) self.added.add(self.person.handle)
self.parse_individual(self.person) self.parse_individual(self.person)
if len(self.person.get_source_references()) == 0: if len(self.person.get_source_references()) == 0:
sref = RelLib.SourceRef() sref = RelLib.SourceRef()
sref.set_base_handle(self.def_src.get_handle()) sref.set_base_handle(self.def_src.handle)
self.person.add_source_reference(sref) self.person.add_source_reference(sref)
self.db.commit_person(self.person, self.trans) self.db.commit_person(self.person, self.trans)
del self.person del self.person
@ -769,7 +795,7 @@ class GedcomParser:
self.repo_count = self.repo_count + 1 self.repo_count = self.repo_count + 1
self.repo = self.find_or_create_repository(matches[3][1:-1]) self.repo = self.find_or_create_repository(matches[3][1:-1])
self.repo.set_type((RelLib.Repository.UNKNOWN,"")) self.repo.set_type((RelLib.Repository.UNKNOWN,""))
self.added.add(self.repo.get_handle()) self.added.add(self.repo.handle)
self.parse_repository(self.repo) self.parse_repository(self.repo)
self.db.commit_repository(self.repo, self.trans) self.db.commit_repository(self.repo, self.trans)
del self.repo del self.repo
@ -1034,21 +1060,21 @@ class GedcomParser:
mrel,frel = self.parse_ftw_relations(2) mrel,frel = self.parse_ftw_relations(2)
gid = matches[2] gid = matches[2]
child = self.find_or_create_person(self.map_gid(gid[1:-1])) child = self.find_or_create_person(self.map_gid(gid[1:-1]))
self.family.add_child_handle(child.get_handle()) self.family.add_child_handle(child.handle)
change = False change = False
for f in child.get_parent_family_handle_list(): for f in child.get_parent_family_handle_list():
if f[0] == self.family.get_handle(): if f[0] == self.family.handle:
if (mrel != f[1] or frel != f[2]): if (mrel != f[1] or frel != f[2]):
change = True change = True
child.change_parent_family_handle(self.family.get_handle(), child.change_parent_family_handle(self.family.handle,
mrel, frel) mrel, frel)
break break
else: else:
change = True change = True
if mrel in rel_types and frel in rel_types: if mrel in rel_types and frel in rel_types:
child.set_main_parent_family_handle(self.family.get_handle()) child.set_main_parent_family_handle(self.family.handle)
else: else:
if child.get_main_parents_family_handle() == self.family.handle: if child.get_main_parents_family_handle() == self.family.handle:
child.set_main_parent_family_handle(None) child.set_main_parent_family_handle(None)
@ -1077,6 +1103,7 @@ class GedcomParser:
note = self.parse_note(matches,self.family,1,note) note = self.parse_note(matches,self.family,1,note)
else: else:
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
try: try:
event.set_type((ged2fam[matches[3]],'')) event.set_type((ged2fam[matches[3]],''))
except: except:
@ -1176,7 +1203,7 @@ class GedcomParser:
ftype = pedi_type.get(matches[2],RelLib.Person.UNKNOWN) ftype = pedi_type.get(matches[2],RelLib.Person.UNKNOWN)
elif matches[1] == TOKEN_SOUR: elif matches[1] == TOKEN_SOUR:
source_ref = self.handle_source(matches,level+1) source_ref = self.handle_source(matches,level+1)
person.get_primary_name().add_source_reference(source_ref) person.primary_name.add_source_reference(source_ref)
elif matches[1] == TOKEN__PRIMARY: elif matches[1] == TOKEN__PRIMARY:
pass #type = matches[1] pass #type = matches[1]
elif matches[1] == TOKEN_NOTE: elif matches[1] == TOKEN_NOTE:
@ -1229,11 +1256,11 @@ class GedcomParser:
photo.set_description(title) photo.set_description(title)
photo.set_mime_type(GrampsMime.get_type(os.path.abspath(path))) photo.set_mime_type(GrampsMime.get_type(os.path.abspath(path)))
self.db.add_object(photo, self.trans) self.db.add_object(photo, self.trans)
self.media_map[path] = photo.get_handle() self.media_map[path] = photo.handle
else: else:
photo = self.db.get_object_from_handle(photo_handle) photo = self.db.get_object_from_handle(photo_handle)
oref = RelLib.MediaRef() oref = RelLib.MediaRef()
oref.set_reference_handle(photo.get_handle()) oref.set_reference_handle(photo.handle)
oref.set_note(note) oref.set_note(note)
state.person.add_media_reference(oref) state.person.add_media_reference(oref)
@ -1270,11 +1297,11 @@ class GedcomParser:
photo.set_description(title) photo.set_description(title)
photo.set_mime_type(GrampsMime.get_type(os.path.abspath(path))) photo.set_mime_type(GrampsMime.get_type(os.path.abspath(path)))
self.db.add_object(photo, self.trans) self.db.add_object(photo, self.trans)
self.media_map[path] = photo.get_handle() self.media_map[path] = photo.handle
else: else:
photo = self.db.get_object_from_handle(photo_handle) photo = self.db.get_object_from_handle(photo_handle)
oref = RelLib.MediaRef() oref = RelLib.MediaRef()
oref.set_reference_handle(photo.get_handle()) oref.set_reference_handle(photo.handle)
oref.set_note(note) oref.set_note(note)
self.family.add_media_reference(oref) self.family.add_media_reference(oref)
@ -1364,7 +1391,7 @@ class GedcomParser:
try: try:
place = self.find_or_create_place(matches[2]) place = self.find_or_create_place(matches[2])
place.set_title(matches[2]) place.set_title(matches[2])
place_handle = place.get_handle() place_handle = place.handle
lds_ord.set_place_handle(place_handle) lds_ord.set_place_handle(place_handle)
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
except NameError: except NameError:
@ -1415,7 +1442,7 @@ class GedcomParser:
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
else: else:
place = self.find_or_create_place(val) place = self.find_or_create_place(val)
place_handle = place.get_handle() place_handle = place.handle
place.set_title(matches[2]) place.set_title(matches[2])
event.set_place_handle(place_handle) event.set_place_handle(place_handle)
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
@ -1469,7 +1496,7 @@ class GedcomParser:
elif matches[1] == TOKEN_PLAC: elif matches[1] == TOKEN_PLAC:
val = matches[2] val = matches[2]
place = self.find_or_create_place(val) place = self.find_or_create_place(val)
place_handle = place.get_handle() place_handle = place.handle
place.set_title(matches[2]) place.set_title(matches[2])
event.set_place_handle(place_handle) event.set_place_handle(place_handle)
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
@ -1576,7 +1603,7 @@ class GedcomParser:
elif matches[1] == TOKEN_PLAC: elif matches[1] == TOKEN_PLAC:
val = matches[2] val = matches[2]
place = self.find_or_create_place(val) place = self.find_or_create_place(val)
place_handle = place.get_handle() place_handle = place.handle
place.set_title(matches[2]) place.set_title(matches[2])
event.set_place_handle(place_handle) event.set_place_handle(place_handle)
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
@ -1654,7 +1681,7 @@ class GedcomParser:
def parse_marnm(self,person,text): def parse_marnm(self,person,text):
data = text.split() data = text.split()
if len(data) == 1: if len(data) == 1:
name = RelLib.Name(person.get_primary_name()) name = RelLib.Name(person.primary_name)
name.set_surname(data[0]) name.set_surname(data[0])
name.set_type((RelLib.Name.MARRIED,'')) name.set_type((RelLib.Name.MARRIED,''))
person.add_alternate_name(name) person.add_alternate_name(name)
@ -1780,8 +1807,6 @@ class GedcomParser:
if int(matches[0]) < level: if int(matches[0]) < level:
self.backup() self.backup()
return return
# else:
# print self.lexer.current
return return
def ignore_change_data(self,level): def ignore_change_data(self,level):
@ -1886,7 +1911,7 @@ class GedcomParser:
self.inline_srcs[(title,note)] = handle self.inline_srcs[(title,note)] = handle
self.ignore_sub_junk(level+1) self.ignore_sub_junk(level+1)
else: else:
handle = self.find_or_create_source(matches[2][1:-1]).get_handle() handle = self.find_or_create_source(matches[2][1:-1]).handle
self.parse_source_reference(source_ref,level) self.parse_source_reference(source_ref,level)
source_ref.set_base_handle(handle) source_ref.set_base_handle(handle)
return source_ref return source_ref
@ -2047,6 +2072,7 @@ class GedcomParser:
def func_person_birt(self,matches,state): def func_person_birt(self,matches,state):
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
if matches[2]: if matches[2]:
event.set_description(matches[2]) event.set_description(matches[2])
event.set_type((RelLib.Event.BIRTH,"")) event.set_type((RelLib.Event.BIRTH,""))
@ -2066,6 +2092,7 @@ class GedcomParser:
def func_person_adop(self,matches,state): def func_person_adop(self,matches,state):
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
event.set_type((RelLib.Event.ADOPT,'')) event.set_type((RelLib.Event.ADOPT,''))
self.parse_adopt_event(event,2) self.parse_adopt_event(event,2)
person_event_name(event,state.person) person_event_name(event,state.person)
@ -2078,6 +2105,7 @@ class GedcomParser:
def func_person_deat(self,matches,state): def func_person_deat(self,matches,state):
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
if matches[2]: if matches[2]:
event.set_description(matches[2]) event.set_description(matches[2])
event.set_type((RelLib.Event.DEATH,"")) event.set_type((RelLib.Event.DEATH,""))
@ -2097,6 +2125,7 @@ class GedcomParser:
def func_person_even(self,matches,state): def func_person_even(self,matches,state):
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
if matches[2]: if matches[2]:
event.set_description(matches[2]) event.set_description(matches[2])
self.parse_person_event(event,2) self.parse_person_event(event,2)
@ -2120,7 +2149,7 @@ class GedcomParser:
def func_person_refn(self,matches,state): def func_person_refn(self,matches,state):
if intRE.match(matches[2]): if intRE.match(matches[2]):
try: try:
self.refn[self.person.get_handle()] = int(matches[2]) self.refn[self.person.handle] = int(matches[2])
except: except:
pass pass
@ -2147,9 +2176,11 @@ class GedcomParser:
return return
elif ged2gramps.has_key(n): elif ged2gramps.has_key(n):
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
event.set_type((ged2gramps[n],'')) event.set_type((ged2gramps[n],''))
else: else:
event = RelLib.Event() event = RelLib.Event()
event.set_gramps_id(self.emapper.find_next())
val = self.gedsource.tag2gramps(n) val = self.gedsource.tag2gramps(n)
if val: if val:
event.set_type((RelLib.Event.CUSTOM,val)) event.set_type((RelLib.Event.CUSTOM,val))
@ -2163,7 +2194,7 @@ class GedcomParser:
self.db.add_event(event, self.trans) self.db.add_event(event, self.trans)
event_ref = RelLib.EventRef() event_ref = RelLib.EventRef()
event_ref.set_reference_handle(event.get_handle()) event_ref.set_reference_handle(event.handle)
event_ref.set_role((RelLib.EventRef.PRIMARY,'')) event_ref.set_role((RelLib.EventRef.PRIMARY,''))
state.person.add_event_ref(event_ref) state.person.add_event_ref(event_ref)