* src/gen/db/exceptions.py: wrong exception def, bug #1593

* src/GrampsDbUtils/_WriteXML.py: correctly catch the exception
	* src/GrampsDbUtils/_GrampsDbWriteXML.py: remove double def of xml export
	* src/GrampsDbUtils/_GrampsDbWRFactories.py: remove write backend factory
	* src/ExportAssistant.py: on fail, do not set success = True
	* src/GrampsDbUtils/__init__.py: don't export unused stuff

2008-01-15  Benny Malengier <benny.malengier@gramps-project.org>


svn: r9818
This commit is contained in:
Benny Malengier 2008-01-15 10:47:25 +00:00
parent 7a1640ae88
commit ccb0b85b15
7 changed files with 80 additions and 106 deletions

View File

@ -6,6 +6,14 @@
* man/sv/gramps.1.in: tried to add Swedish man page (sv). * man/sv/gramps.1.in: tried to add Swedish man page (sv).
but failed. How do I create man/sv in the svn repository? but failed. How do I create man/sv in the svn repository?
2008-01-15 Benny Malengier <benny.malengier@gramps-project.org>
* src/gen/db/exceptions.py: wrong exception def, bug #1593
* src/GrampsDbUtils/_WriteXML.py: correctly catch the exception
* src/GrampsDbUtils/_GrampsDbWriteXML.py: remove double def of xml export
* src/GrampsDbUtils/_GrampsDbWRFactories.py: remove write backend factory
* src/ExportAssistant.py: on fail, do not set success = True
* src/GrampsDbUtils/__init__.py: don't export unused stuff
2008-01-15 Benny Malengier <benny.malengier@gramps-project.org> 2008-01-15 Benny Malengier <benny.malengier@gramps-project.org>
* src/ArgHandler.py: on autoload, do some extra checks first. * src/ArgHandler.py: on autoload, do some extra checks first.

View File

