diff --git a/src/gen/db/read.py b/src/gen/db/read.py index b9b9ecd19..5b09c4531 100644 --- a/src/gen/db/read.py +++ b/src/gen/db/read.py @@ -105,59 +105,6 @@ class DbReadCursor(GrampsCursor): self.cursor = source.db.cursor(txn) self.source = source -class DbEngine(object): - """ - A collection of DbTables and related methods. - """ - def __init__(self, *tables): - self.tables = {} - for table in tables: - self.tables[table.name] = table - - def __getattr__(self, table): - if table in self.tables: - return self.tables[table] - - def __getitem__(self, item): - if item in self.tables: - return self.tables[item] - - def __iter__(self): - return self.__next__() - - def __next__(self): - for item in self.tables.keys(): - yield item - - def __exit__(self, *args, **kwargs): - pass - -class DbTable(object): - """ - An object to hold data related to a primary database schema. - """ - def __init__(self, name, **kwargs): - self.name = name - self.settings = {} - self.settings.update(kwargs) - - def update(self, **kwargs): - self.settings.update(kwargs) - - def __getattr__(self, attr): - if attr in self.settings: - return self.settings[attr] - - def __getitem__(self, item): - if item in self.settings: - return self.settings[attr] - - def get(self, **kwargs): - for keyword in kwargs: - if keyword in self.settings: - return self.settings[keyword](kwargs[keyword]) - - class DbBsddbRead(DbReadBase, Callback): """ Read class for the GRAMPS databases. Implements methods necessary to read @@ -235,64 +182,64 @@ class DbBsddbRead(DbReadBase, Callback): DbReadBase.__init__(self) Callback.__init__(self) - self.engine = DbEngine( - DbTable( - "Person", - handle=self.get_person_from_handle, - gramps_id=self.get_person_from_gramps_id, - _class=Person, - _cursor=self.get_person_cursor, - ), - DbTable( - 'Family', - handle=self.get_family_from_handle, - gramps_id=self.get_family_from_gramps_id, - _class=Family, - _cursor=self.get_family_cursor, - ), - DbTable( - 'Source', - handle=self.get_source_from_handle, - gramps_id=self.get_source_from_gramps_id, - _class=Source, - _cursor=self.get_source_cursor, - ), - DbTable( - 'Event', - handle=self.get_event_from_handle, - gramps_id=self.get_event_from_gramps_id, - _class=Event, - _cursor=self.get_event_cursor, - ), - DbTable( - 'Media', - handle=self.get_object_from_handle, - gramps_id=self.get_object_from_gramps_id, - _class=MediaObject, - _cursor=self.get_media_cursor, - ), - DbTable( - 'Place', - handle=self.get_place_from_handle, - gramps_id=self.get_place_from_gramps_id, - _class=Place, - _cursor=self.get_place_cursor, - ), - DbTable( - 'Repository', - handle=self.get_repository_from_handle, - gramps_id=self.get_repository_from_gramps_id, - _class=Repository, - _cursor=self.get_repository_cursor, - ), - DbTable( - 'Note', - handle=self.get_note_from_handle, - gramps_id=self.get_note_from_gramps_id, - _class=Note, - _cursor=self.get_note_cursor, - ), - ) + 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, + }, + '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, + }, + '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, + }, + '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, + }, + 'Media': + { + "handle_func": self.get_object_from_handle, + "gramps_id_func": self.get_object_from_gramps_id, + "class_func": MediaObject, + "cursor_func": self.get_media_cursor, + }, + '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, + }, + '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, + }, + '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, + }, + } self.set_person_id_prefix('I%04d') self.set_object_id_prefix('O%04d') @@ -394,6 +341,16 @@ 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 self._tables.keys() + + def get_table_metadata(self, table_name): + """Return the metadata for a valid table name.""" + if table_name in self._tables: + return self._tables[table_name] + return None + def get_cursor(self, table, *args, **kwargs): try: return DbReadCursor(table, self.txn) @@ -528,21 +485,6 @@ class DbBsddbRead(DbReadBase, Callback): """ return self.__find_next_gramps_id(self.note_prefix, self.nmap_index, self.nid_trans) - - def get_by_name(self, name, handle): - """ - Given one of the object names (not class_type) lookup the - object by handle. - """ - return self.engine[name].get(handle=handle) - - def get_by_gramps_id(self, name, gramps_id): - """ - Given one of the object names (not class_type) lookup the - object by handle. - """ - return self.engine[name].get(gramps_id=gramps_id) - def get_from_handle(self, handle, class_type, data_map): data = data_map.get(str(handle)) if data: @@ -551,6 +493,35 @@ class DbBsddbRead(DbReadBase, Callback): return newobj 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._tables: + return self._tables[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._tables: + return self._tables[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 gramps' ID. diff --git a/src/gen/plug/_gramplet.py b/src/gen/plug/_gramplet.py index ed946db68..3ad84002c 100644 --- a/src/gen/plug/_gramplet.py +++ b/src/gen/plug/_gramplet.py @@ -119,8 +119,9 @@ class Gramplet(object): Assumes nav_type is one of the codes of Db.get_by_name. """ handle = self.uistate.get_active(nav_type, self.nav_group) - if nav_type in self.dbstate.db.engine and handle: - return self.dbstate.db.engine[nav_type].get(handle=handle) + if nav_type in self.dbstate.db.get_table_names() and handle: + return self.dbstate.db.get_from_name_and_handle(nav_type, handle) + return None def set_active(self, nav_type, handle): """