2009-08-19 22:35:39 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2004-2006 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2009-12-21 19:13:50 +05:30
|
|
|
# $Id$
|
2009-08-19 22:35:39 +05:30
|
|
|
|
|
|
|
"""
|
2009-12-23 21:25:58 +05:30
|
|
|
Exports the DbUndo class for managing Gramps transactions
|
2009-08-19 22:35:39 +05:30
|
|
|
undos and redos.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-09-04 22:50:37 +05:30
|
|
|
from __future__ import with_statement
|
2009-08-19 22:35:39 +05:30
|
|
|
import time, os
|
|
|
|
import cPickle as pickle
|
|
|
|
from bsddb import db
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2010-04-24 00:32:24 +05:30
|
|
|
from collections import deque
|
2009-08-19 22:35:39 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from gen.db.dbconst import *
|
|
|
|
from gen.db import BSDDBTxn
|
|
|
|
import Errors
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Local Constants
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
DBERRS = (db.DBRunRecoveryError, db.DBAccessError,
|
|
|
|
db.DBPageNotFoundError, db.DBInvalidArgError)
|
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
_SIGBASE = ('person', 'family', 'source', 'event', 'media',
|
|
|
|
'place', 'repository', 'reference', 'note')
|
2009-08-19 22:35:39 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2009-12-23 21:25:58 +05:30
|
|
|
# DbUndo class
|
2009-08-19 22:35:39 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-12-23 21:25:58 +05:30
|
|
|
class DbUndo(object):
|
2009-08-19 22:35:39 +05:30
|
|
|
"""
|
|
|
|
Base class for the gramps undo/redo manager. Needs to be subclassed
|
|
|
|
for use with a real backend.
|
|
|
|
"""
|
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
__slots__ = ('undodb', 'db', 'mapbase', 'undo_history_timestamp',
|
|
|
|
'txn', 'undoq', 'redoq')
|
2009-08-19 22:35:39 +05:30
|
|
|
|
|
|
|
def __init__(self, grampsdb):
|
|
|
|
"""
|
|
|
|
Class constructor. Set up main instance variables
|
|
|
|
"""
|
|
|
|
self.db = grampsdb
|
2010-05-21 00:02:08 +05:30
|
|
|
self.undoq = deque()
|
|
|
|
self.redoq = deque()
|
|
|
|
self.undo_history_timestamp = time.time()
|
|
|
|
self.txn = None
|
2009-08-19 22:35:39 +05:30
|
|
|
self.mapbase = (
|
|
|
|
self.db.person_map,
|
|
|
|
self.db.family_map,
|
|
|
|
self.db.source_map,
|
|
|
|
self.db.event_map,
|
|
|
|
self.db.media_map,
|
|
|
|
self.db.place_map,
|
|
|
|
self.db.repository_map,
|
|
|
|
self.db.reference_map,
|
|
|
|
self.db.note_map,
|
|
|
|
)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
"""
|
|
|
|
Clear the undo/redo list (but not the backing storage)
|
|
|
|
"""
|
2010-05-21 00:02:08 +05:30
|
|
|
self.undoq.clear()
|
|
|
|
self.redoq.clear()
|
2009-08-19 22:35:39 +05:30
|
|
|
self.undo_history_timestamp = time.time()
|
|
|
|
self.txn = None
|
|
|
|
|
|
|
|
def __enter__(self, value):
|
|
|
|
"""
|
|
|
|
Context manager method to establish the context
|
|
|
|
"""
|
|
|
|
self.open(value)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""
|
|
|
|
Context manager method to finish the context
|
|
|
|
"""
|
|
|
|
if exc_type is None:
|
|
|
|
self.close()
|
|
|
|
return exc_type is None
|
|
|
|
|
|
|
|
def open(self, value):
|
|
|
|
"""
|
|
|
|
Open the backing storage. Needs to be overridden in the derived
|
|
|
|
class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Close the backing storage. Needs to be overridden in the derived
|
|
|
|
class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
"""
|
|
|
|
Add a new entry on the end. Needs to be overridden in the derived
|
|
|
|
class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""
|
|
|
|
Returns an entry by index number. Needs to be overridden in the
|
|
|
|
derived class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
"""
|
|
|
|
Set an entry to a value. Needs to be overridden in the derived class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""
|
|
|
|
Returns the number of entries. Needs to be overridden in the derived
|
|
|
|
class.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def commit(self, txn, msg):
|
|
|
|
"""
|
|
|
|
Commit the transaction to the undo/redo database. "txn" should be
|
|
|
|
an instance of gramps gramps transaction class
|
|
|
|
"""
|
|
|
|
txn.set_description(msg)
|
|
|
|
txn.timestamp = time.time()
|
2010-04-24 00:32:24 +05:30
|
|
|
self.undoq.append(txn)
|
2009-08-19 22:35:39 +05:30
|
|
|
|
|
|
|
def undo(self, update_history=True):
|
|
|
|
"""
|
|
|
|
Undo a previously committed transaction
|
|
|
|
"""
|
2010-05-21 00:02:08 +05:30
|
|
|
if self.db.readonly or self.undo_count == 0:
|
2009-08-19 22:35:39 +05:30
|
|
|
return False
|
2010-05-21 00:02:08 +05:30
|
|
|
return self.__undo(update_history)
|
2009-08-19 22:35:39 +05:30
|
|
|
|
|
|
|
def redo(self, update_history=True):
|
|
|
|
"""
|
|
|
|
Redo a previously committed, then undone, transaction
|
|
|
|
"""
|
2010-05-21 00:02:08 +05:30
|
|
|
if self.db.readonly or self.redo_count == 0:
|
2009-08-19 22:35:39 +05:30
|
|
|
return False
|
2010-05-21 00:02:08 +05:30
|
|
|
return self.__redo(update_history)
|
2009-08-19 22:35:39 +05:30
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
def undoredo(func):
|
2009-08-19 22:35:39 +05:30
|
|
|
"""
|
2010-05-21 00:02:08 +05:30
|
|
|
Decorator function to wrap undo and redo operations within a bsddb
|
|
|
|
transaction. It also catches bsddb errors and raises an exception
|
|
|
|
as appropriate
|
2009-08-19 22:35:39 +05:30
|
|
|
"""
|
2010-05-21 00:02:08 +05:30
|
|
|
def try_(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
with BSDDBTxn(self.db.env) as txn:
|
|
|
|
self.txn = self.db.txn = txn.txn
|
|
|
|
status = func(self, *args, **kwargs)
|
|
|
|
if not status:
|
|
|
|
txn.abort()
|
|
|
|
self.db.txn = None
|
|
|
|
return status
|
2009-08-19 22:35:39 +05:30
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
except DBERRS, msg:
|
|
|
|
self.db._log_error()
|
|
|
|
raise Errors.DbError(msg)
|
|
|
|
|
|
|
|
return try_
|
2009-08-19 22:35:39 +05:30
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
@undoredo
|
2009-08-19 22:35:39 +05:30
|
|
|
def __undo(self, update_history=True):
|
|
|
|
"""
|
|
|
|
Access the last committed transaction, and revert the data to the
|
|
|
|
state before the transaction was committed.
|
|
|
|
"""
|
2010-04-24 00:32:24 +05:30
|
|
|
txn = self.undoq.pop()
|
|
|
|
self.redoq.append(txn)
|
|
|
|
transaction = txn
|
2009-08-19 22:35:39 +05:30
|
|
|
db = self.db
|
|
|
|
subitems = transaction.get_recnos(reverse=True)
|
|
|
|
|
|
|
|
# Process all records in the transaction
|
|
|
|
for record_id in subitems:
|
|
|
|
(key, trans_type, handle, old_data, new_data) = \
|
|
|
|
pickle.loads(self.undodb[record_id])
|
|
|
|
|
|
|
|
if key == REFERENCE_KEY:
|
|
|
|
self.undo_reference(old_data, handle, self.mapbase[key])
|
|
|
|
else:
|
|
|
|
self.undo_data(old_data, handle, self.mapbase[key],
|
|
|
|
db.emit, _SIGBASE[key])
|
|
|
|
# Notify listeners
|
|
|
|
if db.undo_callback:
|
2010-05-21 00:02:08 +05:30
|
|
|
if self.undo_count > 0:
|
2009-08-19 22:35:39 +05:30
|
|
|
db.undo_callback(_("_Undo %s")
|
|
|
|
% transaction.get_description())
|
|
|
|
else:
|
|
|
|
db.undo_callback(None)
|
|
|
|
|
|
|
|
if db.redo_callback:
|
|
|
|
db.redo_callback(_("_Redo %s")
|
|
|
|
% transaction.get_description())
|
|
|
|
|
|
|
|
if update_history and db.undo_history_callback:
|
|
|
|
db.undo_history_callback()
|
|
|
|
return True
|
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
@undoredo
|
2009-08-19 22:35:39 +05:30
|
|
|
def __redo(self, db=None, update_history=True):
|
|
|
|
"""
|
2010-04-24 00:32:24 +05:30
|
|
|
Access the last undone transaction, and revert the data to the state
|
2009-08-19 22:35:39 +05:30
|
|
|
before the transaction was undone.
|
|
|
|
"""
|
2010-04-24 00:32:24 +05:30
|
|
|
txn = self.redoq.pop()
|
|
|
|
self.undoq.append(txn)
|
|
|
|
transaction = txn
|
2009-08-19 22:35:39 +05:30
|
|
|
db = self.db
|
|
|
|
subitems = transaction.get_recnos()
|
|
|
|
|
|
|
|
# Process all records in the transaction
|
|
|
|
for record_id in subitems:
|
|
|
|
(key, trans_type, handle, old_data, new_data) = \
|
|
|
|
pickle.loads(self.undodb[record_id])
|
|
|
|
|
|
|
|
if key == REFERENCE_KEY:
|
|
|
|
self.undo_reference(new_data, handle, self.mapbase[key])
|
|
|
|
else:
|
|
|
|
self.undo_data(new_data, handle, self.mapbase[key],
|
|
|
|
db.emit, _SIGBASE[key])
|
|
|
|
# Notify listeners
|
|
|
|
if db.undo_callback:
|
|
|
|
db.undo_callback(_("_Undo %s")
|
|
|
|
% transaction.get_description())
|
|
|
|
|
|
|
|
if db.redo_callback:
|
2010-05-21 00:02:08 +05:30
|
|
|
if self.redo_count > 1:
|
2010-04-24 00:32:24 +05:30
|
|
|
new_transaction = self.redoq[-2]
|
2009-08-19 22:35:39 +05:30
|
|
|
db.redo_callback(_("_Redo %s")
|
|
|
|
% new_transaction.get_description())
|
|
|
|
else:
|
|
|
|
db.redo_callback(None)
|
|
|
|
|
|
|
|
if update_history and db.undo_history_callback:
|
|
|
|
db.undo_history_callback()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def undo_reference(self, data, handle, db_map):
|
|
|
|
"""
|
|
|
|
Helper method to undo a reference map entry
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if data is None:
|
|
|
|
db_map.delete(handle, txn=self.txn)
|
|
|
|
else:
|
|
|
|
db_map.put(handle, data, txn=self.txn)
|
|
|
|
|
|
|
|
except DBERRS, msg:
|
|
|
|
self.db._log_error()
|
|
|
|
raise Errors.DbError(msg)
|
|
|
|
|
|
|
|
def undo_data(self, data, handle, db_map, emit, signal_root):
|
|
|
|
"""
|
|
|
|
Helper method to undo/redo the changes made
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if data is None:
|
|
|
|
emit(signal_root + '-delete', ([handle],))
|
|
|
|
db_map.delete(handle, txn=self.txn)
|
|
|
|
else:
|
|
|
|
ex_data = db_map.get(handle, txn=self.txn)
|
|
|
|
if ex_data:
|
|
|
|
signal = signal_root + '-update'
|
|
|
|
else:
|
|
|
|
signal = signal_root + '-add'
|
|
|
|
db_map.put(handle, data, txn=self.txn)
|
|
|
|
emit(signal, ([handle],))
|
|
|
|
|
|
|
|
except DBERRS, msg:
|
|
|
|
self.db._log_error()
|
2010-04-24 00:32:24 +05:30
|
|
|
raise Errors.DbError(msg)
|
|
|
|
|
2010-05-21 00:02:08 +05:30
|
|
|
undo_count = property(lambda self:len(self.undoq))
|
|
|
|
redo_count = property(lambda self:len(self.redoq))
|
2009-08-19 22:35:39 +05:30
|
|
|
|
2009-12-23 21:25:58 +05:30
|
|
|
class DbUndoList(DbUndo):
|
2009-08-19 22:35:39 +05:30
|
|
|
"""
|
|
|
|
Implementation of the gramps undo database using a Python list
|
|
|
|
"""
|
|
|
|
def __init__(self, grampsdb):
|
|
|
|
"""
|
|
|
|
Class constructor
|
|
|
|
"""
|
2009-12-23 21:25:58 +05:30
|
|
|
super(DbUndoList, self).__init__(grampsdb)
|
2009-08-19 22:35:39 +05:30
|
|
|
self.undodb = []
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
"""
|
|
|
|
A list does not need to be opened
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Close the list by resetting it to empty
|
|
|
|
"""
|
|
|
|
self.undodb = []
|
|
|
|
self.clear()
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
"""
|
|
|
|
Add an entry on the end of the list
|
|
|
|
"""
|
|
|
|
self.undodb.append(value)
|
|
|
|
return len(self.undodb)-1
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""
|
|
|
|
Return an item at the specified index
|
|
|
|
"""
|
|
|
|
return self.undodb[index]
|
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
"""
|
|
|
|
Set an item at the speficied index to the given value
|
|
|
|
"""
|
|
|
|
self.undodb[index] = value
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
Iterator
|
|
|
|
"""
|
|
|
|
for item in self.undodb:
|
|
|
|
yield item
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""
|
|
|
|
Return number of entries in the list
|
|
|
|
"""
|
|
|
|
return len(self.undodb)
|
|
|
|
|
2009-12-23 21:25:58 +05:30
|
|
|
class DbUndoBSDDB(DbUndo):
|
2009-08-19 22:35:39 +05:30
|
|
|
"""
|
|
|
|
Class constructor for gramps undo/redo database using a bsddb recno
|
|
|
|
database as the backing store.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, grampsdb, path):
|
|
|
|
"""
|
|
|
|
Class constructor
|
|
|
|
"""
|
2009-12-23 21:25:58 +05:30
|
|
|
super(DbUndoBSDDB, self).__init__(grampsdb)
|
2009-08-19 22:35:39 +05:30
|
|
|
self.undodb = db.DB()
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
"""
|
|
|
|
Open the undo/redo database
|
|
|
|
"""
|
|
|
|
self.undodb.open(self.path, db.DB_RECNO, db.DB_CREATE)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Close the undo/redo database
|
|
|
|
"""
|
|
|
|
self.undodb.close()
|
|
|
|
try:
|
|
|
|
os.remove(self.path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
self.clear()
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
"""
|
|
|
|
Add an entry on the end of the database
|
|
|
|
"""
|
|
|
|
return self.undodb.append(value)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""
|
|
|
|
Returns the number of entries in the database
|
|
|
|
"""
|
|
|
|
x = self.undodb.stat()['nkeys']
|
|
|
|
y = len(self.undodb)
|
|
|
|
assert x == y
|
|
|
|
return x
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""
|
|
|
|
Returns the entry stored at the specified index
|
|
|
|
"""
|
|
|
|
return self.undodb.get(index)
|
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
"""
|
|
|
|
Sets the entry stored at the specified index to the value given.
|
|
|
|
"""
|
|
|
|
self.undodb.put(index, value)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
Iterator
|
|
|
|
"""
|
|
|
|
cursor = self.undodb.cursor()
|
|
|
|
data = cursor.first()
|
|
|
|
while data:
|
|
|
|
yield data
|
|
|
|
data = cursor.next()
|
|
|
|
|
|
|
|
def testundo():
|
|
|
|
class T:
|
|
|
|
def __init__(self):
|
|
|
|
self.msg = ''
|
|
|
|
self.timetstamp = 0
|
|
|
|
def set_description(self, msg):
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
class D:
|
|
|
|
def __init__(self):
|
|
|
|
self.person_map = {}
|
|
|
|
self.family_map = {}
|
|
|
|
self.source_map = {}
|
|
|
|
self.event_map = {}
|
|
|
|
self.media_map = {}
|
|
|
|
self.place_map = {}
|
|
|
|
self.note_map = {}
|
|
|
|
self.repository_map = {}
|
|
|
|
self.reference_map = {}
|
|
|
|
|
|
|
|
print "list tests"
|
2009-12-23 21:25:58 +05:30
|
|
|
undo = DbUndoList(D())
|
2009-08-19 22:35:39 +05:30
|
|
|
print undo.append('foo')
|
|
|
|
print undo.append('bar')
|
|
|
|
print undo[0]
|
|
|
|
undo[0] = 'foobar'
|
|
|
|
print undo[0]
|
|
|
|
print "len", len(undo)
|
|
|
|
print "iter"
|
|
|
|
for data in undo:
|
|
|
|
print data
|
|
|
|
print
|
|
|
|
print "bsddb tests"
|
2009-12-23 21:25:58 +05:30
|
|
|
undo = DbUndoBSDDB(D(), '/tmp/testundo')
|
2009-08-19 22:35:39 +05:30
|
|
|
undo.open()
|
|
|
|
print undo.append('foo')
|
|
|
|
print undo.append('fo2')
|
|
|
|
print undo.append('fo3')
|
|
|
|
print undo[1]
|
|
|
|
undo[1] = 'bar'
|
|
|
|
print undo[1]
|
|
|
|
for data in undo:
|
|
|
|
print data
|
|
|
|
print "len", len(undo)
|
|
|
|
|
|
|
|
print "test commit"
|
|
|
|
undo.commit(T(), msg="test commit")
|
|
|
|
undo.close()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
testundo()
|