@ -430,13 +430,12 @@ class ExportAssistant(gtk.Assistant, ManagedWindow.ManagedWindow) :
# Lock page, show progress bar # Lock page, show progress bar
self.pre_save(page) self.pre_save(page)
# save # save
self.save() success = self.save()
# Unlock page # Unlock page
self.post_save() self.post_save()
#update the label and title #update the label and title
success = True if success is None or success:
if success:
conclusion_title = _('Your data has been saved') conclusion_title = _('Your data has been saved')
conclusion_text = _( conclusion_text = _(
'The copy of your data has been ' 'The copy of your data has been '
@ -449,7 +448,7 @@ class ExportAssistant(gtk.Assistant, ManagedWindow.ManagedWindow) :
#add test, what is dir #add test, what is dir
conclusion_text += '\n\n' + 'Filename: %s' %self.chooser.get_filename() conclusion_text += '\n\n' + 'Filename: %s' %self.chooser.get_filename()
else: else:
conclusion_title = _('Saving failed'), conclusion_title = _('Saving failed')
conclusion_text = _( conclusion_text = _(
'There was an error while saving your data. ' 'There was an error while saving your data. '
'You may try starting the export again.\n\n' 'You may try starting the export again.\n\n'

View File

@ -33,41 +33,18 @@ required e.g.:
> >
> # To get a Gedcom reader > # To get a Gedcom reader
> GrampsDb.gramps_db_reader_factory(db_type = const.APP_GEDCOM) > GrampsDb.gramps_db_reader_factory(db_type = const.APP_GEDCOM)
As of 3.0 the writer_factory is deprecated. Exporter uses directly the plugin
system for export, no factory needed.
""" """
import const import const
import logging import logging
log = logging.getLogger(".GrampDb") log = logging.getLogger(".GrampDb")
from gen.db import GrampsDbException from gen.db import GrampsDbException
from PluginUtils import import_list from PluginUtils import import_list
def gramps_db_writer_factory(db_type):
"""Factory class for obtaining a Gramps database writers.
@param db_type: the type of backend required.
@type db_type: one of the app_* constants in const.py
Raises GrampsDbException if the db_type is not recognised.
"""
if db_type == const.APP_GRAMPS_XML:
import _WriteXML as WriteXML
md = WriteXML.exportData
elif db_type == const.APP_GEDCOM:
import _WriteGedcom as WriteGedcom
md = WriteGedcom.exportData
else:
raise GrampsDbException("Attempt to create a database "
"writer for unknown format: "
"db_type = %s" % (str(db_type),))
return md
def gramps_db_reader_factory(db_type): def gramps_db_reader_factory(db_type):
"""Factory class for obtaining a Gramps database importers. """Factory class for obtaining a Gramps database importers.
@ -97,7 +74,7 @@ def gramps_db_reader_factory(db_type):
if not found: if not found:
raise GrampsDbException("Attempt to create a database " raise GrampsDbException("Attempt to create a database "
"reader for unknown format: " "reader for unknown format: "
"db_type = %s" % (str(db_type),)) "db_type = %s" % (str(db_type)))
return md return md

View File

@ -23,6 +23,8 @@
""" """
Contains the interface to allow a database to get written using Contains the interface to allow a database to get written using
GRAMPS' XML file format. GRAMPS' XML file format.
This module contains all that is needed for xml write, however it does not
provide the export plugin functionality. That is provided in _WriteXML.py
""" """
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -31,7 +33,6 @@ GRAMPS' XML file format.
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import time import time
import shutil
import os import os
import codecs import codecs
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
@ -61,63 +62,14 @@ from BasicUtils import UpdateCallback
from gen.db.exceptions import GrampsDbWriteFailure from gen.db.exceptions import GrampsDbWriteFailure
#from gen.utils.longop import LongOpStatus #from gen.utils.longop import LongOpStatus
import gen.proxy
#-------------------------------------------------------------------------
#
# Attempt to load the GZIP library. Some version of python do not seem
# to be compiled with this available.
#
#-------------------------------------------------------------------------
try:
import gzip
_gzip_ok = 1
except:
_gzip_ok = 0
_xml_version = "1.2.0" _xml_version = "1.2.0"
# table for skipping control chars from XML # table for skipping control chars from XML
strip_dict = dict.fromkeys(range(9)+range(12,20)) strip_dict = dict.fromkeys(range(9)+range(12,20))
def escxml(d): def escxml(d):
return escape(d, { '"' : '&quot;' } ) return escape(d, { '"' : '&quot;' } )
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def exportData(database, filename, person, option_box, callback, version="unknown"):
if os.path.isfile(filename):
try:
shutil.copyfile(filename, filename + ".bak")
shutil.copystat(filename, filename + ".bak")
except:
pass
compress = _gzip_ok == 1
option_box.parse_options()
restrict = option_box.restrict
private = option_box.private
if private:
database = gen.proxy.PrivateProxyDb(database)
if restrict:
database = gen.proxy.LivingProxyDb(
database, gen.proxy.LivingProxyDb.MODE_RESTRICT)
if not option_box.cfilter.is_empty():
database = gen.proxy.FilterProxyDb(database, option_box.cfilter)
g = GrampsDbXmlWriter(database, 0, compress, version, callback)
return g.write(filename)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# #
@ -164,8 +116,8 @@ class GrampsDbXmlWriter(UpdateCallback):
base = os.path.dirname(filename) base = os.path.dirname(filename)
if os.path.isdir(base): if os.path.isdir(base):
if not os.access(base,os.W_OK) or not os.access(base,os.R_OK): if not os.access(base,os.W_OK) or not os.access(base,os.R_OK):
raise GrampsDbWriteFailure,\ raise GrampsDbWriteFailure(
(_('Failure writing %s') % filename, _('Failure writing %s') % filename,
_("The database cannot be saved because you do " _("The database cannot be saved because you do "
"not have permission to write to the directory. " "not have permission to write to the directory. "
"Please make sure you have write access to the " "Please make sure you have write access to the "
@ -174,9 +126,9 @@ class GrampsDbXmlWriter(UpdateCallback):
if os.path.exists(filename): if os.path.exists(filename):
if not os.access(filename,os.W_OK): if not os.access(filename,os.W_OK):
raise GrampsDbWriteFailure, \ raise GrampsDbWriteFailure(
(_('Failure writing %s') % filename, _('Failure writing %s') % filename,
_("The database cannot be saved because you do " _("The database cannot be saved because you do "
"not have permission to write to the file. " "not have permission to write to the file. "
"Please make sure you have write access to the " "Please make sure you have write access to the "
"file and try again.")) "file and try again."))
@ -193,7 +145,8 @@ class GrampsDbXmlWriter(UpdateCallback):
g = open(filename,"w") g = open(filename,"w")
except IOError,msg: except IOError,msg:
print str(msg) print str(msg)
raise GrampsDbWriteFailure((_('Failure writing %s') % filename,str(msg))) raise GrampsDbWriteFailure((_('Failure writing %s') % filename,
str(msg)))
return 0 return 0
self.g = codecs.getwriter("utf8")(g) self.g = codecs.getwriter("utf8")(g)
@ -1151,12 +1104,5 @@ def conf_priv(obj):
# #
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
_title = _('GRAMPS _XML database')
_description = _('The GRAMPS XML database is a format used by older '
'versions of GRAMPS. It is read-write compatible with '
'the present GRAMPS database format.')
_config = None
_filename = 'gramps'
from PluginUtils import register_export # Don't export a writer for plugins, that is the task of _WriteXML.py
register_export(exportData,_title,_description,_config,_filename)

View File

@ -30,6 +30,8 @@ GRAMPS' XML file format.
# load standard python libraries # load standard python libraries
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import shutil
import os
from gettext import gettext as _ from gettext import gettext as _
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -43,6 +45,19 @@ from QuestionDialog import ErrorDialog
import GrampsDbUtils import GrampsDbUtils
import ExportOptions import ExportOptions
from gen.db.exceptions import GrampsDbWriteFailure from gen.db.exceptions import GrampsDbWriteFailure
import gen.proxy
#-------------------------------------------------------------------------
#
# Attempt to load the GZIP library. Some version of python do not seem
# to be compiled with this available.
#
#-------------------------------------------------------------------------
try:
import gzip
_gzip_ok = 1
except:
_gzip_ok = 0
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -53,8 +68,32 @@ def export_data(database, filename, person, option_box, callback=None):
""" """
Calls the XML writer with the syntax expected by the export plugin Calls the XML writer with the syntax expected by the export plugin
""" """
return GrampsDbUtils.exportData(database, filename, person, option_box, if os.path.isfile(filename):
callback, const.VERSION) try:
shutil.copyfile(filename, filename + ".bak")
shutil.copystat(filename, filename + ".bak")
except:
pass
compress = _gzip_ok == 1
option_box.parse_options()
restrict = option_box.restrict
private = option_box.private
if private:
database = gen.proxy.PrivateProxyDb(database)
if restrict:
database = gen.proxy.LivingProxyDb(
database, gen.proxy.LivingProxyDb.MODE_RESTRICT)
if not option_box.cfilter.is_empty():
database = gen.proxy.FilterProxyDb(database, option_box.cfilter)
g = XmlWriter(database, callback, 0, compress)
return g.write(filename)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -74,10 +113,12 @@ class XmlWriter(GrampsDbUtils.GrampsDbXmlWriter):
""" """
Write the database to the specified file. Write the database to the specified file.
""" """
ret = 0 #False
try: try:
ret = GrampsDbUtils.GrampsDbXmlWriter.write(self, filename) ret = GrampsDbUtils.GrampsDbXmlWriter.write(self, filename)
except GrampsDbWriteFailure, val: except GrampsDbWriteFailure, msg:
ErrorDialog(val[0], val[1]) (m1,m2) = msg.messages()
ErrorDialog(m1, m2)
return ret return ret
#------------------------------------------------------------------------- #-------------------------------------------------------------------------

View File

@ -38,15 +38,13 @@ on using these factories see the _GrampsDbUtilsFactories.py file comments.
__version__ = "$Revision$" __version__ = "$Revision$"
from _GrampsDbWRFactories import \ from _GrampsDbWRFactories import \
gramps_db_writer_factory, \
gramps_db_reader_factory gramps_db_reader_factory
from _GedcomParse import GedcomParser from _GedcomParse import GedcomParser
from _WriteGedcom import GedcomWriter from _WriteGedcom import GedcomWriter
from _GrampsDbWriteXML import GrampsDbXmlWriter, \ from _GrampsDbWriteXML import GrampsDbXmlWriter
exportData, quick_write
from _WriteXML import XmlWriter from _WriteXML import XmlWriter
import _Backup as Backup import _Backup as Backup

View File

@ -26,21 +26,26 @@
class GrampsDbException(Exception): class GrampsDbException(Exception):
def __init__(self, value): def __init__(self, value):
Exception.__init__(self)
self.value = value self.value = value
def __str__(self): def __str__(self):
return repr(self.value) return self.value
class GrampsDbWriteFailure(Exception): class GrampsDbWriteFailure(Exception):
""" """
Error used to indicate that a write to a database has failed. Error used to indicate that a write to a database has failed.
""" """
def __init__(self, value, value2=""):
def __int__(self, value): Exception.__init__(self)
self.value = value self.value = value
self.value2 = value2
def __str__(self): def __str__(self):
return repr(self.value) return self.value
def messages(self):
return self.value, self.value2
class FileVersionError(Exception): class FileVersionError(Exception):
""" """