From 3a69a0145e78ec8c303b3c95b870856180763aaf Mon Sep 17 00:00:00 2001 From: Richard Taylor Date: Sun, 4 Feb 2007 21:20:46 +0000 Subject: [PATCH] add initial cursor iter impl. svn: r8052 --- ChangeLog | 4 +++ src/GrampsDb/_CursorIterator.py | 47 +++++++++++++++++++++++++++++++++ src/GrampsDb/_GrampsDbBase.py | 25 ++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/GrampsDb/_CursorIterator.py diff --git a/ChangeLog b/ChangeLog index 406213320..3c04a36f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2007-02-04 Richard Taylor + * src/GrampsDb/_GrampsDbBase.py: add initial cursor iter impl. + * src/GrampsDb/_CursorIterator.py: add initial cursor iter impl. + 2007-02-04 Zsolt Foldvari * src/GrampsLocale: set svn:ignore propery * src/BasicUtils: set svn:ignore propery diff --git a/src/GrampsDb/_CursorIterator.py b/src/GrampsDb/_CursorIterator.py new file mode 100644 index 000000000..b15b558a8 --- /dev/null +++ b/src/GrampsDb/_CursorIterator.py @@ -0,0 +1,47 @@ + +class LongOpStatus(object): + def __init__(self): + self._cancel = False + + def cancel(self): + self._cancel = True + + def shouldCancel(self): + return self._cancel + +class CursorIterator(object): + + def __init__(self,db,cursor): + self._db = db + self._cursor = cursor + self._status = LongOpStatus() + + def __iter__(self): + try: + # Emit start signal + self._db.emit('long-op-start',(self._status,)) + + first = self._cursor.first() + if first: + yield first + + next = self._cursor.next() + while next: + yield next + + # check for cancel + #if self._status.shouldCancel(): + # raise GrampsDbUserCancel + + # emit heartbeat + self._db.emit('long-op-heartbeat') + next = self._cursor.next() + + # emit stop signal + self._db.emit('long-op-end') + self._cursor.close() + raise StopIteration + except: + self._cursor.close() + self._db.emit('long-op-end') + raise diff --git a/src/GrampsDb/_GrampsDbBase.py b/src/GrampsDb/_GrampsDbBase.py index fe6b9776b..786bcbe1f 100644 --- a/src/GrampsDb/_GrampsDbBase.py +++ b/src/GrampsDb/_GrampsDbBase.py @@ -52,6 +52,7 @@ log = logging.getLogger(".GrampsDb") #------------------------------------------------------------------------- from RelLib import * from _GrampsDBCallback import GrampsDBCallback +from _CursorIterator import CursorIterator #------------------------------------------------------------------------- # @@ -196,6 +197,9 @@ class GrampsDbBase(GrampsDBCallback): 'repository-update' : (list, ), 'repository-delete' : (list, ), 'repository-rebuild' : None, + 'long-op-start' : (object, ), + 'long-op-heartbeat' : None, + 'long-op-end' : None } @@ -342,24 +346,45 @@ class GrampsDbBase(GrampsDBCallback): def get_person_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_person_cursor_iter(self): + return CursorIterator(self,self.get_person_cursor()) + def get_family_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_family_cursor_iter(self): + return CursorIterator(self,self.get_family_cursor()) + def get_event_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_event_cursor_iter(self): + return CursorIterator(self,self.get_event_cursor()) + def get_place_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_place_cursor_iter(self): + return CursorIterator(self,self.get_place_cursor()) + def get_source_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_source_cursor_iter(self): + return CursorIterator(self,self.get_source_cursor()) + def get_media_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_media_cursor_iter(self): + return CursorIterator(self,self.get_media_cursor()) + def get_repository_cursor(self): assert False, "Needs to be overridden in the derived class" + def get_repository_cursor_iter(self): + return CursorIterator(self,self.get_repository_cursor()) + def open_undodb(self): if not self.readonly: self.undolog = "%s.undo" % self.full_name