* src/GrampsDb/_GrampsBSDDB.py (load_from): Add method.

* src/GrampsDb/_GrampsXMLDB.py (load_from): Add method.
	* src/GrampsDb/_GrampsGEDDB.py (load_from): Add method.
	* src/GrampsDb/_GrampsDbBase.py (load_from): Add method.
	* src/GrampsDb/_WriteGrdb.py: Use db_copy.
	* src/GrampsDb/_DbUtils.py (db_copy): Add function.
	* src/ViewManager.py: Save as support.
	* src/DataViews/_MapView.py (enable_debug): Set to False to be
	able to see the output besides MapView.



svn: r6604
This commit is contained in:
Alex Roitman
2006-05-10 05:52:55 +00:00
parent 626185cff8
commit 40f355e17d
9 changed files with 281 additions and 88 deletions

View File

@@ -143,3 +143,95 @@ def add_child_to_family(db, family, child,
if need_commit:
db.transaction_commit(trans, _('Add child to family') )
def db_copy(from_db,to_db,callback):
if '__call__' in dir(callback): # callback is really callable
update = update_real
# Prepare length for the callback
person_len = from_db.get_number_of_people()
family_len = from_db.get_number_of_families()
event_len = from_db.get_number_of_events()
source_len = from_db.get_number_of_sources()
place_len = from_db.get_number_of_places()
repo_len = from_db.get_number_of_repositories()
obj_len = from_db.get_number_of_media_objects()
total = person_len + family_len + event_len + place_len + \
source_len + obj_len + repo_len
else:
update = update_empty
total = 0
primary_tables = {
'Person': {'cursor_func': from_db.get_person_cursor,
'table': to_db.person_map },
'Family': {'cursor_func': from_db.get_family_cursor,
'table': to_db.family_map },
'Event': {'cursor_func': from_db.get_event_cursor,
'table': to_db.event_map },
'Place': {'cursor_func': from_db.get_place_cursor,
'table': to_db.place_map },
'Source': {'cursor_func': from_db.get_source_cursor,
'table': to_db.source_map },
'MediaObject': {'cursor_func': from_db.get_media_cursor,
'table': to_db.media_map },
'Repository': {'cursor_func': from_db.get_repository_cursor,
'table': to_db.repository_map },
}
if to_db.__class__.__name__ == 'GrampsBSDDB':
if to_db.UseTXN:
add_data = add_data_txn
else:
add_data = add_data_notxn
else:
add_data = add_data_dict
oldval = 0
count = 0
for table_name in primary_tables.keys():
cursor_func = primary_tables[table_name]['cursor_func']
table = primary_tables[table_name]['table']
cursor = cursor_func()
item = cursor.first()
while item:
(handle,data) = item
add_data(to_db,table,handle,data)
item = cursor.next()
count,oldval = update(callback,count,oldval,total)
update(callback,count,oldval,total)
cursor.close()
# The metadata is always transactionless,
# and the table is small, so using key iteration is OK here.
for handle in from_db.metadata.keys():
data = from_db.metadata.get(handle)
to_db.metadata[handle] = data
def add_data_txn(db,table,handle,data):
the_txn = db.env.txn_begin()
table.put(handle,data,txn=the_txn)
the_txn.commit()
def add_data_notxn(db,table,handle,data):
table.put(handle,data)
def add_data_dict(db,table,handle,data):
table[handle] = data
def update_empty(callback,count,oldval,total):
pass
def update_real(callback,count,oldval,total):
count += 1
newval = int(100.0*count/total)
if newval != oldval:
callback(newval)
oldval = newval
return count,oldval

View File

@@ -53,6 +53,7 @@ except NameError:
#-------------------------------------------------------------------------
from RelLib import *
from _GrampsDbBase import *
from _DbUtils import db_copy
import const
_MINVERSION = 5
@@ -316,15 +317,14 @@ class GrampsBSDDB(GrampsDbBase):
else:
env_flags = db.DB_CREATE|db.DB_PRIVATE|\
db.DB_INIT_MPOOL|db.DB_INIT_LOG
env_name = self.brief_name
env_name = os.path.expanduser('~')
self.env.open(env_name,env_flags)
if self.UseTXN:
self.env.txn_checkpoint()
callback(25)
self.metadata =self.open_table(self.full_name,"meta",no_txn=True)
self.metadata = self.open_table(self.full_name,"meta",no_txn=True)
self.family_map = self.open_table(self.full_name, "family")
self.place_map = self.open_table(self.full_name, "places")
@@ -386,6 +386,11 @@ class GrampsBSDDB(GrampsDbBase):
return 1
def load_from(self, other_database, filename, callback):
self.load(filename,callback)
db_copy(other_database,self,callback)
return 1
def connect_secondary(self):
"""
This method connects or creates secondary index tables.

View File

@@ -356,6 +356,14 @@ class GrampsDbBase(GrampsDBCallback):
"""
assert False, "Needs to be overridden in the derived class"
def load_from(self, other_database, filename, callback):
"""
Loads data from the other database into itself.
The filename is the name of the file for the newly created database.
The method needs to be overridden in the derived class.
"""
assert False, "Needs to be overridden in the derived class"
def close(self):
"""
Closes the specified database. The method needs to be overridden

View File

@@ -30,6 +30,7 @@ from _GrampsInMemDB import *
import _ReadGedcom as ReadGedcom
import _WriteGedcom as WriteGedcom
from _DbUtils import db_copy
#-------------------------------------------------------------------------
#
@@ -56,6 +57,17 @@ class GrampsGEDDB(GrampsInMemDB):
self.db_is_open = True
return 1
def load_from(self, other_database, filename, callback):
db_copy(other_database,self,callback)
GrampsInMemDB.load(self,filename,callback)
self.bookmarks = self.metadata.get('bookmarks')
if self.bookmarks == None:
self.bookmarks = []
self.db_is_open = True
writer = WriteGedcom.GedcomWriter(self,self.get_default_person())
writer.export_data(self.full_name)
return 1
def close(self):
if not self.db_is_open:
return

View File

@@ -30,6 +30,7 @@ from _GrampsInMemDB import *
import _ReadXML as ReadXML
import _WriteXML as WriteXML
from _DbUtils import db_copy
#-------------------------------------------------------------------------
#
@@ -58,6 +59,17 @@ class GrampsXMLDB(GrampsInMemDB):
self.db_is_open = True
return 1
def load_from(self, other_database, filename, callback):
self.id_trans = {}
db_copy(other_database,self,callback)
GrampsInMemDB.load(self,filename,callback)
self.bookmarks = self.metadata.get('bookmarks')
if self.bookmarks == None:
self.bookmarks = []
self.db_is_open = True
WriteXML.quick_write(self,self.full_name)
return 1
def close(self):
if not self.db_is_open:
return

View File

@@ -37,6 +37,7 @@ from gettext import gettext as _
#-------------------------------------------------------------------------
from _GrampsBSDDB import GrampsBSDDB
from QuestionDialog import ErrorDialog
from _DbUtils import db_copy
#-------------------------------------------------------------------------
#
@@ -44,25 +45,7 @@ from QuestionDialog import ErrorDialog
#
#-------------------------------------------------------------------------
def exportData(database, filename, person=None, callback=None, cl=False):
if '__call__' in dir(callback): # callback is really callable
update = update_real
# Prepare length for the callback
person_len = database.get_number_of_people()
family_len = database.get_number_of_families()
event_len = database.get_number_of_events()
source_len = database.get_number_of_sources()
place_len = database.get_number_of_places()
repo_len = database.get_number_of_repositories()
obj_len = database.get_number_of_media_objects()
total = person_len + family_len + event_len + place_len + \
source_len + obj_len + repo_len
else:
update = update_empty
total = 0
filename = os.path.normpath(filename)
new_database = GrampsBSDDB()
try:
@@ -75,69 +58,6 @@ def exportData(database, filename, person=None, callback=None, cl=False):
return
# copy all data from new_database to database
# Need different adders depending on whether the new db is transactional
if new_database.UseTXN:
add_data = add_data_txn
else:
add_data = add_data_notxn
primary_tables = {
'Person': {'cursor_func': database.get_person_cursor,
'new_table': new_database.person_map },
'Family': {'cursor_func': database.get_family_cursor,
'new_table': new_database.family_map },
'Event': {'cursor_func': database.get_event_cursor,
'new_table': new_database.event_map },
'Place': {'cursor_func': database.get_place_cursor,
'new_table': new_database.place_map },
'Source': {'cursor_func': database.get_source_cursor,
'new_table': new_database.source_map },
'MediaObject': {'cursor_func': database.get_media_cursor,
'new_table': new_database.media_map },
'Repository': {'cursor_func': database.get_repository_cursor,
'new_table': new_database.repository_map },
}
count = 0
oldval = 0
for table_name in primary_tables.keys():
cursor_func = primary_tables[table_name]['cursor_func']
new_table = primary_tables[table_name]['new_table']
cursor = cursor_func()
item = cursor.first()
while item:
(handle,data) = item
add_data(new_database,new_table,handle,data)
item = cursor.next()
count,oldval = update(callback,count,oldval,total)
cursor.close()
# The metadata is always transactionless,
# and the table is small, so using key iteration is OK here.
for handle in database.metadata.keys():
new_database.metadata.put(handle,database.metadata.get(handle))
db_copy(database,new_database,callback)
new_database.close()
def add_data_txn(db,table,handle,data):
the_txn = db.env.txn_begin()
table.put(handle,data,txn=the_txn)
the_txn.commit()
def add_data_notxn(db,table,handle,data):
table.put(handle,data)
def update_empty(callback,count,oldval,total):
pass
def update_real(callback,count,oldval,total):
count += 1
newval = int(100.0*count/total)
if newval != oldval:
callback(newval)
oldval = newval
return count,oldval