From 5c958bd7fbaea082df6f2ce427042ad0a642815e Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sat, 5 Dec 2015 21:17:56 +0000 Subject: [PATCH] Raise HandleError exception for bad handles --- gramps/gen/utils/db.py | 8 ++++++-- gramps/gen/utils/libformatting.py | 17 +++++++++-------- gramps/gui/displaystate.py | 5 ++++- gramps/gui/editors/editprimary.py | 4 +++- gramps/gui/selectors/baseselector.py | 5 ----- gramps/gui/selectors/selectcitation.py | 9 ++++++--- gramps/gui/widgets/fanchart.py | 5 ++++- gramps/gui/widgets/fanchartdesc.py | 2 ++ gramps/plugins/database/bsddb_support/read.py | 4 ++-- gramps/plugins/database/bsddb_support/write.py | 17 +++++++---------- gramps/plugins/gramplet/ancestor.py | 15 +++++++++------ gramps/plugins/gramplet/citations.py | 14 ++++++++------ gramps/plugins/tool/relcalc.py | 6 ++++-- gramps/plugins/view/geoclose.py | 13 ++++++++----- gramps/plugins/view/geofamclose.py | 10 ++++++++-- gramps/plugins/view/geofamily.py | 14 ++++++++------ 16 files changed, 88 insertions(+), 60 deletions(-) diff --git a/gramps/gen/utils/db.py b/gramps/gen/utils/db.py index fcdcd443c..d20f98638 100644 --- a/gramps/gen/utils/db.py +++ b/gramps/gen/utils/db.py @@ -522,10 +522,14 @@ def preset_name(basepers, name, sibling=False): def family_name(family, db, noname=_("unknown")): """Builds a name for the family from the parents names""" + father = None + mother = None father_handle = family.get_father_handle() mother_handle = family.get_mother_handle() - father = db.get_person_from_handle(father_handle) - mother = db.get_person_from_handle(mother_handle) + if father_handle: + father = db.get_person_from_handle(father_handle) + if mother_handle: + mother = db.get_person_from_handle(mother_handle) if father and mother: fname = name_displayer.display(father) mname = name_displayer.display(mother) diff --git a/gramps/gen/utils/libformatting.py b/gramps/gen/utils/libformatting.py index b48a53d95..24658eb1d 100644 --- a/gramps/gen/utils/libformatting.py +++ b/gramps/gen/utils/libformatting.py @@ -120,14 +120,15 @@ class FormattingHelper(object): """ Obtain a place name """ text = "" - place = self.dbstate.db.get_place_from_handle(place_handle) - if place: - place_title = place_displayer.display(self.dbstate.db, place) - if place_title != "": - if len(place_title) > 25: - text = place_title[:24]+"..." - else: - text = place_title + if place_handle: + place = self.dbstate.db.get_place_from_handle(place_handle) + if place: + place_title = place_displayer.display(self.dbstate.db, place) + if place_title != "": + if len(place_title) > 25: + text = place_title[:24]+"..." + else: + text = place_title return text def format_person( self, person, line_count, use_markup=False): diff --git a/gramps/gui/displaystate.py b/gramps/gui/displaystate.py index f22b4ba31..69edab7b6 100644 --- a/gramps/gui/displaystate.py +++ b/gramps/gui/displaystate.py @@ -574,7 +574,10 @@ class DisplayState(Callback): self.status.pop(self.status_id) - name, obj = navigation_label(dbstate.db, nav_type, active_handle) + if 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. if nav_type == 'Person' and active_handle \ diff --git a/gramps/gui/editors/editprimary.py b/gramps/gui/editors/editprimary.py index e2c2d26f5..d5ec7b041 100644 --- a/gramps/gui/editors/editprimary.py +++ b/gramps/gui/editors/editprimary.py @@ -347,7 +347,9 @@ class EditPrimary(ManagedWindow, DbGUIElement): Return True if a duplicate GRAMPS ID has been detected. """ - original = self.get_from_handle(self.obj.get_handle()) + original = None + if 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(): return (False, 0) else: diff --git a/gramps/gui/selectors/baseselector.py b/gramps/gui/selectors/baseselector.py index 98ed71c79..24924e815 100644 --- a/gramps/gui/selectors/baseselector.py +++ b/gramps/gui/selectors/baseselector.py @@ -199,8 +199,6 @@ class BaseSelector(ManagedWindow): id_list = self.get_selected_ids() if id_list and 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() elif val != Gtk.ResponseType.DELETE_EVENT: self.close() @@ -233,9 +231,6 @@ class BaseSelector(ManagedWindow): def get_from_handle_func(self): assert False, "Must be defined in the subclass" - def get_from_handle_func2(self): - return None - def set_show_search_bar(self, value): """make the search bar at the top shown """ diff --git a/gramps/gui/selectors/selectcitation.py b/gramps/gui/selectors/selectcitation.py index b696a3ad3..94f3c25cb 100644 --- a/gramps/gui/selectors/selectcitation.py +++ b/gramps/gui/selectors/selectcitation.py @@ -78,7 +78,10 @@ class SelectCitation(BaseSelector): ] 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): - return self.db.get_citation_from_handle + def get_source_or_citation(self, 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) diff --git a/gramps/gui/widgets/fanchart.py b/gramps/gui/widgets/fanchart.py index 45cc2a47c..de9f0dc76 100644 --- a/gramps/gui/widgets/fanchart.py +++ b/gramps/gui/widgets/fanchart.py @@ -1067,9 +1067,12 @@ class FanChartWidget(FanChartBaseWidget): def _fill_data_structures(self): self.set_generations() + if not self.rootpersonh: + return person = self.dbstate.db.get_person_from_handle(self.rootpersonh) if not person: - name = None + #nothing to do, just return + return else: name = name_displayer.display(person) parents = self._have_parents(person) diff --git a/gramps/gui/widgets/fanchartdesc.py b/gramps/gui/widgets/fanchartdesc.py index b875ab4ac..bf8d14cba 100644 --- a/gramps/gui/widgets/fanchartdesc.py +++ b/gramps/gui/widgets/fanchartdesc.py @@ -167,6 +167,8 @@ class FanChartDescWidget(FanChartBaseWidget): def _fill_data_structures(self): self.set_generations() + if not self.rootpersonh: + return person = self.dbstate.db.get_person_from_handle(self.rootpersonh) if not person: #nothing to do, just return diff --git a/gramps/plugins/database/bsddb_support/read.py b/gramps/plugins/database/bsddb_support/read.py index 1332ff49c..3140ee01a 100644 --- a/gramps/plugins/database/bsddb_support/read.py +++ b/gramps/plugins/database/bsddb_support/read.py @@ -71,7 +71,7 @@ from gramps.gen.utils.callback import Callback from . import BsddbBaseCursor from gramps.gen.db.base import DbReadBase 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.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -683,7 +683,7 @@ class DbBsddbRead(DbReadBase, Callback): newobj = class_type() newobj.unserialize(data) return newobj - return None + raise HandleError('Handle %s not found' % handle.decode('utf-8')) def get_from_name_and_handle(self, table_name, handle): """ diff --git a/gramps/plugins/database/bsddb_support/write.py b/gramps/plugins/database/bsddb_support/write.py index 4283fcd5f..07921c52c 100644 --- a/gramps/plugins/database/bsddb_support/write.py +++ b/gramps/plugins/database/bsddb_support/write.py @@ -74,7 +74,7 @@ from gramps.gen.db.dbconst import * from gramps.gen.utils.callback import Callback from gramps.gen.utils.id import create_id 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.const import HOME_DIR, GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -2105,19 +2105,16 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback): def get_from_handle(self, handle, class_type, data_map): if isinstance(handle, str): handle = handle.encode('utf-8') - try: - 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 handle is None: + raise HandleError('Handle is None') + if not handle: + raise HandleError('Handle is empty') + data = data_map.get(handle, txn=self.txn) if data: newobj = class_type() newobj.unserialize(data) return newobj - return None + raise HandleError('Handle %s not found' % handle.decode('utf-8')) @catch_db_error def transaction_begin(self, transaction): diff --git a/gramps/plugins/gramplet/ancestor.py b/gramps/plugins/gramplet/ancestor.py index 9266093a6..8ddc57fd8 100644 --- a/gramps/plugins/gramplet/ancestor.py +++ b/gramps/plugins/gramplet/ancestor.py @@ -152,11 +152,14 @@ class Ancestor(Gramplet): tooltip, person_handle], node=parent_id) family_handle = person.get_main_parents_family_handle() - family = self.dbstate.db.get_family_from_handle(family_handle) - if family: - if family.get_father_handle(): - self.add_to_tree(depth + 1, item_id, family.get_father_handle()) - if family.get_mother_handle(): - self.add_to_tree(depth + 1, item_id, family.get_mother_handle()) + if family_handle: + family = self.dbstate.db.get_family_from_handle(family_handle) + if family: + father_handle = family.get_father_handle() + if father_handle: + self.add_to_tree(depth + 1, item_id, father_handle) + mother_handle = family.get_mother_handle() + if mother_handle: + self.add_to_tree(depth + 1, item_id, mother_handle) return item_id diff --git a/gramps/plugins/gramplet/citations.py b/gramps/plugins/gramplet/citations.py index f7d870cae..a46c049ae 100644 --- a/gramps/plugins/gramplet/citations.py +++ b/gramps/plugins/gramplet/citations.py @@ -105,9 +105,10 @@ class Citations(Gramplet, DbGUIElement): self.add_attribute_citations(event) self.add_mediaref_citations(event) place_handle = event.get_place_handle() - place = self.dbstate.db.get_place_from_handle(place_handle) - if place: - self.add_place_citations(place) + if place_handle: + place = self.dbstate.db.get_place_from_handle(place_handle) + if place: + self.add_place_citations(place) def add_place_citations(self, place): self.add_citations(place) @@ -202,9 +203,10 @@ class Citations(Gramplet, DbGUIElement): if self.check_mediaref_citations(event): return True place_handle = event.get_place_handle() - place = self.dbstate.db.get_place_from_handle(place_handle) - if place and self.check_place_citations(place): - return True + if place_handle: + place = self.dbstate.db.get_place_from_handle(place_handle) + if place and self.check_place_citations(place): + return True return False def check_place_citations(self, place): diff --git a/gramps/plugins/tool/relcalc.py b/gramps/plugins/tool/relcalc.py index fd661b669..263c5bf04 100644 --- a/gramps/plugins/tool/relcalc.py +++ b/gramps/plugins/tool/relcalc.py @@ -170,9 +170,11 @@ class RelCalc(tool.Tool, ManagedWindow): if not iter_: return + other_person = None handle = model.get_handle_from_iter(iter_) - other_person = self.db.get_person_from_handle(handle) - if other_person is None : + if handle: + other_person = self.db.get_person_from_handle(handle) + if other_person is None: self.textbuffer.set_text("") return diff --git a/gramps/plugins/view/geoclose.py b/gramps/plugins/view/geoclose.py index b4aa37e9d..c92da4682 100644 --- a/gramps/plugins/view/geoclose.py +++ b/gramps/plugins/view/geoclose.py @@ -312,12 +312,15 @@ class GeoClose(GeoGraphyView): information. """ active = self.get_active() - person = self.dbstate.db.get_person_from_handle(active) - self.lifeway_layer.clear_ways() - if person is None: - self.goto_handle(None) + if active: + person = self.dbstate.db.get_person_from_handle(active) + self.lifeway_layer.clear_ways() + if person is None: + self.goto_handle(None) + else: + self.goto_handle(handle=person) else: - self.goto_handle(handle=person) + self.goto_handle(None) def draw(self, menu, marks, color, reference): """ diff --git a/gramps/plugins/view/geofamclose.py b/gramps/plugins/view/geofamclose.py index 442f851c8..2f8c4d620 100644 --- a/gramps/plugins/view/geofamclose.py +++ b/gramps/plugins/view/geofamclose.py @@ -300,8 +300,14 @@ class GeoFamClose(GeoGraphyView): information. """ active = self.get_active() - family = self.dbstate.db.get_family_from_handle(active) - self.goto_handle(handle=family) + if active: + family = self.dbstate.db.get_family_from_handle(active) + if family is None: + self.goto_handle(None) + else: + self.goto_handle(handle=family) + else: + self.goto_handle(None) def draw(self, menu, marks, color, reference): """ diff --git a/gramps/plugins/view/geofamily.py b/gramps/plugins/view/geofamily.py index ee2d939fc..a9a7e951a 100644 --- a/gramps/plugins/view/geofamily.py +++ b/gramps/plugins/view/geofamily.py @@ -355,20 +355,24 @@ class GeoFamily(GeoGraphyView): } 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 a lat/lon. """ + if not handle: + return self.place_list = [] self.place_without_coordinates = [] self.minlat = self.maxlat = self.minlon = self.maxlon = 0.0 self.minyear = 9999 self.maxyear = 0 self.message_layer.clear_messages() - family = self.dbstate.db.get_family_from_handle(family_x) - if family is None: - person = self.dbstate.db.get_person_from_handle(self.uistate.get_active('Person')) + if self.dbstate.db.has_family_handle(handle): + family = self.dbstate.db.get_family_from_handle(handle) + self._createmap_for_one_family(family) + else: + person = self.dbstate.db.get_person_from_handle(handle) if not person: return family_list = person.get_family_handle_list() @@ -376,8 +380,6 @@ class GeoFamily(GeoGraphyView): family = self.dbstate.db.get_family_from_handle(family_hdl) if family is not None: self._createmap_for_one_family(family) - else: - self._createmap_for_one_family(family) self.sort = sorted(self.place_list, key=operator.itemgetter(3, 4, 6) )