5746: Use the new User classes for Importers

svn: r19560
This commit is contained in:
Doug Blank 2012-05-17 20:57:23 +00:00
parent 3721709368
commit 8cebcd57db
17 changed files with 137 additions and 100 deletions

View File

@ -53,6 +53,7 @@ from clidbman import CLIDbManager, NAME_FILE, find_locker_name
from gen.plug import BasePluginManager
from gen.plug.report import CATEGORY_BOOK, CATEGORY_CODE
from cli.plug import cl_report
from cli.user import User
#-------------------------------------------------------------------------
#
@ -291,11 +292,12 @@ class ArgHandler(object):
except EOFError:
print
sys.exit(0)
if ans.upper() in ('Y', 'YES', _('YES').upper()):
self.__error( _("Will overwrite the existing file: %s")
% fullpath)
else:
sys.exit(0)
if ans.upper() in ('Y', 'YES', _('YES').upper()):
self.__error( _("Will overwrite the existing file: %s")
% fullpath)
answer = "ok"
else:
sys.exit(0)
if family_tree_format is None:
# Guess the file format based on the file extension.
@ -538,7 +540,7 @@ class ArgHandler(object):
for plugin in pmgr.get_import_plugins():
if family_tree_format == plugin.get_extension():
import_function = plugin.get_import_function()
import_function(self.dbstate.db, filename, None)
import_function(self.dbstate.db, filename, User())
if not self.cl:
if self.imp_db_path:

View File

@ -281,7 +281,7 @@ class CLIDbManager(object):
"""
return self.create_new_db_cli(title)
def import_new_db(self, filename, callback):
def import_new_db(self, filename, user):
"""
Attempt to import the provided file into a new database.
A new database will only be created if an appropriate importer was
@ -321,10 +321,10 @@ class CLIDbManager(object):
self.__start_cursor(_("Importing data..."))
dbclass = gen.db.DbBsddb
dbase = dbclass()
dbase.load(new_path, callback)
dbase.load(new_path, user.callback)
import_function = plugin.get_import_function()
import_function(dbase, filename, callback)
import_function(dbase, filename, user)
# finish up
self.__end_cursor()

View File

@ -55,9 +55,10 @@ class User(gen.user.User):
This class provides a means to interact with the user via CLI.
It implements the interface in gen.user.User()
"""
def __init__(self):
def __init__(self, callback=None):
self.steps = 0;
self.current_step = 0;
self.callback_function = callback
def begin_progress(self, title, message, steps):
"""
@ -92,6 +93,15 @@ class User(gen.user.User):
else:
percent = int((float(self.current_step) / self.steps) * 100)
sys.stdout.write("\r%02d%%" % percent)
def callback(self, percent):
"""
Display progress meter.
"""
if self.callback_function:
self.callback_function(percent)
else:
sys.stdout.write("\r%02d%%" % percent)
def end_progress(self):
"""
@ -113,7 +123,7 @@ class User(gen.user.User):
"""
return False
def warn(self, title, warning):
def warn(self, title, warning=""):
"""
Warn the user.
@ -125,7 +135,7 @@ class User(gen.user.User):
"""
print "%s %s" % (title, warning)
def notify_error(self, title, error):
def notify_error(self, title, error=""):
"""
Notify the user of an error.
@ -135,4 +145,19 @@ class User(gen.user.User):
@type error: str
@returns: none
"""
print "%s %s" % (title, warning)
print "%s %s" % (title, error)
def notify_db_error(self, error):
"""
Notify the user of a DB error.
@param error: the error message
@type error: str
@returns: none
"""
self.notify_error(
_("Low level database corruption detected"),
_("Gramps has detected a problem in the underlying "
"Berkeley database. This can be repaired from "
"the Family Tree Manager. Select the database and "
'click on the Repair button') + '\n\n' + error)

View File

