* src/plugins/WritePkg.py (PackageWriter.export): Add existing

image filenames to the archive.
* src/GrampsDb/_WriteXML.py: Improve callback use, add another
strip_photo option.
* src/ViewManager.py: Typos.
* src/Exporter.py: Start for callback support.
* src/GrampsDb/_WriteGedcom.py: Callback support.


svn: r6013
This commit is contained in:
Alex Roitman 2006-03-01 05:04:22 +00:00
parent e3397ecf96
commit 2dd4571b39
6 changed files with 71 additions and 62 deletions

View File

@ -1,4 +1,12 @@
2006-02-28 Alex Roitman <shura@gramps-project.org> 2006-02-28 Alex Roitman <shura@gramps-project.org>
* src/plugins/WritePkg.py (PackageWriter.export): Add existing
image filenames to the archive.
* src/GrampsDb/_WriteXML.py: Improve callback use, add another
strip_photo option.
* src/ViewManager.py: Typos.
* src/Exporter.py: Start for callback support.
* src/GrampsDb/_WriteGedcom.py: Callback support.
* src/ViewManager.py (ViewManager.import_data): Properly process * src/ViewManager.py (ViewManager.import_data): Properly process
non-native (plugin) imports; (_do_import): factor common code into non-native (plugin) imports; (_do_import): factor common code into
the new method. the new method.

View File

@ -67,7 +67,7 @@ class Exporter:
name. name.
""" """
def __init__(self,dbstate,uistate): def __init__(self,dbstate,uistate,callback=None):
""" """
Set up the window, the druid, and build all the druid's pages. Set up the window, the druid, and build all the druid's pages.
Some page elements are left empty, since their contents depends Some page elements are left empty, since their contents depends
@ -75,6 +75,7 @@ class Exporter:
""" """
self.dbstate = dbstate self.dbstate = dbstate
self.uistate = uistate self.uistate = uistate
self.callback = callback
if self.dbstate.active: if self.dbstate.active:
self.person = self.dbstate.active self.person = self.dbstate.active
else: else:
@ -165,10 +166,12 @@ class Exporter:
if self.exports[ix][3]: if self.exports[ix][3]:
success = self.exports[ix][0](self.dbstate.db, success = self.exports[ix][0](self.dbstate.db,
filename,self.person, filename,self.person,
self.option_box_instance) self.option_box_instance,
self.callback)
else: else:
success = self.exports[ix][0](self.dbstate.db, success = self.exports[ix][0](self.dbstate.db,
filename,self.person) filename,self.person,
self.callback)
return success return success
def build_conclusion(self,success): def build_conclusion(self,success):

View File

@ -1318,7 +1318,7 @@ class GedcomWriter:
# #
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def exportData(database,filename,person,option_box): def exportData(database,filename,person,option_box,callback=None):
ret = 0 ret = 0
try: try:
gw = GedcomWriter(database,person,0,filename,option_box) gw = GedcomWriter(database,person,0,filename,option_box)

View File

