Raise HandleError exception for bad handles

This commit is contained in:
Nick Hall 2015-12-05 21:17:56 +00:00
parent 5d7343f10a
commit 5c958bd7fb
16 changed files with 88 additions and 60 deletions

View File

@ -522,9 +522,13 @@ def preset_name(basepers, name, sibling=False):
def family_name(family, db, noname=_("unknown")): def family_name(family, db, noname=_("unknown")):
"""Builds a name for the family from the parents names""" """Builds a name for the family from the parents names"""
father = None
mother = None
father_handle = family.get_father_handle() father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle() mother_handle = family.get_mother_handle()
if father_handle:
father = db.get_person_from_handle(father_handle) father = db.get_person_from_handle(father_handle)
if mother_handle:
mother = db.get_person_from_handle(mother_handle) mother = db.get_person_from_handle(mother_handle)
if father and mother: if father and mother:
fname = name_displayer.display(father) fname = name_displayer.display(father)

View File

@ -120,6 +120,7 @@ class FormattingHelper(object):
""" Obtain a place name """ Obtain a place name
""" """
text = "" text = ""
if place_handle:
place = self.dbstate.db.get_place_from_handle(place_handle) place = self.dbstate.db.get_place_from_handle(place_handle)
if place: if place:
place_title = place_displayer.display(self.dbstate.db, place) place_title = place_displayer.display(self.dbstate.db, place)

View File

@ -574,7 +574,10 @@ class DisplayState(Callback):
self.status.pop(self.status_id) self.status.pop(self.status_id)
if active_handle:
name, obj = navigation_label(dbstate.db, nav_type, active_handle) name, obj = navigation_label(dbstate.db, nav_type, active_handle)
else:
name = _('No active object')
# Append relationship to default person if funtionality is enabled. # Append relationship to default person if funtionality is enabled.
if nav_type == 'Person' and active_handle \ if nav_type == 'Person' and active_handle \

View File

@ -347,6 +347,8 @@ class EditPrimary(ManagedWindow, DbGUIElement):
Return True if a duplicate GRAMPS ID has been detected. Return True if a duplicate GRAMPS ID has been detected.
""" """
original = None
if self.obj.get_handle():
original = self.get_from_handle(self.obj.get_handle()) original = self.get_from_handle(self.obj.get_handle())
if original and original.get_gramps_id() == self.obj.get_gramps_id(): if original and original.get_gramps_id() == self.obj.get_gramps_id():
return (False, 0) return (False, 0)

View File

@ -199,8 +199,6 @@ class BaseSelector(ManagedWindow):
id_list = self.get_selected_ids() id_list = self.get_selected_ids()
if id_list and id_list[0]: if id_list and id_list[0]:
result = self.get_from_handle_func()(id_list[0]) result = self.get_from_handle_func()(id_list[0])
if result is None and self.get_from_handle_func2:
result = self.get_from_handle_func2()(id_list[0])
self.close() self.close()
elif val != Gtk.ResponseType.DELETE_EVENT: elif val != Gtk.ResponseType.DELETE_EVENT:
self.close() self.close()
@ -233,9 +231,6 @@ class BaseSelector(ManagedWindow):
def get_from_handle_func(self): def get_from_handle_func(self):
assert False, "Must be defined in the subclass" assert False, "Must be defined in the subclass"
def get_from_handle_func2(self):
return None
def set_show_search_bar(self, value): def set_show_search_bar(self, value):
"""make the search bar at the top shown """make the search bar at the top shown
""" """

View File

@ -78,7 +78,10 @@ class SelectCitation(BaseSelector):
] ]
def get_from_handle_func(self): def get_from_handle_func(self):
return self.db.get_source_from_handle return self.get_source_or_citation
def get_from_handle_func2(self): def get_source_or_citation(self, handle):
return self.db.get_citation_from_handle if self.db.has_source_handle(handle):
return self.db.get_source_from_handle(handle)
else:
return self.db.get_citation_from_handle(handle)

View File

@ -1067,9 +1067,12 @@ class FanChartWidget(FanChartBaseWidget):
def _fill_data_structures(self): def _fill_data_structures(self):
self.set_generations() self.set_generations()
if not self.rootpersonh:
return
person = self.dbstate.db.get_person_from_handle(self.rootpersonh) person = self.dbstate.db.get_person_from_handle(self.rootpersonh)
if not person: if not person:
name = None #nothing to do, just return
return
else: else:
name = name_displayer.display(person) name = name_displayer.display(person)
parents = self._have_parents(person) parents = self._have_parents(person)

