From 85c2ff5039487b520eb60ffc26b5428800fb1e03 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Thu, 11 May 2006 06:16:45 +0000 Subject: [PATCH] * src/GrampsDb/_ReadXML.py (GrampsParser): Use UpdateCallback. * src/GrampsDb/_ReadGedcom.py (GedcomParser): Use UpdateCallback. (get_total): Add function, remove unneeded class. (UpdateCallback.update_real): Allow optional argument; (UpdateCallback.set_total): Add method. svn: r6618 --- ChangeLog | 5 +++++ src/BasicUtils.py | 11 ++++++----- src/GrampsDb/_DbUtils.py | 29 ++++++++++++----------------- src/GrampsDb/_ReadGedcom.py | 20 +++++--------------- src/GrampsDb/_ReadXML.py | 34 ++++++++++------------------------ 5 files changed, 38 insertions(+), 61 deletions(-) diff --git a/ChangeLog b/ChangeLog index f59ba1573..afc7d12d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,11 @@ 2006-05-10 Alex Roitman + * src/GrampsDb/_ReadXML.py (GrampsParser): Use UpdateCallback. + * src/GrampsDb/_ReadGedcom.py (GedcomParser): Use UpdateCallback. * src/GrampsDb/_DbUtils.py (db_copy): Use UpdateCallback class. + (get_total): Add function, remove unneeded class. * src/BasicUtils.py: Add module. + (UpdateCallback.update_real): Allow optional argument; + (UpdateCallback.set_total): Add method. * src/Makefile.am (gdir_PYTHON): Add new file. * src/DataViews/_MediaView.py (edit): Handle the exception. * src/plugins/BookReport.py (__init__): Fix url. diff --git a/src/BasicUtils.py b/src/BasicUtils.py index 661dabfa7..43f330caa 100644 --- a/src/BasicUtils.py +++ b/src/BasicUtils.py @@ -60,20 +60,21 @@ class UpdateCallback: self.count = 0 self.oldval = 0 self.oldtime = 0 - self.total = self.get_total() self.interval = interval else: self.update = self.update_empty - def get_total(self): - assert False, "Needs to be defined in the derived class" + def set_total(self,total): + self.total = total def update_empty(self): pass - def update_real(self): + def update_real(self,count=None): self.count += 1 - newval = int(100.0*self.count/self.total) + if not count: + count = self.count + newval = int(100*count/self.total) newtime = time.time() time_has_come = self.interval and (newtime-self.oldtime>self.interval) value_changed = newval!=self.oldval diff --git a/src/GrampsDb/_DbUtils.py b/src/GrampsDb/_DbUtils.py index d6b980fd2..665d3a3ce 100644 --- a/src/GrampsDb/_DbUtils.py +++ b/src/GrampsDb/_DbUtils.py @@ -146,26 +146,21 @@ def add_child_to_family(db, family, child, db.transaction_commit(trans, _('Add child to family') ) -class DbUpdateCallback(BasicUtils.UpdateCallback): - def __init__(self,db,callback,interval=1): - self.db = db - BasicUtils.UpdateCallback.__init__(self,callback,interval) - - def get_total(self): - person_len = self.db.get_number_of_people() - family_len = self.db.get_number_of_families() - event_len = self.db.get_number_of_events() - source_len = self.db.get_number_of_sources() - place_len = self.db.get_number_of_places() - repo_len = self.db.get_number_of_repositories() - obj_len = self.db.get_number_of_media_objects() +def get_total(db): + person_len = db.get_number_of_people() + family_len = db.get_number_of_families() + event_len = db.get_number_of_events() + source_len = db.get_number_of_sources() + place_len = db.get_number_of_places() + repo_len = db.get_number_of_repositories() + obj_len = db.get_number_of_media_objects() - return person_len + family_len + event_len + \ - place_len + source_len + obj_len + repo_len + return person_len + family_len + event_len + \ + place_len + source_len + obj_len + repo_len - def db_copy(from_db,to_db,callback): - uc = DbUpdateCallback(from_db,callback) + uc = UpdateCallback(from_db,callback) + uc.set_total(get_total(from_db)) primary_tables = { 'Person': {'cursor_func': from_db.get_person_cursor, diff --git a/src/GrampsDb/_ReadGedcom.py b/src/GrampsDb/_ReadGedcom.py index 86a267b9f..03cdfe872 100644 --- a/src/GrampsDb/_ReadGedcom.py +++ b/src/GrampsDb/_ReadGedcom.py @@ -76,6 +76,7 @@ from _GedcomInfo import * from _GedTokens import * from QuestionDialog import ErrorDialog, WarningDialog from _GrampsDbBase import EVENT_KEY +from BasicUtils import UpdateCallback addr_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)\s*(.*)') addr2_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)') @@ -489,18 +490,16 @@ class Reader: # # #------------------------------------------------------------------------- -class GedcomParser: +class GedcomParser(UpdateCallback): SyntaxError = "Syntax Error" BadFile = "Not a GEDCOM file" def __init__(self,dbase,filename,callback,codeset,note_map,lines,people): + UpdateCallback.__init__(self,callback) + self.set_total(lines) - self.maxlines = lines self.maxpeople = people - self.interval = lines/100 - self.percent = 0 - self.callback = callback self.dp = GedcomDateParser() self.db = dbase self.emapper = IdFinder(dbase.get_gramps_ids(EVENT_KEY), @@ -668,19 +667,10 @@ class GedcomParser: else: return (0,tries) - def track_lines(self): - if self.current == 1: - self.current = self.interval - self.percent += 1 - if self.callback: - self.callback(self.percent) - else: - self.current -= 1 - def get_next(self): if self.backoff == False: self.groups = self.lexer.read() - self.track_lines() + self.update() # EOF ? if not self.groups: diff --git a/src/GrampsDb/_ReadXML.py b/src/GrampsDb/_ReadXML.py index e79258386..2d7a6cca4 100644 --- a/src/GrampsDb/_ReadXML.py +++ b/src/GrampsDb/_ReadXML.py @@ -62,6 +62,7 @@ import NameDisplay from _GrampsDbBase import \ PERSON_KEY,FAMILY_KEY,SOURCE_KEY,EVENT_KEY,\ MEDIA_KEY,PLACE_KEY,REPOSITORY_KEY +from BasicUtils import UpdateCallback #------------------------------------------------------------------------- # @@ -268,14 +269,14 @@ class LineParser: # Gramps database parsing class. Derived from SAX XML parser # #------------------------------------------------------------------------- -class GrampsParser: +class GrampsParser(UpdateCallback): def __init__(self,database,callback,base,change,filename): + UpdateCallback.__init__(self,callback) self.filename = filename self.stext_list = [] self.scomments_list = [] self.note_list = [] - self.oldval = 0 self.tlist = [] self.conf = 2 self.gid2id = {} @@ -336,12 +337,6 @@ class GrampsParser: self.lmap = {} self.media_file_map = {} - self.callback = callback - if '__call__' in dir(self.callback): # callback is really callable - self.update = self.update_real - else: - self.update = self.update_empty - self.increment = 100 self.event = None self.eventref = None self.childref = None @@ -611,7 +606,7 @@ class GrampsParser: else: no_magic = False self.trans = self.db.transaction_begin("",batch=True,no_magic=no_magic) - self.linecount = linecount + self.set_total(linecount) self.db.disable_signals() @@ -694,7 +689,7 @@ class GrampsParser: # GRAMPS LEGACY: title in the placeobj tag self.placeobj.title = attrs.get('title','') self.locations = 0 - self.update() + self.update(self.p.CurrentLineNumber) def start_location(self,attrs): """Bypass the function calls for this one, since it appears to @@ -752,7 +747,7 @@ class GrampsParser: self.db.add_event(self.event,self.trans) else: # This is new event, with ID and handle already existing - self.update() + self.update(self.p.CurrentLineNumber) gramps_id = self.map_eid(attrs["id"]) try: self.event = self.db.find_event_from_handle( @@ -820,7 +815,7 @@ class GrampsParser: self.db.bookmarks.append(handle) def start_person(self,attrs): - self.update() + self.update(self.p.CurrentLineNumber) new_id = self.map_gid(attrs['id']) try: self.person = self.db.find_person_from_handle( @@ -916,7 +911,7 @@ class GrampsParser: self.repo.add_url(url) def start_family(self,attrs): - self.update() + self.update(self.p.CurrentLineNumber) gramps_id = self.map_fid(attrs["id"]) try: self.family = self.db.find_family_from_handle( @@ -1042,7 +1037,7 @@ class GrampsParser: self.person.add_source_reference(self.source_ref) def start_source(self,attrs): - self.update() + self.update(self.p.CurrentLineNumber) gramps_id = self.map_sid(attrs["id"]) try: self.source = self.db.find_source_from_handle( @@ -1123,7 +1118,7 @@ class GrampsParser: pass def stop_database(self,*tag): - self.update() + self.update(self.p.CurrentLineNumber) def stop_object(self,*tag): self.db.commit_media_object(self.object,self.trans,self.change) @@ -1721,15 +1716,6 @@ class GrampsParser: if self.func: self.tlist.append(data) - def update_empty(self): - pass - - def update_real(self): - line = self.p.CurrentLineNumber - newval = int(100*line/self.linecount) - if newval != self.oldval: - self.callback(newval) - self.oldval = newval def append_value(orig,val): if orig: