diff --git a/gramps/gen/db/base.py b/gramps/gen/db/base.py index b93b2aada..d78a7624c 100644 --- a/gramps/gen/db/base.py +++ b/gramps/gen/db/base.py @@ -74,12 +74,6 @@ class DbReadBase: self.basedb = self self.__feature = {} # {"feature": VALUE, ...} - def get_table_func(self, table=None, func=None): - """ - Base implementation of get_table_func. - """ - return None - def get_feature(self, feature): """ Databases can implement certain features or not. The default is diff --git a/gramps/gen/db/generic.py b/gramps/gen/db/generic.py index 11347843a..268af11c0 100644 --- a/gramps/gen/db/generic.py +++ b/gramps/gen/db/generic.py @@ -254,7 +254,7 @@ class DbGenericUndo(DbUndo): else: sql = "INSERT INTO %s (handle, blob_data) VALUES (?, ?)" % table self.db.dbapi.execute(sql, [handle, pickle.dumps(data)]) - obj = self.db.get_table_func(cls)["class_func"].create(data) + obj = self.db._get_table_func(cls)["class_func"].create(data) self.db._update_secondary_values(obj) def undo_signals(self, data, handle, obj_key, emit, signal_root): @@ -763,7 +763,7 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): """ raise NotImplementedError - def get_table_func(self, table=None, func=None): + def _get_table_func(self, table=None, func=None): """ Private implementation of get_table_func. """ @@ -774,16 +774,45 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): elif func in self.__tables[table].keys(): return self.__tables[table][func] else: - return super().get_table_func(table, func) + return None def get_table_names(self): """Return a list of valid table names.""" - return list(self.get_table_func()) + return list(self._get_table_func()) def get_table_metadata(self, table_name): """Return the metadata for a valid table name.""" - if table_name in self.get_table_func(): - return self.get_table_func(table_name) + if table_name in self._get_table_func(): + return self._get_table_func(table_name) + return None + + def get_from_name_and_handle(self, table_name, handle): + """ + Returns a gen.lib object (or None) given table_name and + handle. + + Examples: + + >>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008") + >>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23") + """ + if table_name in self._get_table_func() and handle: + return self._get_table_func(table_name, "handle_func")(handle) + return None + + def get_from_name_and_gramps_id(self, table_name, gramps_id): + """ + Returns a gen.lib object (or None) given table_name and + Gramps ID. + + Examples: + + >>> self.get_from_name_and_gramps_id("Person", "I00002") + >>> self.get_from_name_and_gramps_id("Family", "F056") + >>> self.get_from_name_and_gramps_id("Media", "M00012") + """ + if table_name in self._get_table_func(): + return self._get_table_func(table_name, "gramps_id_func")(gramps_id) return None def _txn_begin(self): @@ -1626,7 +1655,7 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): """ Iterate over items in a class. """ - cursor = self.get_table_func(class_.__name__, "cursor_func") + cursor = self._get_table_func(class_.__name__, "cursor_func") for data in cursor(): yield class_.create(data[1]) @@ -2506,35 +2535,6 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): self.emit('note-rebuild') self.emit('tag-rebuild') - def get_from_name_and_handle(self, table_name, handle): - """ - Returns a gen.lib object (or None) given table_name and - handle. - - Examples: - - >>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008") - >>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23") - """ - if table_name in self.get_table_func() and handle: - return self.get_table_func(table_name, "handle_func")(handle) - return None - - def get_from_name_and_gramps_id(self, table_name, gramps_id): - """ - Returns a gen.lib object (or None) given table_name and - Gramps ID. - - Examples: - - >>> self.get_from_name_and_gramps_id("Person", "I00002") - >>> self.get_from_name_and_gramps_id("Family", "F056") - >>> self.get_from_name_and_gramps_id("Media", "M00012") - """ - if table_name in self.get_table_func(): - return self.get_table_func(table_name, "gramps_id_func")(gramps_id) - return None - def get_save_path(self): return self._directory diff --git a/gramps/gen/lib/test/serialize_test.py b/gramps/gen/lib/test/serialize_test.py index 04eb1d242..1e8df55b5 100644 --- a/gramps/gen/lib/test/serialize_test.py +++ b/gramps/gen/lib/test/serialize_test.py @@ -114,9 +114,9 @@ def generate_case(obj): #setattr(DatabaseCheck, name, test2) db = import_as_dict(EXAMPLE, User()) -for table in db.get_table_func(): - for handle in db.get_table_func(table,"handles_func")(): - obj = db.get_table_func(table,"handle_func")(handle) +for table in db.get_table_names(): + for handle in db.get_table_metadata(table)["handles_func"](): + obj = db.get_table_metadata(table)["handle_func"](handle) generate_case(obj) if __name__ == "__main__": diff --git a/gramps/gen/merge/diff.py b/gramps/gen/merge/diff.py index 20bd8783a..6928e7779 100644 --- a/gramps/gen/merge/diff.py +++ b/gramps/gen/merge/diff.py @@ -108,14 +108,20 @@ def diff_dbs(db1, db2, user): for item in ['Person', 'Family', 'Source', 'Citation', 'Event', 'Media', 'Place', 'Repository', 'Note', 'Tag']: step() - handles1 = sorted([handle for handle in db1.get_table_func(item,"handles_func")()]) - handles2 = sorted([handle for handle in db2.get_table_func(item,"handles_func")()]) + + handles_func1 = db1.get_table_metadata(item)["handles_func"] + handles_func2 = db2.get_table_metadata(item)["handles_func"] + handle_func1 = db1.get_table_metadata(item)["handle_func"] + handle_func2 = db2.get_table_metadata(item)["handle_func"] + + handles1 = sorted([handle for handle in db1.handles_func1()]) + handles2 = sorted([handle for handle in db2.handles_func2()]) p1 = 0 p2 = 0 while p1 < len(handles1) and p2 < len(handles2): if handles1[p1] == handles2[p2]: # in both - item1 = db1.get_table_func(item,"handle_func")(handles1[p1]) - item2 = db2.get_table_func(item,"handle_func")(handles2[p2]) + item1 = db1.handle_func1(handles1[p1]) + item2 = db2.handle_func2(handles2[p2]) diff = diff_items(item, to_struct(item1), to_struct(item2)) if diff: diffs += [(item, item1, item2)] @@ -123,19 +129,19 @@ def diff_dbs(db1, db2, user): p1 += 1 p2 += 1 elif handles1[p1] < handles2[p2]: # p1 is mssing in p2 - item1 = db1.get_table_func(item,"handle_func")(handles1[p1]) + item1 = db1.handle_func1(handles1[p1]) missing_from_new += [(item, item1)] p1 += 1 elif handles1[p1] > handles2[p2]: # p2 is mssing in p1 - item2 = db2.get_table_func(item,"handle_func")(handles2[p2]) + item2 = db2.handle_func2(handles2[p2]) missing_from_old += [(item, item2)] p2 += 1 while p1 < len(handles1): - item1 = db1.get_table_func(item,"handle_func")(handles1[p1]) + item1 = db1.handle_func1(handles1[p1]) missing_from_new += [(item, item1)] p1 += 1 while p2 < len(handles2): - item2 = db2.get_table_func(item,"handle_func")(handles2[p2]) + item2 = db2.handle_func2(handles2[p2]) missing_from_old += [(item, item2)] p2 += 1 return diffs, missing_from_old, missing_from_new diff --git a/gramps/gen/proxy/filter.py b/gramps/gen/proxy/filter.py index 05cad911b..ec8b0e189 100644 --- a/gramps/gen/proxy/filter.py +++ b/gramps/gen/proxy/filter.py @@ -175,19 +175,6 @@ class FilterProxyDb(ProxyDbBase): } } - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def get_person_from_handle(self, handle): """ Finds a Person in the database from the passed Gramps ID. diff --git a/gramps/gen/proxy/living.py b/gramps/gen/proxy/living.py index 0827f5c2a..52779e036 100644 --- a/gramps/gen/proxy/living.py +++ b/gramps/gen/proxy/living.py @@ -198,19 +198,6 @@ class LivingProxyDb(ProxyDbBase): } } - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def get_person_from_handle(self, handle): """ Finds a Person in the database from the passed Gramps ID. diff --git a/gramps/gen/proxy/private.py b/gramps/gen/proxy/private.py index 5898a98f3..ae05cf387 100644 --- a/gramps/gen/proxy/private.py +++ b/gramps/gen/proxy/private.py @@ -159,19 +159,6 @@ class PrivateProxyDb(ProxyDbBase): } } - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def get_person_from_handle(self, handle): """ Finds a Person in the database from the passed Gramps ID. diff --git a/gramps/gen/proxy/proxybase.py b/gramps/gen/proxy/proxybase.py index 5ba5d9235..b35167be4 100644 --- a/gramps/gen/proxy/proxybase.py +++ b/gramps/gen/proxy/proxybase.py @@ -123,122 +123,6 @@ class ProxyDbBase(DbReadBase): self.note_map = ProxyMap(self, self.get_raw_note_data, self.get_note_handles) - self.__tables = { - 'Person': - { - "handle_func": self.get_person_from_handle, - "gramps_id_func": self.get_person_from_gramps_id, - "class_func": Person, - "cursor_func": self.get_person_cursor, - "handles_func": self.get_person_handles, - "iter_func": self.iter_people, - "count_func": self.get_number_of_people, - }, - 'Family': - { - "handle_func": self.get_family_from_handle, - "gramps_id_func": self.get_family_from_gramps_id, - "class_func": Family, - "cursor_func": self.get_family_cursor, - "handles_func": self.get_family_handles, - "iter_func": self.iter_families, - "count_func": self.get_number_of_families, - }, - 'Source': - { - "handle_func": self.get_source_from_handle, - "gramps_id_func": self.get_source_from_gramps_id, - "class_func": Source, - "cursor_func": self.get_source_cursor, - "handles_func": self.get_source_handles, - "iter_func": self.iter_sources, - "count_func": self.get_number_of_sources, - }, - 'Citation': - { - "handle_func": self.get_citation_from_handle, - "gramps_id_func": self.get_citation_from_gramps_id, - "class_func": Citation, - "cursor_func": self.get_citation_cursor, - "handles_func": self.get_citation_handles, - "iter_func": self.iter_citations, - "count_func": self.get_number_of_citations, - }, - 'Event': - { - "handle_func": self.get_event_from_handle, - "gramps_id_func": self.get_event_from_gramps_id, - "class_func": Event, - "cursor_func": self.get_event_cursor, - "handles_func": self.get_event_handles, - "iter_func": self.iter_events, - "count_func": self.get_number_of_events, - }, - 'Media': - { - "handle_func": self.get_media_from_handle, - "gramps_id_func": self.get_media_from_gramps_id, - "class_func": Media, - "cursor_func": self.get_media_cursor, - "handles_func": self.get_media_handles, - "iter_func": self.iter_media, - "count_func": self.get_number_of_media, - }, - 'Place': - { - "handle_func": self.get_place_from_handle, - "gramps_id_func": self.get_place_from_gramps_id, - "class_func": Place, - "cursor_func": self.get_place_cursor, - "handles_func": self.get_place_handles, - "iter_func": self.iter_places, - "count_func": self.get_number_of_places, - }, - 'Repository': - { - "handle_func": self.get_repository_from_handle, - "gramps_id_func": self.get_repository_from_gramps_id, - "class_func": Repository, - "cursor_func": self.get_repository_cursor, - "handles_func": self.get_repository_handles, - "iter_func": self.iter_repositories, - "count_func": self.get_number_of_repositories, - }, - 'Note': - { - "handle_func": self.get_note_from_handle, - "gramps_id_func": self.get_note_from_gramps_id, - "class_func": Note, - "cursor_func": self.get_note_cursor, - "handles_func": self.get_note_handles, - "iter_func": self.iter_notes, - "count_func": self.get_number_of_notes, - }, - 'Tag': - { - "handle_func": self.get_tag_from_handle, - "gramps_id_func": None, - "class_func": Tag, - "cursor_func": self.get_tag_cursor, - "handles_func": self.get_tag_handles, - "iter_func": self.iter_tags, - "count_func": self.get_number_of_tags, - } - } - - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def is_open(self): """ Return True if the database has been opened. diff --git a/gramps/gen/proxy/referencedbyselection.py b/gramps/gen/proxy/referencedbyselection.py index 90b220b8b..79554c60c 100644 --- a/gramps/gen/proxy/referencedbyselection.py +++ b/gramps/gen/proxy/referencedbyselection.py @@ -188,19 +188,6 @@ class ReferencedBySelectionProxyDb(ProxyDbBase): } } - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def queue_object(self, obj_type, handle, reference=True): self.queue.append((obj_type, handle, reference)) diff --git a/gramps/plugins/db/bsddb/read.py b/gramps/plugins/db/bsddb/read.py index a71ba17aa..bfbad545c 100644 --- a/gramps/plugins/db/bsddb/read.py +++ b/gramps/plugins/db/bsddb/read.py @@ -471,7 +471,7 @@ class DbBsddbRead(DbReadBase, Callback): } } - def get_table_func(self, table=None, func=None): + def _get_table_func(self, table=None, func=None): """ Private implementation of get_table_func. """ @@ -482,7 +482,46 @@ class DbBsddbRead(DbReadBase, Callback): elif func in self.__tables[table].keys(): return self.__tables[table][func] else: - return super().get_table_func(table, func) + return None + + def get_table_names(self): + """Return a list of valid table names.""" + return list(self._get_table_func()) + + def get_table_metadata(self, table_name): + """Return the metadata for a valid table name.""" + if table_name in self._get_table_func(): + return self._get_table_func(table_name) + return None + + def get_from_name_and_gramps_id(self, table_name, gramps_id): + """ + Returns a gen.lib object (or None) given table_name and + Gramps ID. + + Examples: + + >>> self.get_from_name_and_gramps_id("Person", "I00002") + >>> self.get_from_name_and_gramps_id("Family", "F056") + >>> self.get_from_name_and_gramps_id("Media", "M00012") + """ + if table_name in self._get_table_func(): + return self._get_table_func(table_name,"gramps_id_func")(gramps_id) + return None + + def get_from_name_and_handle(self, table_name, handle): + """ + Returns a gen.lib object (or None) given table_name and + handle. + + Examples: + + >>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008") + >>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23") + """ + if table_name in self._get_table_func() and handle: + return self._get_table_func(table_name,"handle_func")(handle) + return None def set_prefixes(self, person, media, family, source, citation, place, event, repository, note): @@ -501,16 +540,6 @@ class DbBsddbRead(DbReadBase, Callback): """Return True when the file has a supported version.""" return True - def get_table_names(self): - """Return a list of valid table names.""" - return list(self.get_table_func()) - - def get_table_metadata(self, table_name): - """Return the metadata for a valid table name.""" - if table_name in self.get_table_func(): - return self.get_table_func(table_name) - return None - def get_cursor(self, table, *args, **kwargs): try: return DbReadCursor(table, self.txn) @@ -700,35 +729,6 @@ class DbBsddbRead(DbReadBase, Callback): return newobj raise HandleError('Handle %s not found' % handle.decode('utf-8')) - def get_from_name_and_handle(self, table_name, handle): - """ - Returns a gen.lib object (or None) given table_name and - handle. - - Examples: - - >>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008") - >>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23") - """ - if table_name in self.get_table_func() and handle: - return self.get_table_func(table_name,"handle_func")(handle) - return None - - def get_from_name_and_gramps_id(self, table_name, gramps_id): - """ - Returns a gen.lib object (or None) given table_name and - Gramps ID. - - Examples: - - >>> self.get_from_name_and_gramps_id("Person", "I00002") - >>> self.get_from_name_and_gramps_id("Family", "F056") - >>> self.get_from_name_and_gramps_id("Media", "M00012") - """ - if table_name in self.get_table_func(): - return self.get_table_func(table_name,"gramps_id_func")(gramps_id) - return None - def get_person_from_handle(self, handle): """ Find a Person in the database from the passed handle. diff --git a/gramps/plugins/db/bsddb/write.py b/gramps/plugins/db/bsddb/write.py index 7a352153f..ca03409b8 100644 --- a/gramps/plugins/db/bsddb/write.py +++ b/gramps/plugins/db/bsddb/write.py @@ -241,139 +241,6 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback): DbBsddbRead.__init__(self) DbWriteBase.__init__(self) #UpdateCallback.__init__(self) - self.__tables = { - 'Person': - { - "handle_func": self.get_person_from_handle, - "gramps_id_func": self.get_person_from_gramps_id, - "class_func": Person, - "cursor_func": self.get_person_cursor, - "handles_func": self.get_person_handles, - "add_func": self.add_person, - "commit_func": self.commit_person, - "count_func": self.get_number_of_people, - "del_func": self.remove_person, - "iter_func": self.iter_people, - }, - 'Family': - { - "handle_func": self.get_family_from_handle, - "gramps_id_func": self.get_family_from_gramps_id, - "class_func": Family, - "cursor_func": self.get_family_cursor, - "handles_func": self.get_family_handles, - "add_func": self.add_family, - "commit_func": self.commit_family, - "count_func": self.get_number_of_families, - "del_func": self.remove_family, - "iter_func": self.iter_families, - }, - 'Source': - { - "handle_func": self.get_source_from_handle, - "gramps_id_func": self.get_source_from_gramps_id, - "class_func": Source, - "cursor_func": self.get_source_cursor, - "handles_func": self.get_source_handles, - "add_func": self.add_source, - "commit_func": self.commit_source, - "count_func": self.get_number_of_sources, - "del_func": self.remove_source, - "iter_func": self.iter_sources, - }, - 'Citation': - { - "handle_func": self.get_citation_from_handle, - "gramps_id_func": self.get_citation_from_gramps_id, - "class_func": Citation, - "cursor_func": self.get_citation_cursor, - "handles_func": self.get_citation_handles, - "add_func": self.add_citation, - "commit_func": self.commit_citation, - "count_func": self.get_number_of_citations, - "del_func": self.remove_citation, - "iter_func": self.iter_citations, - }, - 'Event': - { - "handle_func": self.get_event_from_handle, - "gramps_id_func": self.get_event_from_gramps_id, - "class_func": Event, - "cursor_func": self.get_event_cursor, - "handles_func": self.get_event_handles, - "add_func": self.add_event, - "commit_func": self.commit_event, - "count_func": self.get_number_of_events, - "del_func": self.remove_event, - "iter_func": self.iter_events, - }, - 'Media': - { - "handle_func": self.get_media_from_handle, - "gramps_id_func": self.get_media_from_gramps_id, - "class_func": Media, - "cursor_func": self.get_media_cursor, - "handles_func": self.get_media_handles, - "add_func": self.add_media, - "commit_func": self.commit_media, - "count_func": self.get_number_of_media, - "del_func": self.remove_media, - "iter_func": self.iter_media, - }, - 'Place': - { - "handle_func": self.get_place_from_handle, - "gramps_id_func": self.get_place_from_gramps_id, - "class_func": Place, - "cursor_func": self.get_place_cursor, - "handles_func": self.get_place_handles, - "add_func": self.add_place, - "commit_func": self.commit_place, - "count_func": self.get_number_of_places, - "del_func": self.remove_place, - "iter_func": self.iter_places, - }, - 'Repository': - { - "handle_func": self.get_repository_from_handle, - "gramps_id_func": self.get_repository_from_gramps_id, - "class_func": Repository, - "cursor_func": self.get_repository_cursor, - "handles_func": self.get_repository_handles, - "add_func": self.add_repository, - "commit_func": self.commit_repository, - "count_func": self.get_number_of_repositories, - "del_func": self.remove_repository, - "iter_func": self.iter_repositories, - }, - 'Note': - { - "handle_func": self.get_note_from_handle, - "gramps_id_func": self.get_note_from_gramps_id, - "class_func": Note, - "cursor_func": self.get_note_cursor, - "handles_func": self.get_note_handles, - "add_func": self.add_note, - "commit_func": self.commit_note, - "count_func": self.get_number_of_notes, - "del_func": self.remove_note, - "iter_func": self.iter_notes, - }, - 'Tag': - { - "handle_func": self.get_tag_from_handle, - "gramps_id_func": None, - "class_func": Tag, - "cursor_func": self.get_tag_cursor, - "handles_func": self.get_tag_handles, - "add_func": self.add_tag, - "commit_func": self.commit_tag, - "count_func": self.get_number_of_tags, - "del_func": self.remove_tag, - "iter_func": self.iter_tags, - } - } - self.secondary_connected = False self.has_changed = False self.brief_name = None @@ -381,19 +248,6 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback): self.update_python_version = False self.update_pickle_version = False - def get_table_func(self, table=None, func=None): - """ - Private implementation of get_table_func. - """ - if table is None: - return list(self.__tables.keys()) - elif func is None: - return self.__tables[table] - elif func in self.__tables[table].keys(): - return self.__tables[table][func] - else: - return super().get_table_func(table, func) - def catch_db_error(func): """ Decorator function for catching database errors. If *func* throws