@ -39,13 +39,12 @@ class ImportPlugin(Plugin):
@type description: string
@param import_function: A function to call to perform the import.
The function must take the form:
def import_function(db, filename, callback):
def import_function(db, filename, user):
where:
"db" is a Gramps database to import the data into
"filename" is the file that contains data to be imported
"callback" is a callable object that takes two parameters.
The first parameter is a progress indicator.
The second parameter is a text string.
"user" is an instance of the User class implementing
GUI functions (callbacks, errors, warnings, etc)
@type import_function: callable
@param extension: The extension for the files imported by this plugin.
Example: "ged"

View File

@ -61,6 +61,7 @@ import Utils
from gui.pluginmanager import GuiPluginManager
from QuestionDialog import (DBErrorDialog, ErrorDialog, QuestionDialog2,
WarningDialog)
from gui.user import User
import Errors
#-------------------------------------------------------------------------
@ -241,7 +242,7 @@ class DbLoader(CLIDbLoader):
#an importer can return an object with info, object.info_text()
#returns that info. Otherwise None is set to import_info
self.import_info = importer(self.dbstate.db, filename,
self._pulse_progress)
User(callback=self._pulse_progress))
dirname = os.path.dirname(filename) + os.path.sep
config.set('paths.recent-import-dir', dirname)
except UnicodeError, msg:

View File

@ -38,6 +38,7 @@ import copy
import subprocess
import urlparse
from gen.ggettext import gettext as _
from gui.user import User
#-------------------------------------------------------------------------
#
# set up logging
@ -510,7 +511,7 @@ class DbManager(CLIDbManager):
dbase.load(new_path, None)
self.__start_cursor(_("Importing archive..."))
check_out(dbase, revision, db_path, None)
check_out(dbase, revision, db_path, User())
self.__end_cursor()
dbase.close()
@ -805,7 +806,7 @@ def find_revisions(name):
def check_out(dbase, rev, path, callback):
def check_out(dbase, rev, path, user):
"""
Checks out the revision from rcs, and loads the resulting XML file
into the database.
@ -820,7 +821,7 @@ def check_out(dbase, rev, path, callback):
del proc
if status != 0:
ErrorDialog(
user.notify_error(
_("Retrieve failed"),
_("An attempt to retrieve the data failed "
"with the following message:\n\n%s") % message
@ -833,7 +834,7 @@ def check_out(dbase, rev, path, callback):
rdr = plugin.get_import_function()
xml_file = os.path.join(path, ARCHIVE)
rdr(dbase, xml_file, callback)
rdr(dbase, xml_file, user)
os.unlink(xml_file)
def check_in(dbase, filename, callback, cursor_func = None):

View File

@ -24,6 +24,13 @@
The User class provides basic interaction with the user.
"""
#-------------------------------------------------------------------------
#
# Python modules
#
#-------------------------------------------------------------------------
import sys
#-------------------------------------------------------------------------
#
# Gramps modules
@ -31,7 +38,7 @@ The User class provides basic interaction with the user.
#-------------------------------------------------------------------------
import gen.user
from gui.utils import ProgressMeter
from QuestionDialog import WarningDialog, ErrorDialog
from QuestionDialog import WarningDialog, ErrorDialog, DBErrorDialog
#-------------------------------------------------------------------------
#
@ -43,8 +50,9 @@ class User(gen.user.User):
This class provides a means to interact with the user via GTK.
It implements the interface in gen.user.User()
"""
def __init__(self):
def __init__(self, callback=None):
self.progress = None
self.callback_function = callback
def begin_progress(self, title, message, steps):
"""
@ -72,7 +80,16 @@ class User(gen.user.User):
"""
if self.progress:
self.progress.step()
def callback(self, percentage):
"""
Display the precentage.
"""
if self.callback_function:
self.callback_function(percentage)
else:
sys.stdout.write("\r%02d%%" % percent)
def end_progress(self):
"""
Stop showing the progress indicator to the user.
@ -95,7 +112,7 @@ class User(gen.user.User):
"""
return False
def warn(self, title, warning):
def warn(self, title, warning=""):
"""
Warn the user.
@ -107,7 +124,7 @@ class User(gen.user.User):
"""
WarningDialog(title, warning)
def notify_error(self, title, error):
def notify_error(self, title, error=""):
"""
Notify the user of an error.
@ -117,4 +134,14 @@ class User(gen.user.User):
@type error: str
@returns: none
"""
ErrorDialog(title, warning)
ErrorDialog(title, error)
def notify_db_error(self, error):
"""
Notify the user of a DB error.
@param error: the DB error message
@type error: str
@returns: none
"""
DBErrorDialog(error)

View File

@ -52,7 +52,6 @@ from gen.ggettext import ngettext
import gen.lib
from gen.db import DbTxn
from gen.plug.utils import OpenFileOrStdin
from QuestionDialog import ErrorDialog
from DateHandler import parser as _dp
from Utils import gender as gender_map
from Utils import create_id
@ -138,14 +137,14 @@ def rd(line_number, row, col, key, default = None):
else:
return default
def importData(dbase, filename, callback=None):
def importData(dbase, filename, user):
"""Function called by Gramps to import data on persons in CSV format."""
parser = CSVParser(dbase, callback)
parser = CSVParser(dbase, user)
try:
with OpenFileOrStdin(filename, 'b') as filehandle:
parser.parse(filehandle)
except EnvironmentError, err:
ErrorDialog(_("%s could not be opened\n") % filename, str(err))
user.notify_error(_("%s could not be opened\n") % filename, str(err))
return
return None # This module doesn't provide info about what got imported.
@ -156,9 +155,9 @@ def importData(dbase, filename, callback=None):
#-------------------------------------------------------------------------
class CSVParser(object):
"""Class to read data in CSV format from a file object."""
def __init__(self, dbase, callback):
def __init__(self, dbase, user):
self.db = dbase
self.callback = callback
self.user = user
self.trans = None
self.lineno = 0
self.index = 0

View File

@ -44,7 +44,6 @@ LOG = logging.getLogger(".GedcomImport")
#
#------------------------------------------------------------------------
import Errors
from QuestionDialog import ErrorDialog, DBErrorDialog
from glade import Glade
from libmixin import DbMixin
import libgedcom
@ -58,7 +57,7 @@ import config
# importData
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None):
def importData(database, filename, user):
"""
Try to handle ANSEL encoded files that are not really ANSEL encoded
"""
@ -107,14 +106,14 @@ def importData(database, filename, callback=None):
stage_one.set_encoding(code_set)
ifile.seek(0)
gedparse = libgedcom.GedcomParser(
database, ifile, filename, callback, stage_one,
database, ifile, filename, user, stage_one,
config.get('preferences.default-source'))
except IOError, msg:
ErrorDialog(_("%s could not be opened\n") % filename, str(msg))
user.notify_error(_("%s could not be opened\n") % filename, str(msg))
return
except Errors.GedcomError, msg:
ErrorDialog(_("Invalid GEDCOM file"),
_("%s could not be imported") % filename + "\n" + str(msg))
user.notify_error(_("Invalid GEDCOM file"),
_("%s could not be imported") % filename + "\n" + str(msg))
return
try:
@ -125,11 +124,11 @@ def importData(database, filename, callback=None):
ifile.close()
except IOError, msg:
msg = _("%s could not be opened\n") % filename
ErrorDialog(msg, str(msg))
user.notify_error(msg, str(msg))
return
except Errors.DbError, msg:
DBErrorDialog(str(msg.value))
user.notify_db_error(str(msg.value))
return
except Errors.GedcomError, msg:
ErrorDialog(_('Error reading GEDCOM file'), str(msg))
user.notify_error(_('Error reading GEDCOM file'), str(msg))
return

View File

@ -49,7 +49,6 @@ LOG = logging.getLogger(".ImportGeneWeb")
import Errors
import gen.lib
from gen.db import DbTxn
from QuestionDialog import ErrorDialog
from htmlentitydefs import name2codepoint
_date_parse = re.compile('([kmes~?<>]+)?([0-9/]+)([J|H|F])?(\.\.)?([0-9/]+)?([J|H|F])?')
@ -72,21 +71,21 @@ _cal_map = {
#
#
#-------------------------------------------------------------------------
def importData(database, filename, cb=None):
def importData(database, filename, user):
global callback
try:
g = GeneWebParser(database,filename)
except IOError,msg:
ErrorDialog(_("%s could not be opened\n") % filename,str(msg))
user.notify_error(_("%s could not be opened\n") % filename,str(msg))
return
try:
status = g.parse_geneweb_file()
except IOError,msg:
errmsg = _("%s could not be opened\n") % filename
ErrorDialog(errmsg,str(msg))
user.notify_error(errmsg,str(msg))
return
#-------------------------------------------------------------------------

View File

@ -48,7 +48,6 @@ log = logging.getLogger(".ReadPkg")
#-------------------------------------------------------------------------
import const
import ImportXml
from QuestionDialog import ErrorDialog, WarningDialog
import Utils
#-------------------------------------------------------------------------
@ -56,7 +55,7 @@ import Utils
#
#
#-------------------------------------------------------------------------
def impData(database, name, cb=None):
def impData(database, name, user):
# Create tempdir, if it does not exist, then check for writability
# THE TEMP DIR is named as the filname.gpkg.media and is created
# in the mediapath dir of the family tree we import to
@ -69,16 +68,16 @@ def impData(database, name, cb=None):
try:
os.mkdir(tmpdir_path, 0700)
except:
ErrorDialog( _("Could not create media directory %s") %
user.notify_error( _("Could not create media directory %s") %
tmpdir_path )
return
elif not os.access(tmpdir_path, os.W_OK):
ErrorDialog(_("Media directory %s is not writable") % tmpdir_path)
user.notify_error(_("Media directory %s is not writable") % tmpdir_path)
return
else:
# mediadir exists and writable -- User could have valuable stuff in
# it, have him remove it!
ErrorDialog(_("Media directory %s exists. Delete it first, then"
user.notify_error(_("Media directory %s exists. Delete it first, then"
" restart the import process") % tmpdir_path)
return
try:
@ -87,13 +86,13 @@ def impData(database, name, cb=None):
archive.extract(tarinfo, tmpdir_path)
archive.close()
except:
ErrorDialog(_("Error extracting into %s") % tmpdir_path)
user.notify_error(_("Error extracting into %s") % tmpdir_path)
return
imp_db_name = os.path.join(tmpdir_path, const.XMLFILE)
importer = ImportXml.importData
info = importer(database, imp_db_name, cb)
info = importer(database, imp_db_name, user)
newmediapath = database.get_mediapath()
#import of gpkg should not change media path as all media has new paths!
if not oldmediapath == newmediapath :
@ -102,7 +101,7 @@ def impData(database, name, cb=None):
# Set correct media dir if possible, complain if problems
if oldmediapath is None:
database.set_mediapath(tmpdir_path)
WarningDialog(
user.warn(
_("Base path for relative media set"),
_("The base media path of this family tree has been set to "
"%s. Consider taking a simpler path. You can change this "
@ -112,7 +111,7 @@ def impData(database, name, cb=None):
" correct paths in your media objects."
) % tmpdir_path)
else:
WarningDialog(
user.warn(
_("Cannot set base media path"),
_("The family tree you imported into already has a base media "
"path: %(orig_path)s. The imported media objects however "

View File

@ -35,7 +35,6 @@ LOG = logging.getLogger(".Db")
# Gramps Modules
#
#-------------------------------------------------------------------------
from QuestionDialog import ErrorDialog
#-------------------------------------------------------------------------
#
@ -53,17 +52,8 @@ _DBVERSION = 14
# http://gramps.1791082.n4.nabble.com/Status-of-GEPS-023-tp4329141p4329746.html
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None, cl=0):
if cl:
LOG.warn("Error: %s could not be opened.\n%s Exiting." \
% (filename,
_("The database version is not supported "
"by this version of Gramps.\n"\
"Please upgrade to the corresponding version "
"or use XML for porting data between different "
"database versions.")))
else:
ErrorDialog(_("%s could not be opened") % filename,
def importData(database, filename, user):
user.notify_error(_("%s could not be opened") % filename,
_("The Database version is not supported "
"by this version of Gramps."
"You should use an old copy of Gramps at "

View File

@ -51,7 +51,6 @@ import Utils
from gui.utils import ProgressMeter
import gen.lib
from gen.db import DbTxn
from QuestionDialog import ErrorDialog
class ProgenError(Exception):
"""Error used to report Progen errors."""
@ -63,21 +62,21 @@ class ProgenError(Exception):
return self.value
def _importData(database, filename, cb=None):
def _importData(database, filename, user):
try:
g = ProgenParser(database, filename)
except IOError, msg:
ErrorDialog(_("%s could not be opened") % filename, str(msg))
user.notify_error(_("%s could not be opened") % filename, str(msg))
return
try:
status = g.parse_progen_file()
except ProgenError, msg:
ErrorDialog(_("Pro-Gen data error"), str(msg))
user.notify_error(_("Pro-Gen data error"), str(msg))
return
except IOError, msg:
ErrorDialog(_("%s could not be opened") % filename, str(msg))
user.notify_error(_("%s could not be opened") % filename, str(msg))
return

View File

@ -52,24 +52,23 @@ import Errors
import gen.lib
from gen.db import DbTxn
from gen.plug.utils import OpenFileOrStdin
from QuestionDialog import ErrorDialog
#-------------------------------------------------------------------------
#
# Support Functions
#
#-------------------------------------------------------------------------
def importData(database, filename, cb_progress=None):
def importData(database, filename, user):
"""Function called by Gramps to import data on persons in VCard format."""
parser = VCardParser(database)
try:
with OpenFileOrStdin(filename) as filehandle:
parser.parse(filehandle)
except EnvironmentError, msg:
ErrorDialog(_("%s could not be opened\n") % filename, str(msg))
user.notify_error(_("%s could not be opened\n") % filename, str(msg))
return
except Errors.GrampsImportError, msg:
ErrorDialog(_("%s could not be opened\n") % filename, str(msg))
user.notify_error(_("%s could not be opened\n") % filename, str(msg))
return
return None # This module doesn't provide info about what got imported.

View File

@ -43,7 +43,6 @@ LOG = logging.getLogger(".ImportXML")
# Gramps Modules
#
#-------------------------------------------------------------------------
from QuestionDialog import ErrorDialog, WarningDialog
import gen.mime
import gen.lib
from gen.db import DbTxn
@ -95,10 +94,7 @@ INSTANTIATED = 1
# Must takes care of renaming media files according to their new IDs.
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None):
## return Utils.profile(importDataPro, database, filename, callback, cl)
##def importDataPro(database, filename, callback=None, cl=0):
def importData(database, filename, user):
filename = os.path.normpath(filename)
basefile = os.path.dirname(filename)
database.smap = {}
@ -107,7 +103,7 @@ def importData(database, filename, callback=None):
line_cnt = 0
person_cnt = 0
with ImportOpenFileContextManager(filename) as xml_file:
with ImportOpenFileContextManager(filename, user) as xml_file:
if xml_file is None:
return
@ -115,7 +111,7 @@ def importData(database, filename, callback=None):
change = time.time()
else:
change = os.path.getmtime(filename)
parser = GrampsParser(database, callback, change)
parser = GrampsParser(database, user, change)
if filename != '-':
linecounter = LineParser(filename)
@ -128,15 +124,15 @@ def importData(database, filename, callback=None):
try:
info = parser.parse(xml_file, line_cnt, person_cnt)
except GrampsImportError, err: # version error
ErrorDialog(*err.messages())
user.notify_error(*err.messages())
return
except IOError, msg:
ErrorDialog(_("Error reading %s") % filename, str(msg))
user.notify_error(_("Error reading %s") % filename, str(msg))
import traceback
traceback.print_exc()
return
except ExpatError, msg:
ErrorDialog(_("Error reading %s") % filename,
user.notify_error(_("Error reading %s") % filename,
_("The file is probably either corrupt or not a "
"valid Gramps database."))
return
@ -377,9 +373,10 @@ class ImportOpenFileContextManager:
"""
Context manager to open a file or stdin for reading.
"""
def __init__(self, filename):
def __init__(self, filename, user):
self.filename = filename
self.filehandle = None
self.user = user
def __enter__(self):
if self.filename == '-':
@ -418,10 +415,10 @@ class ImportOpenFileContextManager:
else:
xml_file = open(filename, "r")
except IOError, msg:
ErrorDialog(_("%s could not be opened") % filename, str(msg))
self.user.notify_error(_("%s could not be opened") % filename, str(msg))
xml_file = None
except:
ErrorDialog(_("%s could not be opened") % filename)
self.user.notify_error(_("%s could not be opened") % filename)
xml_file = None
return xml_file
@ -433,8 +430,9 @@ class ImportOpenFileContextManager:
#-------------------------------------------------------------------------
class GrampsParser(UpdateCallback):
def __init__(self, database, callback, change):
UpdateCallback.__init__(self, callback)
def __init__(self, database, user, change):
UpdateCallback.__init__(self, user.callback)
self.user = user
self.__gramps_version = 'unknown'
self.__xml_version = '1.0.0'
self.stext_list = []
@ -894,7 +892,7 @@ class GrampsParser(UpdateCallback):
if not oldpath:
self.db.set_mediapath(self.mediapath)
elif not oldpath == self.mediapath:
ErrorDialog(_("Could not change media path"),
self.user.notify_error(_("Could not change media path"),
_("The opened file has media path %s, which conflicts with"
" the media path of the family tree you import into. "
"The original media path has been retained. Copy the "
@ -997,7 +995,7 @@ class GrampsParser(UpdateCallback):
'newgramps': const.VERSION,
'xmlversion': self.__xml_version,
}
WarningDialog(_('Old xml file'), msg)
self.user.warn(_('Old xml file'), msg)
def start_lds_ord(self, attrs):
self.ord = gen.lib.LdsOrd()
@ -1599,7 +1597,7 @@ class GrampsParser(UpdateCallback):
msg = _('Your family tree groups name "%(key)s" together'
' with "%(parent)s", did not change this grouping to "%(value)s".') % {
'key' : key, 'parent' : present, 'value' : value }
WarningDialog(_("Gramps ignored namemap value"), msg)
self.user.warn(_("Gramps ignored namemap value"), msg)
else:
self.db.set_name_group_mapping(key, value)

View File

@ -1770,9 +1770,9 @@ class GedcomParser(UpdateCallback):
name.set_first_name(text.strip())
return name
def __init__(self, dbase, ifile, filename, callback, stage_one,
def __init__(self, dbase, ifile, filename, user, stage_one,
default_source):
UpdateCallback.__init__(self, callback)
UpdateCallback.__init__(self, user.callback)
self.set_total(stage_one.get_line_count())
self.repo2id = {}

View File

@ -41,11 +41,11 @@ import os
# db = dbdjango.DbDjango()
# run_report(db, "ancestor_report", off="txt", of="ar.txt", pid="I0363")
def import_file(db, filename, callback):
def import_file(db, filename, user):
"""
Import a file (such as a GEDCOM file) into the given db.
>>> import_file(DbDjango(), "/home/user/Untitled_1.ged", lambda a: a)
>>> import_file(DbDjango(), "/home/user/Untitled_1.ged", User())
"""
from grampsdb.models import Person
dbstate = DbState.DbState()
@ -67,7 +67,7 @@ def import_file(db, filename, callback):
return False
retval = import_function = getattr(mod, pdata.import_function)
db.prepare_import()
import_function(db, filename, callback)
import_function(db, filename, user)
db.commit_import()
# FIXME: need to call probably_alive
for person in Person.objects.all():