From b4b9675feb2ee0afc3fdecf260db3a4819d603d0 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Wed, 19 Aug 2009 17:14:32 +0000 Subject: [PATCH] cursor.py -- add additional methods mirroring those in DBCursor and a getattr method to pass through non-overridden method calls to DBCursor svn: r13079 --- src/gen/db/cursor.py | 150 +++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 69 deletions(-) diff --git a/src/gen/db/cursor.py b/src/gen/db/cursor.py index 92af2f42b..4e1502469 100644 --- a/src/gen/db/cursor.py +++ b/src/gen/db/cursor.py @@ -51,77 +51,12 @@ class GrampsCursor(object): """ self.cursor = self.source = None - def first(self): + def __getattr__(self, name): """ - 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. + Return a method from the underlying cursor object, if it exists + """ + return getattr(self.cursor, name) - If no data is available, None is returned. - """ - - data = self.cursor.first() - if data: - return (data[0], pickle.loads(data[1])) - return None - - def next(self): - """ - 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. - - None is returned when no more data is available. - """ - - data = self.cursor.next() - if data: - return (data[0], pickle.loads(data[1])) - return None - - def delete(self): - """ - Delete the data at the current cursor position - """ - self.cursor.delete() - - def close(self): - """ - Close the cursor. - - This should be called when the user is finished using the cursor, - freeing up the cursor's resources. - """ - self.cursor.close() - - def get_length(self): - """ - Return the number of records in the table referenced by the cursor. - """ - return self.source.stat()['ndata'] - - def __len__(self): - """ - Convienence method to work with len(cursor). - """ - return self.get_length() - - def __iter__(self): - """ - Iterator - """ - - data = self.first() - while data: - yield data - data = self.next() - def __enter__(self): """ Context manager enter method @@ -134,4 +69,81 @@ class GrampsCursor(object): """ 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): + """ + 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. + + If no data is available, None is returned. + """ + + data = self.cursor.first(*args, **kwargs) + if data: + return (data[0], pickle.loads(data[1])) + return None + + def next(self, *args, **kwargs): + """ + 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. + + None is returned when no more data is available. + """ + + data = self.cursor.next(*args, **kwargs) + if data: + return (data[0], pickle.loads(data[1])) + return None + + def prev(self, *args, **kwargs): + """ + Return the previous (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. + + If no data is available, None is returned. + """ + + data = self.cursor.prev(*args, **kwargs) + if data: + return (data[0], pickle.loads(data[1])) + return None + + def last(self, *args, **kwargs): + """ + Return the last (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. + + None is returned when no more data is available. + """ + + data = self.cursor.last(*args, **kwargs) + if data: + return (data[0], pickle.loads(data[1])) + return None