2007-06-04 08:22:49 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2007 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-06-12 21:47:07 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import cPickle as pickle
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GrampsCursor class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
2009-05-21 22:49:50 +05:30
|
|
|
class GrampsCursor(object):
|
2007-06-04 08:22:49 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Provide a basic iterator that allows the user to cycle through
|
2008-02-23 14:08:55 +05:30
|
|
|
the elements in a particular map.
|
|
|
|
|
|
|
|
A cursor should never be directly instantiated. Instead, in should be
|
|
|
|
created by the database class.
|
2007-06-04 08:22:49 +05:30
|
|
|
|
|
|
|
A cursor should only be used for a single pass through the
|
|
|
|
database. If multiple passes are needed, multiple cursors
|
|
|
|
should be used.
|
|
|
|
"""
|
2009-06-12 21:47:07 +05:30
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Instantiate the object. Note, this method should be overridden in
|
|
|
|
derived classes that properly set self.cursor and self.source
|
|
|
|
"""
|
|
|
|
self.cursor = self.source = None
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
|
|
|
Return a method from the underlying cursor object, if it exists
|
|
|
|
"""
|
|
|
|
return getattr(self.cursor, name)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
"""
|
|
|
|
Context manager enter method
|
|
|
|
"""
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""
|
|
|
|
Context manager exit method
|
|
|
|
"""
|
|
|
|
self.close()
|
|
|
|
return exc_type is None
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
Iterator
|
|
|
|
"""
|
|
|
|
|
|
|
|
data = self.first()
|
|
|
|
while data:
|
|
|
|
yield data
|
|
|
|
data = self.next()
|
|
|
|
|
|
|
|
def first(self, *args, **kwargs):
|
2007-06-04 08:22:49 +05:30
|
|
|
"""
|
2008-02-23 14:08:55 +05:30
|
|
|
Return the first (index, data) pair in the database.
|
|
|
|
|
|
|
|
This should be called before the first call to next(). Note that the
|
|
|
|
data return is in the format of the serialized format stored in the
|
|
|
|
database, not in the more usable class object. The data should be
|
|
|
|
converted to a class using the class's unserialize method.
|
2007-06-04 08:22:49 +05:30
|
|
|
|
|
|
|
If no data is available, None is returned.
|
|
|
|
"""
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
data = self.cursor.first(*args, **kwargs)
|
2009-06-12 21:47:07 +05:30
|
|
|
if data:
|
|
|
|
return (data[0], pickle.loads(data[1]))
|
2007-06-04 08:22:49 +05:30
|
|
|
return None
|
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
def next(self, *args, **kwargs):
|
2007-06-04 08:22:49 +05:30
|
|
|
"""
|
2008-02-23 14:08:55 +05:30
|
|
|
Return the next (index, data) pair in the database.
|
|
|
|
|
|
|
|
Like the first() method, the data return is in the format of the
|
|
|
|
serialized format stored in the database, not in the more usable class
|
|
|
|
object. The data should be converted to a class using the class's
|
|
|
|
unserialize method.
|
2007-06-04 08:22:49 +05:30
|
|
|
|
|
|
|
None is returned when no more data is available.
|
|
|
|
"""
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
data = self.cursor.next(*args, **kwargs)
|
2009-06-12 21:47:07 +05:30
|
|
|
if data:
|
|
|
|
return (data[0], pickle.loads(data[1]))
|
2007-06-04 08:22:49 +05:30
|
|
|
return None
|
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
def prev(self, *args, **kwargs):
|
2007-06-04 08:22:49 +05:30
|
|
|
"""
|
2009-08-19 22:44:32 +05:30
|
|
|
Return the previous (index, data) pair in the database.
|
2008-02-23 14:08:55 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
Like the first() method, the data return is in the format of the
|
|
|
|
serialized format stored in the database, not in the more usable class
|
|
|
|
object. The data should be converted to a class using the class's
|
|
|
|
unserialize method.
|
2009-08-07 14:41:13 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
If no data is available, None is returned.
|
2009-08-07 14:41:13 +05:30
|
|
|
"""
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
data = self.cursor.prev(*args, **kwargs)
|
|
|
|
if data:
|
|
|
|
return (data[0], pickle.loads(data[1]))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def last(self, *args, **kwargs):
|
2009-06-12 21:47:07 +05:30
|
|
|
"""
|
2009-08-19 22:44:32 +05:30
|
|
|
Return the last (index, data) pair in the database.
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
Like the first() method, the data return is in the format of the
|
|
|
|
serialized format stored in the database, not in the more usable class
|
|
|
|
object. The data should be converted to a class using the class's
|
|
|
|
unserialize method.
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
None is returned when no more data is available.
|
2009-06-12 21:47:07 +05:30
|
|
|
"""
|
2009-08-19 22:44:32 +05:30
|
|
|
|
|
|
|
data = self.cursor.last(*args, **kwargs)
|
|
|
|
if data:
|
|
|
|
return (data[0], pickle.loads(data[1]))
|
|
|
|
return None
|