View File

@ -167,6 +167,8 @@ class FanChartDescWidget(FanChartBaseWidget):
def _fill_data_structures(self): def _fill_data_structures(self):
self.set_generations() self.set_generations()
if not self.rootpersonh:
return
person = self.dbstate.db.get_person_from_handle(self.rootpersonh) person = self.dbstate.db.get_person_from_handle(self.rootpersonh)
if not person: if not person:
#nothing to do, just return #nothing to do, just return

View File

@ -71,7 +71,7 @@ from gramps.gen.utils.callback import Callback
from . import BsddbBaseCursor from . import BsddbBaseCursor
from gramps.gen.db.base import DbReadBase from gramps.gen.db.base import DbReadBase
from gramps.gen.utils.id import create_id from gramps.gen.utils.id import create_id
from gramps.gen.errors import DbError from gramps.gen.errors import DbError, HandleError
from gramps.gen.constfunc import get_env_var from gramps.gen.constfunc import get_env_var
from gramps.gen.const import GRAMPS_LOCALE as glocale from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
@ -683,7 +683,7 @@ class DbBsddbRead(DbReadBase, Callback):
newobj = class_type() newobj = class_type()
newobj.unserialize(data) newobj.unserialize(data)
return newobj return newobj
return None raise HandleError('Handle %s not found' % handle.decode('utf-8'))
def get_from_name_and_handle(self, table_name, handle): def get_from_name_and_handle(self, table_name, handle):
""" """

View File

@ -74,7 +74,7 @@ from gramps.gen.db.dbconst import *
from gramps.gen.utils.callback import Callback from gramps.gen.utils.callback import Callback
from gramps.gen.utils.id import create_id from gramps.gen.utils.id import create_id
from gramps.gen.updatecallback import UpdateCallback from gramps.gen.updatecallback import UpdateCallback
from gramps.gen.errors import DbError from gramps.gen.errors import DbError, HandleError
from gramps.gen.constfunc import win, get_env_var from gramps.gen.constfunc import win, get_env_var
from gramps.gen.const import HOME_DIR, GRAMPS_LOCALE as glocale from gramps.gen.const import HOME_DIR, GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
@ -2105,19 +2105,16 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
def get_from_handle(self, handle, class_type, data_map): def get_from_handle(self, handle, class_type, data_map):
if isinstance(handle, str): if isinstance(handle, str):
handle = handle.encode('utf-8') handle = handle.encode('utf-8')
try: if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = data_map.get(handle, txn=self.txn) data = data_map.get(handle, txn=self.txn)
except:
data = None
# under certain circumstances during a database reload,
# data_map can be none. If so, then don't report an error
if data_map:
_LOG.error("Failed to get from handle", exc_info=True)
if data: if data:
newobj = class_type() newobj = class_type()
newobj.unserialize(data) newobj.unserialize(data)
return newobj return newobj
return None raise HandleError('Handle %s not found' % handle.decode('utf-8'))
@catch_db_error @catch_db_error
def transaction_begin(self, transaction): def transaction_begin(self, transaction):

View File

@ -152,11 +152,14 @@ class Ancestor(Gramplet):
tooltip, person_handle], node=parent_id) tooltip, person_handle], node=parent_id)
family_handle = person.get_main_parents_family_handle() family_handle = person.get_main_parents_family_handle()
if family_handle:
family = self.dbstate.db.get_family_from_handle(family_handle) family = self.dbstate.db.get_family_from_handle(family_handle)
if family: if family:
if family.get_father_handle(): father_handle = family.get_father_handle()
self.add_to_tree(depth + 1, item_id, family.get_father_handle()) if father_handle:
if family.get_mother_handle(): self.add_to_tree(depth + 1, item_id, father_handle)
self.add_to_tree(depth + 1, item_id, family.get_mother_handle()) mother_handle = family.get_mother_handle()
if mother_handle:
self.add_to_tree(depth + 1, item_id, mother_handle)
return item_id return item_id

View File

@ -105,6 +105,7 @@ class Citations(Gramplet, DbGUIElement):
self.add_attribute_citations(event) self.add_attribute_citations(event)
self.add_mediaref_citations(event) self.add_mediaref_citations(event)
place_handle = event.get_place_handle() place_handle = event.get_place_handle()
if place_handle:
place = self.dbstate.db.get_place_from_handle(place_handle) place = self.dbstate.db.get_place_from_handle(place_handle)
if place: if place:
self.add_place_citations(place) self.add_place_citations(place)
@ -202,6 +203,7 @@ class Citations(Gramplet, DbGUIElement):
if self.check_mediaref_citations(event): if self.check_mediaref_citations(event):
return True return True
place_handle = event.get_place_handle() place_handle = event.get_place_handle()
if place_handle:
place = self.dbstate.db.get_place_from_handle(place_handle) place = self.dbstate.db.get_place_from_handle(place_handle)
if place and self.check_place_citations(place): if place and self.check_place_citations(place):
return True return True

View File

@ -170,9 +170,11 @@ class RelCalc(tool.Tool, ManagedWindow):
if not iter_: if not iter_:
return return
other_person = None
handle = model.get_handle_from_iter(iter_) handle = model.get_handle_from_iter(iter_)
if handle:
other_person = self.db.get_person_from_handle(handle) other_person = self.db.get_person_from_handle(handle)
if other_person is None : if other_person is None:
self.textbuffer.set_text("") self.textbuffer.set_text("")
return return

View File

@ -312,12 +312,15 @@ class GeoClose(GeoGraphyView):
information. information.
""" """
active = self.get_active() active = self.get_active()
if active:
person = self.dbstate.db.get_person_from_handle(active) person = self.dbstate.db.get_person_from_handle(active)
self.lifeway_layer.clear_ways() self.lifeway_layer.clear_ways()
if person is None: if person is None:
self.goto_handle(None) self.goto_handle(None)
else: else:
self.goto_handle(handle=person) self.goto_handle(handle=person)
else:
self.goto_handle(None)
def draw(self, menu, marks, color, reference): def draw(self, menu, marks, color, reference):
""" """

View File

@ -300,8 +300,14 @@ class GeoFamClose(GeoGraphyView):
information. information.
""" """
active = self.get_active() active = self.get_active()
if active:
family = self.dbstate.db.get_family_from_handle(active) family = self.dbstate.db.get_family_from_handle(active)
if family is None:
self.goto_handle(None)
else:
self.goto_handle(handle=family) self.goto_handle(handle=family)
else:
self.goto_handle(None)
def draw(self, menu, marks, color, reference): def draw(self, menu, marks, color, reference):
""" """

View File

@ -355,20 +355,24 @@ class GeoFamily(GeoGraphyView):
} }
self._createpersonmarkers(dbstate, person, comment, family_id) self._createpersonmarkers(dbstate, person, comment, family_id)
def _createmap(self, family_x): def _createmap(self, handle):
""" """
Create all markers for each people's event in the database which has Create all markers for each people's event in the database which has
a lat/lon. a lat/lon.
""" """
if not handle:
return
self.place_list = [] self.place_list = []
self.place_without_coordinates = [] self.place_without_coordinates = []
self.minlat = self.maxlat = self.minlon = self.maxlon = 0.0 self.minlat = self.maxlat = self.minlon = self.maxlon = 0.0
self.minyear = 9999 self.minyear = 9999
self.maxyear = 0 self.maxyear = 0
self.message_layer.clear_messages() self.message_layer.clear_messages()
family = self.dbstate.db.get_family_from_handle(family_x) if self.dbstate.db.has_family_handle(handle):
if family is None: family = self.dbstate.db.get_family_from_handle(handle)
person = self.dbstate.db.get_person_from_handle(self.uistate.get_active('Person')) self._createmap_for_one_family(family)
else:
person = self.dbstate.db.get_person_from_handle(handle)
if not person: if not person:
return return
family_list = person.get_family_handle_list() family_list = person.get_family_handle_list()
@ -376,8 +380,6 @@ class GeoFamily(GeoGraphyView):
family = self.dbstate.db.get_family_from_handle(family_hdl) family = self.dbstate.db.get_family_from_handle(family_hdl)
if family is not None: if family is not None:
self._createmap_for_one_family(family) self._createmap_for_one_family(family)
else:
self._createmap_for_one_family(family)
self.sort = sorted(self.place_list, self.sort = sorted(self.place_list,
key=operator.itemgetter(3, 4, 6) key=operator.itemgetter(3, 4, 6)
) )