@ -131,12 +131,19 @@ class XmlWriter:
db - database to write db - database to write
callback - function to provide progress indication callback - function to provide progress indication
strip_photos - remove full paths off of media object paths strip_photos - remove paths off of media object paths
0: do not touch the paths
1: remove everything expect the filename
2: remove leading slash
compress - attempt to compress the database compress - attempt to compress the database
""" """
self.compress = compress self.compress = compress
self.db = db self.db = db
self.callback = callback self.callback = callback
if '__call__' in dir(self.callback): # callback is really callable
self.update = self.update_real
else:
self.update = self.update_empty
self.strip_photos = strip_photos self.strip_photos = strip_photos
def write(self,filename): def write(self,filename):
@ -212,8 +219,10 @@ class XmlWriter:
repo_len = len(self.db.get_repository_handles()) repo_len = len(self.db.get_repository_handles())
obj_len = len(self.db.get_media_object_handles()) obj_len = len(self.db.get_media_object_handles())
total = person_len + family_len + event_len + place_len + \ self.total = person_len + family_len + event_len + place_len + \
source_len + obj_len + repo_len source_len + obj_len + repo_len
self.count = 0
self.oldval = 0
self.g.write('<?xml version="1.0" encoding="UTF-8"?>\n') self.g.write('<?xml version="1.0" encoding="UTF-8"?>\n')
self.g.write('<!DOCTYPE database ' self.g.write('<!DOCTYPE database '
@ -239,9 +248,6 @@ class XmlWriter:
self.g.write(" </researcher>\n") self.g.write(" </researcher>\n")
self.g.write(" </header>\n") self.g.write(" </header>\n")
count = 0
delta = max(int(total/50),1)
if event_len > 0: if event_len > 0:
self.g.write(" <events>\n") self.g.write(" <events>\n")
sorted_keys = self.db.get_gramps_ids(EVENT_KEY) sorted_keys = self.db.get_gramps_ids(EVENT_KEY)
@ -249,9 +255,7 @@ class XmlWriter:
for gramps_id in sorted_keys: for gramps_id in sorted_keys:
event = self.db.get_event_from_gramps_id(gramps_id) event = self.db.get_event_from_gramps_id(gramps_id)
self.write_event(event,2) self.write_event(event,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
count = count + 1
self.g.write(" </events>\n") self.g.write(" </events>\n")
if person_len > 0: if person_len > 0:
@ -268,9 +272,7 @@ class XmlWriter:
for gramps_id in sorted_keys: for gramps_id in sorted_keys:
person = self.db.get_person_from_gramps_id(gramps_id) person = self.db.get_person_from_gramps_id(gramps_id)
self.write_person(person,2) self.write_person(person,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
count += 1
self.g.write(" </people>\n") self.g.write(" </people>\n")
if family_len > 0: if family_len > 0:
@ -280,9 +282,7 @@ class XmlWriter:
for gramps_id in sorted_keys: for gramps_id in sorted_keys:
family = self.db.get_family_from_gramps_id(gramps_id) family = self.db.get_family_from_gramps_id(gramps_id)
self.write_family(family,2) self.write_family(family,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
count = count + 1
self.g.write(" </families>\n") self.g.write(" </families>\n")
if source_len > 0: if source_len > 0:
@ -292,9 +292,6 @@ class XmlWriter:
for key in keys: for key in keys:
source = self.db.get_source_from_gramps_id(key) source = self.db.get_source_from_gramps_id(key)
self.write_source(source,2) self.write_source(source,2)
if self.callback and count % delta == 0:
self.callback(float(count)/float(total))
count = count + 1
self.g.write(" </sources>\n") self.g.write(" </sources>\n")
if place_len > 0: if place_len > 0:
@ -305,11 +302,7 @@ class XmlWriter:
# try: # try:
place = self.db.get_place_from_gramps_id(key) place = self.db.get_place_from_gramps_id(key)
self.write_place_obj(place,2) self.write_place_obj(place,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
# except:
# print "Could not find place %s" % key
count = count + 1
self.g.write(" </places>\n") self.g.write(" </places>\n")
if obj_len > 0: if obj_len > 0:
@ -319,9 +312,7 @@ class XmlWriter:
for gramps_id in sorted_keys: for gramps_id in sorted_keys:
obj = self.db.get_object_from_gramps_id(gramps_id) obj = self.db.get_object_from_gramps_id(gramps_id)
self.write_object(obj,2) self.write_object(obj,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
count += 1
self.g.write(" </objects>\n") self.g.write(" </objects>\n")
if repo_len > 0: if repo_len > 0:
@ -331,9 +322,7 @@ class XmlWriter:
for key in keys: for key in keys:
repo = self.db.get_repository_from_gramps_id(key) repo = self.db.get_repository_from_gramps_id(key)
self.write_repository(repo,2) self.write_repository(repo,2)
if self.callback and count % delta == 0: self.update()
self.callback(float(count)/float(total))
count += 1
self.g.write(" </repositories>\n") self.g.write(" </repositories>\n")
if len(self.db.get_bookmarks()) > 0: if len(self.db.get_bookmarks()) > 0:
@ -351,6 +340,16 @@ class XmlWriter:
self.g.write("</database>\n") self.g.write("</database>\n")
def update_empty(self):
pass
def update_real(self):
self.count += 1
newval = int(100*self.count/self.total)
if newval != self.oldval:
self.callback(newval)
self.oldval = newval
def fix(self,line): def fix(self,line):
l = line.strip() l = line.strip()
l = l.replace('&','&amp;') l = l.replace('&','&amp;')
@ -950,8 +949,11 @@ class XmlWriter:
desc_text = ' description="%s"' % self.fix(desc) desc_text = ' description="%s"' % self.fix(desc)
else: else:
desc_text = '' desc_text = ''
if self.strip_photos: if self.strip_photos == 1:
path = os.path.basename(path) path = os.path.basename(path)
elif self.strip_photos == 2 and path[0] == '/':
path = path[1:]
self.g.write('%s<file src="%s" mime="%s"%s/>\n' self.g.write('%s<file src="%s" mime="%s"%s/>\n'
% (" "*(index+1),path,mime_type,desc_text)) % (" "*(index+1),path,mime_type,desc_text))
self.write_attribute_list(obj.get_attribute_list()) self.write_attribute_list(obj.get_attribute_list())

View File

@ -870,7 +870,7 @@ class ViewManager:
def export_data(self,obj): def export_data(self,obj):
import Exporter import Exporter
Exporter.Exporter(self.state,self.uistate) Exporter.Exporter(self.state,self.uistate,self.pulse_progressbar)
def import_data(self,obj): def import_data(self,obj):
choose = gtk.FileChooserDialog(_('GRAMPS: Import database'), choose = gtk.FileChooserDialog(_('GRAMPS: Import database'),
@ -930,9 +930,12 @@ class ViewManager:
return False return False
# First we try our best formats # First we try our best formats
if filetype in format_list: if filetype in (const.app_gramps,
const.app_gramps_xml,
const.app_gedcom):
self._do_import(choose, self._do_import(choose,
GrampsDb.gramps_db_reader_factory(filetype)) GrampsDb.gramps_db_reader_factory(filetype),
filename)
return True return True
# Then we try all the known plugins # Then we try all the known plugins
@ -941,7 +944,7 @@ class ViewManager:
for (importData,mime_filter,mime_type, for (importData,mime_filter,mime_type,
native_format,format_name) in PluginMgr.import_list: native_format,format_name) in PluginMgr.import_list:
if filetype == mime_type or the_file == mime_type: if filetype == mime_type or the_file == mime_type:
self._do_import(choose,importData) self._do_import(choose,importData,filename)
return True return True
# Finally, we give up and declare this an unknown format # Finally, we give up and declare this an unknown format
@ -954,12 +957,11 @@ class ViewManager:
choose.destroy() choose.destroy()
return False return False
def _do_import(self,dialog,importer): def _do_import(self,dialog,importer,filename):
dialog.destroy() dialog.destroy()
self.window.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) self.window.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
self.progress.show() self.progress.show()
GrampsDb.gramps_db_reader_factory(filetype)( importer(self.state.db,filename,self.pulse_progressbar)
self.state.db,filename,self.pulse_progressbar)
self.uistate.clear_history() self.uistate.clear_history()
self.progress.hide() self.progress.hide()
self.window.window.set_cursor(None) self.window.window.set_cursor(None)

View File

@ -140,9 +140,7 @@ class PackageWriter:
def fs_ok_clicked(obj): def fs_ok_clicked(obj):
name = fs_top.get_filename() name = fs_top.get_filename()
if os.path.isfile(name): if os.path.isfile(name):
ti = tf.gettarinfo(name,base) archive.add(name)
ti.mtime = time.time()
tf.addfile(ti)
fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file")) fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file"))
fs_top.hide_fileop_buttons() fs_top.hide_fileop_buttons()
@ -158,21 +156,21 @@ class PackageWriter:
# during the process (i.e. when removing object) # during the process (i.e. when removing object)
for m_id in self.db.get_media_object_handles(): for m_id in self.db.get_media_object_handles():
mobject = self.db.get_object_from_handle(m_id) mobject = self.db.get_object_from_handle(m_id)
oldfile = mobject.get_path() filename = mobject.get_path()
base = os.path.basename(oldfile) if os.path.isfile(filename):
if os.path.isfile(oldfile): archive.add(filename)
tarinfo = archive.gettarinfo(oldfile,base)
tarinfo.mtime = int(time.time())
archive.addfile(tarinfo,file(oldfile))
else: else:
# File is lost => ask what to do # File is lost => ask what to do
if missmedia_action == 0: if missmedia_action == 0:
mmd = MissingMediaDialog(_("Media object could not be found"), mmd = MissingMediaDialog(
_("%(file_name)s is referenced in the database, but no longer exists. " _("Media object could not be found"),
"The file may have been deleted or moved to a different location. " _("%(file_name)s is referenced in the database, "
"You may choose to either remove the reference from the database, " "but no longer exists. The file may have been "
"keep the reference to the missing file, or select a new file." "deleted or moved to a different location. "
) % { 'file_name' : oldfile }, "You may choose to either remove the reference "
"from the database, keep the reference to the "
"missing file, or select a new file."
) % { 'file_name' : filename },
remove_clicked, leave_clicked, select_clicked) remove_clicked, leave_clicked, select_clicked)
missmedia_action = mmd.default_action missmedia_action = mmd.default_action
elif missmedia_action == 1: elif missmedia_action == 1:
@ -183,13 +181,9 @@ class PackageWriter:
select_clicked() select_clicked()
# Write XML now # Write XML now
# import tempfile
# tempdir = tempfile.mkdtemp('.tmp','gramps-',
# os.path.dirname(self.filename))
# new_xml_file = os.path.join(tempdir,'data.gramps')
g = StringIO() g = StringIO()
gfile = XmlWriter(self.db,None,1) gfile = XmlWriter(self.db,None,2)
gfile.write_handle(g) #write(new_xml_file) #write_handle(g) gfile.write_handle(g)
tarinfo = tarfile.TarInfo('data.gramps') tarinfo = tarfile.TarInfo('data.gramps')
tarinfo.size = len(g.getvalue()) tarinfo.size = len(g.getvalue())
tarinfo.mtime = time.time() tarinfo.mtime = time.time()