* 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
This commit is contained in:
Alex Roitman 2006-05-11 06:16:45 +00:00
parent ae59d17a60
commit e7971c78d4
5 changed files with 38 additions and 61 deletions

View File

@ -1,6 +1,11 @@
2006-05-10 Alex Roitman <shura@gramps-project.org> 2006-05-10 Alex Roitman <shura@gramps-project.org>
* src/GrampsDb/_ReadXML.py (GrampsParser): Use UpdateCallback.
* src/GrampsDb/_ReadGedcom.py (GedcomParser): Use UpdateCallback.
* src/GrampsDb/_DbUtils.py (db_copy): Use UpdateCallback class. * src/GrampsDb/_DbUtils.py (db_copy): Use UpdateCallback class.
(get_total): Add function, remove unneeded class.
* src/BasicUtils.py: Add module. * 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/Makefile.am (gdir_PYTHON): Add new file.
* src/DataViews/_MediaView.py (edit): Handle the exception. * src/DataViews/_MediaView.py (edit): Handle the exception.
* src/plugins/BookReport.py (__init__): Fix url. * src/plugins/BookReport.py (__init__): Fix url.

View File

@ -60,20 +60,21 @@ class UpdateCallback:
self.count = 0 self.count = 0
self.oldval = 0 self.oldval = 0
self.oldtime = 0 self.oldtime = 0
self.total = self.get_total()
self.interval = interval self.interval = interval
else: else:
self.update = self.update_empty self.update = self.update_empty
def get_total(self): def set_total(self,total):
assert False, "Needs to be defined in the derived class" self.total = total
def update_empty(self): def update_empty(self):
pass pass
def update_real(self): def update_real(self,count=None):
self.count += 1 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() newtime = time.time()
time_has_come = self.interval and (newtime-self.oldtime>self.interval) time_has_come = self.interval and (newtime-self.oldtime>self.interval)
value_changed = newval!=self.oldval value_changed = newval!=self.oldval

View File

