* src/GrampsDb/_GrampsBSDDB.py (get_gramps_ids): Move to

GrampsDbBase; (_get_obj_from_gramps_id,_find_from_handle): Only
read data if the key exists.
* src/GrampsDb/_GrampsDbBase.py (get_gramps_ids,has_gramps_id):
Add methods; (check_*_from_handle): Add methods that make sure
that object exists in the db, create as necessary, but do not read
if it exists; (_check_from_handle): Add lower-level method;
(_find_from_handle): Only read data if the key exists.
* src/GrampsDb/_GrampsInMemDB.py (get_gramps_ids): Move to
GrampsDbBase.
* src/GrampsDb/_ReadXML.py: Multiple improvements.


svn: r5858
This commit is contained in:
Alex Roitman 2006-01-31 01:41:55 +00:00
parent 2c4f2b8225
commit 2c1d8b226c
5 changed files with 402 additions and 268 deletions

View File

@ -1,3 +1,16 @@
2006-01-30 Alex Roitman <shura@gramps-project.org>
* src/GrampsDb/_GrampsBSDDB.py (get_gramps_ids): Move to
GrampsDbBase; (_get_obj_from_gramps_id,_find_from_handle): Only
read data if the key exists.
* src/GrampsDb/_GrampsDbBase.py (get_gramps_ids,has_gramps_id):
Add methods; (check_*_from_handle): Add methods that make sure
that object exists in the db, create as necessary, but do not read
if it exists; (_check_from_handle): Add lower-level method;
(_find_from_handle): Only read data if the key exists.
* src/GrampsDb/_GrampsInMemDB.py (get_gramps_ids): Move to
GrampsDbBase.
* src/GrampsDb/_ReadXML.py: Multiple improvements.
2006-01-29 Don Allingham <don@gramps-project.org> 2006-01-29 Don Allingham <don@gramps-project.org>
* src/DdTargets.py: Add support for EventRef * src/DdTargets.py: Add support for EventRef
* src/DisplayTabs.py: Drag and drop support in base class * src/DisplayTabs.py: Drag and drop support in base class

View File

@ -842,20 +842,6 @@ class GrampsBSDDB(GrampsDbBase):
self.name_group.put(name,group,txn=self.txn) self.name_group.put(name,group,txn=self.txn)
self.emit('person-rebuild') self.emit('person-rebuild')
def get_gramps_ids(self,obj_key):
key2table = {
PERSON_KEY: self.id_trans,
FAMILY_KEY: self.fid_trans,
SOURCE_KEY: self.sid_trans,
EVENT_KEY: self.eid_trans,
MEDIA_KEY: self.oid_trans,
PLACE_KEY: self.pid_trans,
REPOSITORY_KEY: self.rid_trans,
}
table = key2table[obj_key]
return table.keys()
def get_surname_list(self): def get_surname_list(self):
vals = [ (locale.strxfrm(unicode(val)),unicode(val)) vals = [ (locale.strxfrm(unicode(val)),unicode(val))
for val in set(self.surnames.keys()) ] for val in set(self.surnames.keys()) ]
@ -873,8 +859,8 @@ class GrampsBSDDB(GrampsDbBase):
return vals return vals
def _get_obj_from_gramps_id(self,val,tbl,class_init): def _get_obj_from_gramps_id(self,val,tbl,class_init):
data = tbl.get(str(val),txn=self.txn) if tbl.has_key(str(val)):
if data: data = tbl.get(str(val),txn=self.txn)
obj = class_init() obj = class_init()
obj.unserialize(cPickle.loads(data)) obj.unserialize(cPickle.loads(data))
return obj return obj
@ -970,8 +956,8 @@ class GrampsBSDDB(GrampsDbBase):
def _find_from_handle(self,handle,transaction,class_type,dmap,add_func): def _find_from_handle(self,handle,transaction,class_type,dmap,add_func):
obj = class_type() obj = class_type()
handle = str(handle) handle = str(handle)
data = dmap.get(handle,txn=self.txn) if dmap.has_key(handle):
if data: data = dmap.get(handle,txn=self.txn)
obj.unserialize(data) obj.unserialize(data)
else: else:
obj.set_handle(handle) obj.set_handle(handle)

View File

@ -625,13 +625,20 @@ class GrampsDbBase(GrampsDBCallback):
def _find_from_handle(self,handle,transaction,class_type,dmap,add_func): def _find_from_handle(self,handle,transaction,class_type,dmap,add_func):
obj = class_type() obj = class_type()
handle = str(handle) handle = str(handle)
if dmap.get(handle): if dmap.has_key(handle):
obj.unserialize(dmap.get(handle)) obj.unserialize(dmap.get(handle))
else: else:
obj.set_handle(handle) obj.set_handle(handle)
add_func(obj,transaction) add_func(obj,transaction)
return obj return obj
def _check_from_handle(self,handle,transaction,class_type,dmap,add_func):
handle = str(handle)
if not dmap.has_key(handle):
obj = class_type()
obj.set_handle(handle)
add_func(obj,transaction)
def find_person_from_handle(self,handle,transaction): def find_person_from_handle(self,handle,transaction):
""" """
Finds a Person in the database from the passed GRAMPS ID. Finds a Person in the database from the passed GRAMPS ID.
@ -642,7 +649,7 @@ class GrampsDbBase(GrampsDBCallback):
def find_source_from_handle(self,handle,transaction): def find_source_from_handle(self,handle,transaction):
""" """
Finds a Source in the database from the passed GRAMPS ID. Finds a Source in the database from the passed handle.
If no such Source exists, a new Source is added to the database. If no such Source exists, a new Source is added to the database.
""" """
return self._find_from_handle(handle,transaction,Source, return self._find_from_handle(handle,transaction,Source,
@ -650,7 +657,7 @@ class GrampsDbBase(GrampsDBCallback):
def find_event_from_handle(self,handle,transaction): def find_event_from_handle(self,handle,transaction):
""" """
Finds a Event in the database from the passed GRAMPS ID. Finds a Event in the database from the passed handle.
If no such Event exists, a new Event is added to the database. If no such Event exists, a new Event is added to the database.
""" """
return self._find_from_handle(handle,transaction,Event, return self._find_from_handle(handle,transaction,Event,
@ -658,15 +665,15 @@ class GrampsDbBase(GrampsDBCallback):
def find_object_from_handle(self,handle,transaction): def find_object_from_handle(self,handle,transaction):
""" """
Finds an MediaObject in the database from the passed GRAMPS ID. Finds an MediaObject in the database from the passed handle.
If no such MediaObject exists, a new Object is added to the database.""" If no such MediaObject exists, a new Object is added to the database.
"""
return self._find_from_handle(handle,transaction,MediaObject, return self._find_from_handle(handle,transaction,MediaObject,
self.media_map,self.add_object) self.media_map,self.add_object)
def find_place_from_handle(self,handle,transaction): def find_place_from_handle(self,handle,transaction):
""" """
Finds a Place in the database from the passed GRAMPS ID. Finds a Place in the database from the passed handle.
If no such Place exists, a new Place is added to the database. If no such Place exists, a new Place is added to the database.
""" """
return self._find_from_handle(handle,transaction,Place, return self._find_from_handle(handle,transaction,Place,
@ -674,7 +681,7 @@ class GrampsDbBase(GrampsDBCallback):
def find_family_from_handle(self,handle,transaction): def find_family_from_handle(self,handle,transaction):
""" """
Finds a Family in the database from the passed gramps' ID. Finds a Family in the database from the passed handle.
If no such Family exists, a new Family is added to the database. If no such Family exists, a new Family is added to the database.
""" """
return self._find_from_handle(handle,transaction,Family, return self._find_from_handle(handle,transaction,Family,
@ -682,12 +689,71 @@ class GrampsDbBase(GrampsDBCallback):
def find_repository_from_handle(self,handle,transaction): def find_repository_from_handle(self,handle,transaction):
""" """
Finds a Repository in the database from the passed gramps' ID. Finds a Repository in the database from the passed handle.
If no such Repository exists, a new Repository is added to the database. If no such Repository exists, a new Repository is added to the database.
""" """
return self._find_from_handle(handle,transaction,Repository, return self._find_from_handle(handle,transaction,Repository,
self.repository_map,self.add_repository) self.repository_map,self.add_repository)
def check_person_from_handle(self,handle,transaction):
"""
Checks whether a Person with the passed handle exists in the database.
If no such Person exists, a new Person is added to the database.
"""
self._check_from_handle(handle,transaction,Person,
self.person_map,self.add_person)
def check_source_from_handle(self,handle,transaction):
"""
Checks whether a Source with the passed handle exists in the database.
If no such Source exists, a new Source is added to the database.
"""
self._check_from_handle(handle,transaction,Source,
self.source_map,self.add_source)
def check_event_from_handle(self,handle,transaction):
"""
Checks whether an Event with the passed handle exists in the database.
If no such Event exists, a new Event is added to the database.
"""
self._check_from_handle(handle,transaction,Event,
self.event_map,self.add_event)
def check_object_from_handle(self,handle,transaction):
"""
Checks whether a MediaObject with the passed handle exists in
the database. If no such MediaObject exists, a new Object is
added to the database.
"""
self._check_from_handle(handle,transaction,MediaObject,
self.media_map,self.add_object)
def check_place_from_handle(self,handle,transaction):
"""
Checks whether a Place with the passed handle exists in the database.
If no such Place exists, a new Place is added to the database.
"""
self._check_from_handle(handle,transaction,Place,
self.place_map,self.add_place)
def check_family_from_handle(self,handle,transaction):
"""
Checks whether a Family with the passed handle exists in the database.
If no such Family exists, a new Family is added to the database.
"""
self._check_from_handle(handle,transaction,Family,
self.family_map,self.add_family)
def check_repository_from_handle(self,handle,transaction):
"""
Checks whether a Repository with the passed handle exists in the
database. If no such Repository exists, a new Repository is added
to the database.
"""
self._check_from_handle(handle,transaction,Repository,
self.repository_map,self.add_repository)
def get_person_from_gramps_id(self,val): def get_person_from_gramps_id(self,val):
""" """
Finds a Person in the database from the passed GRAMPS ID. Finds a Person in the database from the passed GRAMPS ID.
@ -982,12 +1048,32 @@ class GrampsDbBase(GrampsDBCallback):
return [] return []
def get_gramps_ids(self,obj_key): def get_gramps_ids(self,obj_key):
""" key2table = {
Returns the list of gramps IDs contained within the database PERSON_KEY: self.id_trans,
for the objects of the obj_key type. FAMILY_KEY: self.fid_trans,
The function must be overridden in the derived class. SOURCE_KEY: self.sid_trans,
""" EVENT_KEY: self.eid_trans,
assert False, "Needs to be overridden in the derived class" MEDIA_KEY: self.oid_trans,
PLACE_KEY: self.pid_trans,
REPOSITORY_KEY: self.rid_trans,
}
table = key2table[obj_key]
return table.keys()
def has_gramps_id(self,obj_key,gramps_id):
key2table = {
PERSON_KEY: self.id_trans,
FAMILY_KEY: self.fid_trans,
SOURCE_KEY: self.sid_trans,
EVENT_KEY: self.eid_trans,
MEDIA_KEY: self.oid_trans,
PLACE_KEY: self.pid_trans,
REPOSITORY_KEY: self.rid_trans,
}
table = key2table[obj_key]
return table.has_key(str(gramps_id))
def find_initial_person(self): def find_initial_person(self):
person = self.get_default_person() person = self.get_default_person()

View File

@ -146,27 +146,6 @@ class GrampsInMemDB(GrampsDbBase):
else: else:
self.name_group[name] = group self.name_group[name] = group
def get_gramps_ids(self,obj_key):
"""
Returns the list of gramps IDs contained within the database
for the objects of the obj_key type.
The function must be overridden in the derived class.
"""
key2table_getfun = {
PERSON_KEY: (self.person_map, self.get_person_from_handle),
FAMILY_KEY: (self.family_map, self.get_family_from_handle),
SOURCE_KEY: (self.source_map, self.get_source_from_handle),
EVENT_KEY: (self.event_map, self.get_event_from_handle),
MEDIA_KEY: (self.media_map, self.get_object_from_handle),
PLACE_KEY: (self.place_map, self.get_place_from_handle),
REPOSITORY_KEY: (self.repository_map,
self.get_repository_from_handle),
}
table,getfun = key2table_getfun[obj_key]
return [getfun(handle).gramps_id for handle in iter(table)]
def get_surname_list(self): def get_surname_list(self):
a = {} a = {}
for person_id in iter(self.person_map): for person_id in iter(self.person_map):

View File

@ -52,6 +52,9 @@ import Utils
import DateHandler import DateHandler
import NameDisplay import NameDisplay
import _ConstXML import _ConstXML
from _GrampsDbBase import \
PERSON_KEY,FAMILY_KEY,SOURCE_KEY,EVENT_KEY,\
MEDIA_KEY,PLACE_KEY,REPOSITORY_KEY
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -64,30 +67,13 @@ try:
except: except:
gzip_ok = 0 gzip_ok = 0
_FAMILY_TRANS = {
'Married' : RelLib.Family.MARRIED,
'Unmarried' : RelLib.Family.UNMARRIED,
'Partners' : RelLib.Family.UNMARRIED,
'Civil Union' : RelLib.Family.CIVIL_UNION,
'Unknown' : RelLib.Family.UNKNOWN,
'Other' : RelLib.Family.CUSTOM,
}
_NAME_TRANS = {
"Unknown" : RelLib.Name.UNKNOWN,
"Custom" : RelLib.Name.CUSTOM,
"Also Known As" : RelLib.Name.AKA,
"Birth Name" : RelLib.Name.BIRTH,
"Married Name" : RelLib.Name.MARRIED,
}
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Importing data into the currently open database. # Importing data into the currently open database.
# Must takes care of renaming media files according to their new IDs. # Must takes care of renaming media files according to their new IDs.
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def importData(database, filename, callback=None,cl=0,use_trans=True): def importData(database, filename, callback=None,cl=0,use_trans=False):
filename = os.path.normpath(filename) filename = os.path.normpath(filename)
basefile = os.path.dirname(filename) basefile = os.path.dirname(filename)
@ -98,8 +84,11 @@ def importData(database, filename, callback=None,cl=0,use_trans=True):
change = os.path.getmtime(filename) change = os.path.getmtime(filename)
parser = GrampsParser(database,callback,basefile,change,filename) parser = GrampsParser(database,callback,basefile,change,filename)
print "starting line parser"
linecounter = LineParser(filename) linecounter = LineParser(filename)
print "finished parser"
lc = linecounter.get_count() lc = linecounter.get_count()
print "got line count:", lc
ro = database.readonly ro = database.readonly
database.readonly = False database.readonly = False
@ -267,20 +256,13 @@ class GrampsParser:
self.conf = 2 self.conf = 2
self.gid2id = {} self.gid2id = {}
self.gid2fid = {} self.gid2fid = {}
self.gid2eid = {}
self.gid2pid = {} self.gid2pid = {}
self.gid2oid = {} self.gid2oid = {}
self.gid2sid = {} self.gid2sid = {}
self.gid2rid = {}
self.change = change self.change = change
self.dp = DateHandler.parser self.dp = DateHandler.parser
self.child_relmap = {
"None" : RelLib.Person.CHILD_NONE,
"Birth" : RelLib.Person.CHILD_BIRTH,
"Adopted" : RelLib.Person.CHILD_ADOPTED,
"Stepchild" : RelLib.Person.CHILD_STEPCHILD,
"Sponsored" : RelLib.Person.CHILD_SPONSORED,
"Foster" : RelLib.Person.CHILD_FOSTER,
"Unknown" : RelLib.Person.CHILD_UNKNOWN,
}
self.place_names = sets.Set() self.place_names = sets.Set()
cursor = database.get_place_cursor() cursor = database.get_place_cursor()
data = cursor.next() data = cursor.next()
@ -293,6 +275,8 @@ class GrampsParser:
self.ord = None self.ord = None
self.objref = None self.objref = None
self.object = None self.object = None
self.repo = None
self.reporef = None
self.pref = None self.pref = None
self.use_p = 0 self.use_p = 0
self.in_note = 0 self.in_note = 0
@ -341,9 +325,11 @@ class GrampsParser:
self.witness_comment = "" self.witness_comment = ""
self.idswap = {} self.idswap = {}
self.fidswap = {} self.fidswap = {}
self.eidswap = {}
self.sidswap = {} self.sidswap = {}
self.pidswap = {} self.pidswap = {}
self.oidswap = {} self.oidswap = {}
self.ridswap = {}
self.eidswap = {} self.eidswap = {}
self.func_map = { self.func_map = {
@ -432,69 +418,10 @@ class GrampsParser:
"street" : (None, self.stop_street), "street" : (None, self.stop_street),
"suffix" : (None, self.stop_suffix), "suffix" : (None, self.stop_suffix),
"title" : (None, self.stop_title), "title" : (None, self.stop_title),
"url" : (self.start_url, None) "url" : (self.start_url, None),
} "repository" : (self.start_repo,self.stop_repo),
"reporef" : (self.start_reporef,self.stop_reporef),
self.save_attr = { "rname" : (None, self.stop_rname),
"Unknown" : RelLib.Attribute.UNKNOWN,
"Custom" : RelLib.Attribute.CUSTOM,
"Caste" : RelLib.Attribute.CASTE,
"Description" : RelLib.Attribute.DESCRIPTION,
"Identification Number" : RelLib.Attribute.ID,
"National Origin" : RelLib.Attribute.NATIONAL,
"Number of Children" : RelLib.Attribute.NUM_CHILD,
"Social Security Number" : RelLib.Attribute.SSN,
"Number of Children" : RelLib.Attribute.NUM_CHILD,
}
self.save_event = {
"Unknown" : RelLib.Event.UNKNOWN,
"Custom" : RelLib.Event.CUSTOM,
"Marriage" : RelLib.Event.MARRIAGE,
"Marriage Settlement" : RelLib.Event.MARR_SETTL,
"Marriage License" : RelLib.Event.MARR_LIC,
"Marriage Contract" : RelLib.Event.MARR_CONTR,
"Marriage Banns" : RelLib.Event.MARR_BANNS,
"Engagement" : RelLib.Event.ENGAGEMENT,
"Divorce" : RelLib.Event.DIVORCE,
"Divorce Filing" : RelLib.Event.DIV_FILING,
"Annulment" : RelLib.Event.ANNULMENT,
"Alternate Marriage" : RelLib.Event.MARR_ALT,
"Adopted" : RelLib.Event.ADOPT,
"Birth" : RelLib.Event.BIRTH,
"Death" : RelLib.Event.DEATH,
"Adult Christening" : RelLib.Event.ADULT_CHRISTEN,
"Baptism" : RelLib.Event.BAPTISM,
"Bar Mitzvah" : RelLib.Event.BAR_MITZVAH,
"Bas Mitzvah" : RelLib.Event.BAS_MITZVAH,
"Blessing" : RelLib.Event.BLESS,
"Burial" : RelLib.Event.BURIAL,
"Cause Of Death" : RelLib.Event.CAUSE_DEATH,
"Census" : RelLib.Event.CENSUS,
"Christening" : RelLib.Event.CHRISTEN,
"Confirmation" : RelLib.Event.CONFIRMATION,
"Cremation" : RelLib.Event.CREMATION,
"Degree" : RelLib.Event.DEGREE,
"Divorce Filing" : RelLib.Event.DIV_FILING,
"Education" : RelLib.Event.EDUCATION,
"Elected" : RelLib.Event.ELECTED,
"Emigration" : RelLib.Event.EMIGRATION,
"First Communion" : RelLib.Event.FIRST_COMMUN,
"Immigration" : RelLib.Event.IMMIGRATION,
"Graduation" : RelLib.Event.GRADUATION,
"Medical Information" : RelLib.Event.MED_INFO,
"Military Service" : RelLib.Event.MILITARY_SERV,
"Naturalization" : RelLib.Event.NATURALIZATION,
"Nobility Title" : RelLib.Event.NOB_TITLE,
"Number of Marriages" : RelLib.Event.NUM_MARRIAGES,
"Occupation" : RelLib.Event.OCCUPATION,
"Ordination" : RelLib.Event.ORDINATION,
"Probate" : RelLib.Event.PROBATE,
"Property" : RelLib.Event.PROPERTY,
"Religion" : RelLib.Event.RELIGION,
"Residence" : RelLib.Event.RESIDENCE,
"Retirement" : RelLib.Event.RETIREMENT,
"Will" : RelLib.Event.WILL,
} }
def find_person_by_gramps_id(self,gramps_id): def find_person_by_gramps_id(self,gramps_id):
@ -523,6 +450,19 @@ class GrampsParser:
self.gid2fid[gramps_id] = intid self.gid2fid[gramps_id] = intid
return family return family
def find_event_by_gramps_id(self,gramps_id):
intid = self.gid2eid.get(gramps_id)
if intid:
event = self.db.get_event_from_handle(intid)
else:
intid = Utils.create_id()
event = RelLib.Event()
event.set_handle(intid)
event.set_gramps_id(gramps_id)
self.db.add_event(event,self.trans)
self.gid2eid[gramps_id] = intid
return event
def find_place_by_gramps_id(self,gramps_id): def find_place_by_gramps_id(self,gramps_id):
intid = self.gid2pid.get(gramps_id) intid = self.gid2pid.get(gramps_id)
if intid: if intid:
@ -562,47 +502,76 @@ class GrampsParser:
self.gid2oid[gramps_id] = intid self.gid2oid[gramps_id] = intid
return obj return obj
def map_gid(self,handle): def find_repository_by_gramps_id(self,gramps_id):
if not self.idswap.get(handle): intid = self.gid2rid.get(gramps_id)
if self.db.get_person_from_gramps_id(handle): if intid:
self.idswap[handle] = self.db.find_next_person_gramps_id() repo = self.db.get_repository_from_handle(intid)
else: else:
self.idswap[handle] = handle intid = Utils.create_id()
return self.idswap[handle] repo = RelLib.Repository()
repo.set_handle(intid)
repo.set_gramps_id(gramps_id)
self.db.add_repository(repo,self.trans)
self.gid2rid[gramps_id] = intid
return repo
def map_fid(self,handle): def map_gid(self,gramps_id):
if not self.fidswap.get(handle): if not self.idswap.get(gramps_id):
if self.db.get_family_from_gramps_id(handle): if self.db.has_gramps_id(PERSON_KEY,gramps_id):
self.fidswap[handle] = self.db.find_next_family_gramps_id() self.idswap[gramps_id] = self.db.find_next_person_gramps_id()
else: else:
self.fidswap[handle] = handle self.idswap[gramps_id] = gramps_id
return self.fidswap[handle] return self.idswap[gramps_id]
def map_pid(self,handle): def map_fid(self,gramps_id):
if not self.pidswap.get(handle): if not self.fidswap.get(gramps_id):
if self.db.get_place_from_gramps_id(handle): if self.db.has_gramps_id(FAMILY_KEY,gramps_id):
self.pidswap[handle] = self.db.find_next_place_gramps_id() self.fidswap[gramps_id] = self.db.find_next_family_gramps_id()
else: else:
self.pidswap[handle] = handle self.fidswap[gramps_id] = gramps_id
return self.pidswap[handle] return self.fidswap[gramps_id]
def map_sid(self,handle): def map_eid(self,gramps_id):
if not self.sidswap.get(handle): if not self.eidswap.get(gramps_id):
if self.db.get_source_from_gramps_id(handle): if self.db.has_gramps_id(EVENT_KEY,gramps_id):
self.sidswap[handle] = self.db.find_next_source_gramps_id() self.eidswap[gramps_id] = self.db.find_next_event_gramps_id()
else: else:
self.sidswap[handle] = handle self.eidswap[gramps_id] = gramps_id
return self.sidswap[handle] return self.eidswap[gramps_id]
def map_oid(self,handle): def map_pid(self,gramps_id):
if not self.oidswap.get(handle): if not self.pidswap.get(gramps_id):
if self.db.get_object_from_gramps_id(handle): if self.db.has_gramps_id(PLACE_KEY,gramps_id):
self.oidswap[handle] = self.db.find_next_object_gramps_id() self.pidswap[gramps_id] = self.db.find_next_place_gramps_id()
else: else:
self.oidswap[handle] = handle self.pidswap[gramps_id] = gramps_id
return self.oidswap[handle] return self.pidswap[gramps_id]
def parse(self,file,use_trans=True,linecount=0): def map_sid(self,gramps_id):
if not self.sidswap.get(gramps_id):
if self.db.has_gramps_id(SOURCE_KEY,gramps_id):
self.sidswap[gramps_id] = self.db.find_next_source_gramps_id()
else:
self.sidswap[gramps_id] = gramps_id
return self.sidswap[gramps_id]
def map_oid(self,gramps_id):
if not self.oidswap.get(gramps_id):
if self.db.has_gramps_id(MEDIA_KEY,gramps_id):
self.oidswap[gramps_id] = self.db.find_next_object_gramps_id()
else:
self.oidswap[gramps_id] = gramps_id
return self.oidswap[gramps_id]
def map_rid(self,gramps_id):
if not self.ridswap.get(gramps_id):
if self.db.has_gramps_id(REPOSITORY_KEY,gramps_id):
self.ridswap[gramps_id] = self.db.find_next_repository_gramps_id()
else:
self.ridswap[gramps_id] = gramps_id
return self.ridswap[gramps_id]
def parse(self,file,use_trans=False,linecount=0):
self.trans = self.db.transaction_begin("",batch=True) self.trans = self.db.transaction_begin("",batch=True)
self.linecount = linecount self.linecount = linecount
@ -630,8 +599,7 @@ class GrampsParser:
del self.func_map del self.func_map
del self.func_list del self.func_list
del self.p del self.p
if use_trans: self.db.transaction_commit(self.trans,_("GRAMPS XML import"))
self.db.transaction_commit(self.trans,_("GRAMPS XML import"))
self.db.enable_signals() self.db.enable_signals()
self.db.request_rebuild() self.db.request_rebuild()
@ -660,33 +628,33 @@ class GrampsParser:
def start_sealed_to(self,attrs): def start_sealed_to(self,attrs):
try: try:
family = self.db.find_family_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_family_from_handle(handle,self.trans)
except KeyError: except KeyError:
handle = self.map_fid(attrs['ref']) gramps_id = self.map_fid(attrs['ref'])
family = self.find_family_by_gramps_id(handle) family = self.find_family_by_gramps_id(gramps_id)
self.ord.set_family_handle(family.get_handle()) handle = family.handle
self.ord.set_family_handle(handle)
def start_place(self,attrs): def start_place(self,attrs):
try: try:
self.placeobj = self.db.find_place_from_handle( self.placeobj = self.db.find_place_from_handle(
attrs['hlink'].replace('_',''),self.trans) attrs['hlink'].replace('_',''),self.trans)
except KeyError: except KeyError:
handle = self.map_pid(attrs['ref']) gramps_id = self.map_pid(attrs['ref'])
self.placeobj = self.find_place_by_gramps_id(handle) self.placeobj = self.find_place_by_gramps_id(gramps_id)
def start_placeobj(self,attrs): def start_placeobj(self,attrs):
handle = self.map_pid(attrs['id']) gramps_id = self.map_pid(attrs['id'])
try: try:
self.placeobj = self.db.find_place_from_handle( self.placeobj = self.db.find_place_from_handle(
attrs['handle'].replace('_',''),self.trans) attrs['handle'].replace('_',''),self.trans)
self.placeobj.set_gramps_id(handle) self.placeobj.set_gramps_id(gramps_id)
except KeyError: except KeyError:
self.placeobj = self.find_place_by_gramps_id(handle) self.placeobj = self.find_place_by_gramps_id(gramps_id)
# GRAMPS LEGACY: title in the placeobj tag # GRAMPS LEGACY: title in the placeobj tag
self.placeobj.title = attrs.get('title','') self.placeobj.title = attrs.get('title','')
self.locations = 0 self.locations = 0
self.update() self.update()
def start_location(self,attrs): def start_location(self,attrs):
@ -717,10 +685,10 @@ class GrampsParser:
self.event.set_note(note_text) self.event.set_note(note_text)
return return
if attrs.has_key('hlink'): try:
person = self.db.find_person_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) person = self.db.find_person_from_handle(handle,self.trans)
elif attrs.has_key('ref'): except KeyError:
person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"]))
# Add an EventRef from that person # Add an EventRef from that person
# to this event using ROLE_WITNESS role # to this event using ROLE_WITNESS role
@ -735,19 +703,26 @@ class GrampsParser:
self.placeobj.long = attrs.get('long','') self.placeobj.long = attrs.get('long','')
def start_event(self,attrs): def start_event(self,attrs):
self.event = RelLib.Event()
if self.person or self.family: if self.person or self.family:
# GRAMPS LEGACY: old events that were written inside # GRAMPS LEGACY: old events that were written inside
# person or family objects. # person or family objects.
self.event = RelLib.Event()
self.event.handle = Utils.create_id() self.event.handle = Utils.create_id()
self.event.type = _ConstXML.tuple_from_xml(_ConstXML.events, self.event.type = _ConstXML.tuple_from_xml(_ConstXML.events,
attrs['type']) attrs['type'])
self.db.add_event(self.event,self.trans)
else: else:
# This is new event, with handle already existing # This is new event, with ID and handle already existing
self.event.handle = attrs['handle'].replace('_','') self.update()
gramps_id = self.map_eid(attrs["id"])
try:
self.event = self.db.find_event_from_handle(
attrs['handle'].replace('_',''),self.trans)
self.event.gramps_id = gramps_id
except KeyError:
self.event = self.find_event_by_gramps_id(gramps_id)
self.event.conf = int(attrs.get("conf",2)) self.event.conf = int(attrs.get("conf",2))
self.event.private = bool(attrs.get("priv")) self.event.private = bool(attrs.get("priv"))
self.db.add_event(self.event,self.trans)
def start_eventref(self,attrs): def start_eventref(self,attrs):
self.eventref = RelLib.EventRef() self.eventref = RelLib.EventRef()
@ -788,18 +763,18 @@ class GrampsParser:
def start_address(self,attrs): def start_address(self,attrs):
self.address = RelLib.Address() self.address = RelLib.Address()
self.person.add_address(self.address)
self.address.conf = int(attrs.get("conf",2)) self.address.conf = int(attrs.get("conf",2))
self.address.private = bool(attrs.get("priv")) self.address.private = bool(attrs.get("priv"))
def start_bmark(self,attrs): def start_bmark(self,attrs):
try: try:
person = self.db.find_person_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_person_from_handle(handle,self.trans)
except KeyError: except KeyError:
handle = self.map_gid(attrs["ref"]) gramps_id = self.map_gid(attrs["ref"])
person = self.find_person_by_gramps_id(handle) person = self.find_person_by_gramps_id(gramps_id)
self.db.bookmarks.append(person.get_handle()) handle = person.handle
self.db.bookmarks.append(handle)
def start_person(self,attrs): def start_person(self,attrs):
self.update() self.update()
@ -826,27 +801,30 @@ class GrampsParser:
def start_father(self,attrs): def start_father(self,attrs):
try: try:
person = self.db.find_person_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_person_from_handle(handle,self.trans)
except KeyError: except KeyError:
person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"]))
self.family.set_father_handle(person.get_handle()) handle = person_handle
self.family.set_father_handle(handle)
def start_mother(self,attrs): def start_mother(self,attrs):
try: try:
person = self.db.find_person_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_person_from_handle(handle,self.trans)
except KeyError: except KeyError:
person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"]))
self.family.set_mother_handle(person.get_handle()) handle = person_handle
self.family.set_mother_handle(handle)
def start_child(self,attrs): def start_child(self,attrs):
try: try:
person = self.db.find_person_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_person_from_handle(handle,self.trans)
except KeyError: except KeyError:
person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"]))
self.family.add_child_handle(person.get_handle()) handle = person_handle
self.family.add_child_handle(handle)
def start_url(self,attrs): def start_url(self,attrs):
if not attrs.has_key("href"): if not attrs.has_key("href"):
@ -859,16 +837,18 @@ class GrampsParser:
self.person.add_url(url) self.person.add_url(url)
elif self.placeobj: elif self.placeobj:
self.placeobj.add_url(url) self.placeobj.add_url(url)
elif self.repo:
self.repo.add_url(url)
def start_family(self,attrs): def start_family(self,attrs):
self.update() self.update()
handle = self.map_fid(attrs["id"]) gramps_id = self.map_fid(attrs["id"])
try: try:
self.family = self.db.find_family_from_handle( self.family = self.db.find_family_from_handle(
attrs['handle'].replace('_',''),self.trans) attrs['handle'].replace('_',''),self.trans)
self.family.set_gramps_id(handle) self.family.set_gramps_id(gramps_id)
except KeyError: except KeyError:
self.family = self.find_family_by_gramps_id(handle) self.family = self.find_family_by_gramps_id(gramps_id)
# GRAMPS LEGACY: the type now belongs to <rel> tag # GRAMPS LEGACY: the type now belongs to <rel> tag
# Here we need to support old format of <family type="Married"> # Here we need to support old format of <family type="Married">
self.family.type = _ConstXML.tuple_from_xml( self.family.type = _ConstXML.tuple_from_xml(
@ -897,24 +877,26 @@ class GrampsParser:
def start_childof(self,attrs): def start_childof(self,attrs):
try: try:
family = self.db.find_family_from_handle( handle = attrs["hlink"].replace('_','')
attrs["hlink"].replace('_',''),self.trans) self.db.check_family_from_handle(handle,self.trans)
except KeyError: except KeyError:
family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"])) family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"]))
handle = family.handle
mrel = _ConstXML.tuple_from_xml(_ConstXML.child_relations, mrel = _ConstXML.tuple_from_xml(_ConstXML.child_relations,
attrs.get('mrel','Birth')) attrs.get('mrel','Birth'))
frel = _ConstXML.tuple_from_xml(_ConstXML.child_relations, frel = _ConstXML.tuple_from_xml(_ConstXML.child_relations,
attrs.get('frel','Birth')) attrs.get('frel','Birth'))
self.person.add_parent_family_handle(family.handle,mrel,frel) self.person.add_parent_family_handle(handle,mrel,frel)
def start_parentin(self,attrs): def start_parentin(self,attrs):
try: try:
family = self.db.find_family_from_handle( handle = attrs["hlink"].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_family_from_handle(handle,self.trans)
except KeyError: except KeyError:
family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"])) family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"]))
self.person.add_family_handle(family.handle) handle = family.handle
self.person.add_family_handle(handle)
def start_name(self,attrs): def start_name(self,attrs):
if not self.in_witness: if not self.in_witness:
@ -939,13 +921,14 @@ class GrampsParser:
def start_sourceref(self,attrs): def start_sourceref(self,attrs):
self.source_ref = RelLib.SourceRef() self.source_ref = RelLib.SourceRef()
try: try:
source = self.db.find_source_from_handle( handle = attrs["hlink"].replace('_','')
attrs["hlink"].replace('_',''),self.trans) self.db.check_source_from_handle(handle,self.trans)
except KeyError: except KeyError:
source = self.find_source_by_gramps_id(self.map_sid(attrs["ref"])) source = self.find_source_by_gramps_id(self.map_sid(attrs["ref"]))
handle = source.handle
self.source_ref.ref = handle
self.source_ref.confidence = int(attrs.get("conf",self.conf)) self.source_ref.confidence = int(attrs.get("conf",self.conf))
self.source_ref.ref = source.handle
if self.photo: if self.photo:
self.photo.add_source_reference(self.source_ref) self.photo.add_source_reference(self.source_ref)
elif self.ord: elif self.ord:
@ -971,23 +954,41 @@ class GrampsParser:
def start_source(self,attrs): def start_source(self,attrs):
self.update() self.update()
handle = self.map_sid(attrs["id"]) gramps_id = self.map_sid(attrs["id"])
try: try:
self.source = self.db.find_source_from_handle( self.source = self.db.find_source_from_handle(
attrs['handle'].replace('_',''),self.trans) attrs['handle'].replace('_',''),self.trans)
self.source.set_gramps_id(handle) self.source.set_gramps_id(gramps_id)
except KeyError: except KeyError:
self.source = self.find_source_by_gramps_id(handle) self.source = self.find_source_by_gramps_id(gramps_id)
def start_reporef(self,attrs):
self.reporef = RelLib.RepoRef()
try:
handle = attrs['hlink'].replace('_','')
self.db.check_repository_from_handle(handle,self.trans)
except KeyError:
repo = self.find_repo_by_gramps_id(self.map_rid(attrs['ref']))
handle = repo.handle
self.reporef.ref = handle
self.reporef.call_number = attrs.get('callno','')
self.reporef.media_type = _ConstXML.tuple_from_xml(
_ConstXML.source_media_types,attrs.get('medium',"Unknown"))
# we count here on self.source being available
# reporefs can only be found within source
self.source.add_repo_reference(self.reporef)
def start_objref(self,attrs): def start_objref(self,attrs):
self.objref = RelLib.MediaRef() self.objref = RelLib.MediaRef()
try: try:
obj = self.db.find_object_from_handle( handle = attrs['hlink'].replace('_','')
attrs['hlink'].replace('_',''),self.trans) self.db.check_object_from_handle(handle,self.trans)
except KeyError: except KeyError:
obj = self.find_object_by_gramps_id(self.map_oid(attrs['ref'])) obj = self.find_object_by_gramps_id(self.map_oid(attrs['ref']))
handle = obj.handle
self.objref.ref = obj.handle self.objref.ref = handle
self.objref.private = bool(attrs.get('priv')) self.objref.private = bool(attrs.get('priv'))
if self.event: if self.event:
self.event.add_media_reference(self.objref) self.event.add_media_reference(self.objref)
@ -1001,13 +1002,13 @@ class GrampsParser:
self.placeobj.add_media_reference(self.objref) self.placeobj.add_media_reference(self.objref)
def start_object(self,attrs): def start_object(self,attrs):
handle = self.map_oid(attrs['id']) gramps_id = self.map_oid(attrs['id'])
try: try:
self.object = self.db.find_object_from_handle( self.object = self.db.find_object_from_handle(
attrs['handle'].replace('_',''),self.trans) attrs['handle'].replace('_',''),self.trans)
self.object.set_gramps_id(handle) self.object.set_gramps_id(gramps_id)
except KeyError: except KeyError:
self.object = self.find_object_by_gramps_id(handle) self.object = self.find_object_by_gramps_id(gramps_id)
# GRAMPS LEGACY: src, mime, and description attributes # GRAMPS LEGACY: src, mime, and description attributes
# now belong to the <file> tag. Here we are supporting # now belong to the <file> tag. Here we are supporting
@ -1021,6 +1022,15 @@ class GrampsParser:
src = os.path.dirname(fullpath) + '/' + src src = os.path.dirname(fullpath) + '/' + src
self.object.path = src self.object.path = src
def start_repo(self,attrs):
gramps_id = self.map_rid(attrs['id'])
try:
self.repo = self.db.find_repository_from_handle(
attrs['handle'].replace('_',''),self.trans)
self.repo.set_gramps_id(gramps_id)
except KeyError:
self.repo = self.find_repository_by_gramps_id(gramps_id)
def stop_people(self,*tag): def stop_people(self,*tag):
pass pass
@ -1034,6 +1044,13 @@ class GrampsParser:
def stop_objref(self,*tag): def stop_objref(self,*tag):
self.objref = None self.objref = None
def stop_repo(self,*tag):
self.db.commit_repository(self.repo,self.trans,self.change)
self.repo = None
def stop_reporef(self,*tag):
self.reporef = None
def start_photo(self,attrs): def start_photo(self,attrs):
self.photo = RelLib.MediaObject() self.photo = RelLib.MediaObject()
self.pref = RelLib.MediaRef() self.pref = RelLib.MediaRef()
@ -1253,6 +1270,10 @@ class GrampsParser:
self.attribute.set_value(tag) self.attribute.set_value(tag)
def stop_address(self,*tag): def stop_address(self,*tag):
if self.person:
self.person.add_address(self.address)
elif self.repo:
self.repo.add_address(self.address)
self.address = None self.address = None
def stop_places(self,*tag): def stop_places(self,*tag):
@ -1278,12 +1299,15 @@ class GrampsParser:
def stop_family(self,*tag): def stop_family(self,*tag):
self.db.commit_family(self.family,self.trans,self.change) self.db.commit_family(self.family,self.trans,self.change)
self.family = None self.family = None
while gtk.events_pending():
gtk.main_iteration()
def stop_type(self,tag): def stop_type(self,tag):
# Event type if self.event:
self.event.type = _ConstXML.tuple_from_xml(_ConstXML.events,tag) # Event type
self.event.type = _ConstXML.tuple_from_xml(_ConstXML.events,tag)
elif self.repo:
# Repository type
self.repo.type = _ConstXML.tuple_from_xml(
_ConstXML.repository_types,tag)
def stop_eventref(self,tag): def stop_eventref(self,tag):
self.eventref = None self.eventref = None
@ -1310,8 +1334,8 @@ class GrampsParser:
self.event = None self.event = None
def stop_name(self,tag): def stop_name(self,tag):
# Parse witnesses created by older gramps
if self.in_witness: if self.in_witness:
# Parse witnesses created by older gramps
note_text = self.event.get_note() + "\n" + \ note_text = self.event.get_note() + "\n" + \
_("Witness name: %s") % tag _("Witness name: %s") % tag
self.event.set_note(note_text) self.event.set_note(note_text)
@ -1327,6 +1351,10 @@ class GrampsParser:
self.person.get_primary_name().build_sort_name() self.person.get_primary_name().build_sort_name()
self.name = None self.name = None
def stop_rname(self,tag):
# Repository name
self.repo.name = tag
def stop_ref(self,tag): def stop_ref(self,tag):
# Parse witnesses created by older gramps # Parse witnesses created by older gramps
person = self.find_person_by_gramps_id(self.map_gid(tag)) person = self.find_person_by_gramps_id(self.map_gid(tag))
@ -1353,8 +1381,6 @@ class GrampsParser:
self.event.set_place_handle(self.placeobj.get_handle()) self.event.set_place_handle(self.placeobj.get_handle())
self.db.commit_place(self.placeobj,self.trans,self.change) self.db.commit_place(self.placeobj,self.trans,self.change)
self.placeobj = None self.placeobj = None
while gtk.events_pending():
gtk.main_iteration()
def stop_date(self,tag): def stop_date(self,tag):
if tag: if tag:
@ -1372,8 +1398,6 @@ class GrampsParser:
def stop_person(self,*tag): def stop_person(self,*tag):
self.db.commit_person(self.person,self.trans,self.change) self.db.commit_person(self.person,self.trans,self.change)
self.person = None self.person = None
while gtk.events_pending():
gtk.main_iteration()
def stop_description(self,tag): def stop_description(self,tag):
self.event.description = tag self.event.description = tag
@ -1391,7 +1415,7 @@ class GrampsParser:
self.person.set_gender (RelLib.Person.UNKNOWN) self.person.set_gender (RelLib.Person.UNKNOWN)
def stop_stitle(self,tag): def stop_stitle(self,tag):
self.source.set_title(tag) self.source.title = tag
def stop_sourceref(self,*tag): def stop_sourceref(self,*tag):
self.source_ref = None self.source_ref = None
@ -1401,22 +1425,22 @@ class GrampsParser:
self.source = None self.source = None
def stop_sauthor(self,tag): def stop_sauthor(self,tag):
self.source.set_author(tag) self.source.author = tag
def stop_phone(self,tag): def stop_phone(self,tag):
self.address.set_phone(tag) self.address.phone = tag
def stop_street(self,tag): def stop_street(self,tag):
self.address.set_street(tag) self.address.street = tag
def stop_city(self,tag): def stop_city(self,tag):
self.address.set_city(tag) self.address.city = tag
def stop_state(self,tag): def stop_state(self,tag):
self.address.set_state(tag) self.address.state = tag
def stop_country(self,tag): def stop_country(self,tag):
self.address.set_country(tag) self.address.country = tag
def stop_postal(self,tag): def stop_postal(self,tag):
self.address.set_postal_code(tag) self.address.set_postal_code(tag)
@ -1504,6 +1528,10 @@ class GrampsParser:
self.placeobj.set_note_object(self.note) self.placeobj.set_note_object(self.note)
elif self.eventref: elif self.eventref:
self.eventref.set_note_object(self.note) self.eventref.set_note_object(self.note)
elif self.repo:
self.repo.set_note_object(self.note)
elif self.reporef:
self.reporef.set_note_object(self.note)
def stop_research(self,tag): def stop_research(self,tag):
self.owner.set(self.resname, self.resaddr, self.rescity, self.resstate, self.owner.set(self.resname, self.resaddr, self.rescity, self.resstate,
@ -1587,22 +1615,64 @@ def append_value(orig,val):
def build_place_title(loc): def build_place_title(loc):
"Builds a title from a location" "Builds a title from a location"
city = loc.get_city()
state = loc.get_state()
country = loc.get_country()
county = loc.get_county()
parish = loc.get_parish()
value = "" value = ""
if loc.parish:
if parish: value = loc.parish
value = parish if loc.city:
if city: value = append_value(value,loc.city)
value = append_value(value,city) if loc.county:
if county: value = append_value(value,loc.county)
value = append_value(value,county) if loc.state:
if state: value = append_value(value,loc.state)
value = append_value(value,state) if loc.country:
if country: value = append_value(value,loc.country)
value = append_value(value,country)
return value return value
if __name__ == "__main__":
import sys
import hotshot#, hotshot.stats
import const
from GrampsDb import gramps_db_factory, gramps_db_reader_factory
def callback(val):
print val
codeset = None
db_class = gramps_db_factory(const.app_gramps)
database = db_class()
database.load("test.grdb",lambda x: None, mode="w")
filename = os.path.normpath(sys.argv[1])
basefile = os.path.dirname(filename)
change = os.path.getmtime(filename)
parser = GrampsParser(database,callback,basefile,change,filename)
print "starting line parser"
linecounter = LineParser(filename)
print "finished parser"
lc = linecounter.get_count()
print "got line count:", lc
xml_file = gzip.open(filename,"rb")
if True:
pr = hotshot.Profile('mystats.profile')
print "Start"
pr.runcall(parser.parse,xml_file,False,lc)
print "Finished"
pr.close()
## print "Loading profile"
## stats = hotshot.stats.load('mystats.profile')
## print "done"
## stats.strip_dirs()
## stats.sort_stats('time','calls')
## stats.print_stats(100)
else:
import time
t = time.time()
parser.parse(xml_file,False,lc)
print time.time() - t
xml_file.close()