@ -146,26 +146,21 @@ def add_child_to_family(db, family, child,
db.transaction_commit(trans, _('Add child to family') ) db.transaction_commit(trans, _('Add child to family') )
class DbUpdateCallback(BasicUtils.UpdateCallback): def get_total(db):
def __init__(self,db,callback,interval=1): person_len = db.get_number_of_people()
self.db = db family_len = db.get_number_of_families()
BasicUtils.UpdateCallback.__init__(self,callback,interval) event_len = db.get_number_of_events()
source_len = db.get_number_of_sources()
def get_total(self): place_len = db.get_number_of_places()
person_len = self.db.get_number_of_people() repo_len = db.get_number_of_repositories()
family_len = self.db.get_number_of_families() obj_len = db.get_number_of_media_objects()
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()
return person_len + family_len + event_len + \ return person_len + family_len + event_len + \
place_len + source_len + obj_len + repo_len place_len + source_len + obj_len + repo_len
def db_copy(from_db,to_db,callback): 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 = { primary_tables = {
'Person': {'cursor_func': from_db.get_person_cursor, 'Person': {'cursor_func': from_db.get_person_cursor,

View File

@ -76,6 +76,7 @@ from _GedcomInfo import *
from _GedTokens import * from _GedTokens import *
from QuestionDialog import ErrorDialog, WarningDialog from QuestionDialog import ErrorDialog, WarningDialog
from _GrampsDbBase import EVENT_KEY from _GrampsDbBase import EVENT_KEY
from BasicUtils import UpdateCallback
addr_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)\s*(.*)') addr_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)\s*(.*)')
addr2_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)') addr2_re = re.compile('(.+)([\n\r]+)(.+)\s*,(.+)\s+(\d+)')
@ -489,18 +490,16 @@ class Reader:
# #
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class GedcomParser: class GedcomParser(UpdateCallback):
SyntaxError = "Syntax Error" SyntaxError = "Syntax Error"
BadFile = "Not a GEDCOM file" BadFile = "Not a GEDCOM file"
def __init__(self,dbase,filename,callback,codeset,note_map,lines,people): 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.maxpeople = people
self.interval = lines/100
self.percent = 0
self.callback = callback
self.dp = GedcomDateParser() self.dp = GedcomDateParser()
self.db = dbase self.db = dbase
self.emapper = IdFinder(dbase.get_gramps_ids(EVENT_KEY), self.emapper = IdFinder(dbase.get_gramps_ids(EVENT_KEY),
@ -668,19 +667,10 @@ class GedcomParser:
else: else:
return (0,tries) 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): def get_next(self):
if self.backoff == False: if self.backoff == False:
self.groups = self.lexer.read() self.groups = self.lexer.read()
self.track_lines() self.update()
# EOF ? # EOF ?
if not self.groups: if not self.groups:

View File

@ -62,6 +62,7 @@ import NameDisplay
from _GrampsDbBase import \ from _GrampsDbBase import \
PERSON_KEY,FAMILY_KEY,SOURCE_KEY,EVENT_KEY,\ PERSON_KEY,FAMILY_KEY,SOURCE_KEY,EVENT_KEY,\
MEDIA_KEY,PLACE_KEY,REPOSITORY_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 # Gramps database parsing class. Derived from SAX XML parser
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class GrampsParser: class GrampsParser(UpdateCallback):
def __init__(self,database,callback,base,change,filename): def __init__(self,database,callback,base,change,filename):
UpdateCallback.__init__(self,callback)
self.filename = filename self.filename = filename
self.stext_list = [] self.stext_list = []
self.scomments_list = [] self.scomments_list = []
self.note_list = [] self.note_list = []
self.oldval = 0
self.tlist = [] self.tlist = []
self.conf = 2 self.conf = 2
self.gid2id = {} self.gid2id = {}
@ -336,12 +337,6 @@ class GrampsParser:
self.lmap = {} self.lmap = {}
self.media_file_map = {} 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.event = None
self.eventref = None self.eventref = None
self.childref = None self.childref = None
@ -611,7 +606,7 @@ class GrampsParser:
else: else:
no_magic = False no_magic = False
self.trans = self.db.transaction_begin("",batch=True,no_magic=no_magic) self.trans = self.db.transaction_begin("",batch=True,no_magic=no_magic)
self.linecount = linecount self.set_total(linecount)
self.db.disable_signals() self.db.disable_signals()
@ -694,7 +689,7 @@ class GrampsParser:
# GRAMPS LEGACY: title in the placeobj tag # GRAMPS LEGACY: title in the placeobj tag
self.placeobj.title = attrs.get('title','') self.placeobj.title = attrs.get('title','')
self.locations = 0 self.locations = 0
self.update() self.update(self.p.CurrentLineNumber)
def start_location(self,attrs): def start_location(self,attrs):
"""Bypass the function calls for this one, since it appears to """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) self.db.add_event(self.event,self.trans)
else: else:
# This is new event, with ID and handle already existing # This is new event, with ID and handle already existing
self.update() self.update(self.p.CurrentLineNumber)
gramps_id = self.map_eid(attrs["id"]) gramps_id = self.map_eid(attrs["id"])
try: try:
self.event = self.db.find_event_from_handle( self.event = self.db.find_event_from_handle(
@ -820,7 +815,7 @@ class GrampsParser:
self.db.bookmarks.append(handle) self.db.bookmarks.append(handle)
def start_person(self,attrs): def start_person(self,attrs):
self.update() self.update(self.p.CurrentLineNumber)
new_id = self.map_gid(attrs['id']) new_id = self.map_gid(attrs['id'])
try: try:
self.person = self.db.find_person_from_handle( self.person = self.db.find_person_from_handle(
@ -916,7 +911,7 @@ class GrampsParser:
self.repo.add_url(url) self.repo.add_url(url)
def start_family(self,attrs): def start_family(self,attrs):
self.update() self.update(self.p.CurrentLineNumber)
gramps_id = self.map_fid(attrs["id"]) gramps_id = self.map_fid(attrs["id"])
try: try:
self.family = self.db.find_family_from_handle( self.family = self.db.find_family_from_handle(
@ -1042,7 +1037,7 @@ class GrampsParser:
self.person.add_source_reference(self.source_ref) self.person.add_source_reference(self.source_ref)
def start_source(self,attrs): def start_source(self,attrs):
self.update() self.update(self.p.CurrentLineNumber)
gramps_id = self.map_sid(attrs["id"]) gramps_id = self.map_sid(attrs["id"])
try: try:
self.source = self.db.find_source_from_handle( self.source = self.db.find_source_from_handle(
@ -1123,7 +1118,7 @@ class GrampsParser:
pass pass
def stop_database(self,*tag): def stop_database(self,*tag):
self.update() self.update(self.p.CurrentLineNumber)
def stop_object(self,*tag): def stop_object(self,*tag):
self.db.commit_media_object(self.object,self.trans,self.change) self.db.commit_media_object(self.object,self.trans,self.change)
@ -1721,15 +1716,6 @@ class GrampsParser:
if self.func: if self.func:
self.tlist.append(data) 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): def append_value(orig,val):
if orig: if orig: