Revised iterable table interface; removed DbEngine and DbTable
svn: r14048
This commit is contained in:
parent
12a4c7bebf
commit
f415159d6f
@ -105,59 +105,6 @@ class DbReadCursor(GrampsCursor):
|
|||||||
self.cursor = source.db.cursor(txn)
|
self.cursor = source.db.cursor(txn)
|
||||||
self.source = source
|
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):
|
class DbBsddbRead(DbReadBase, Callback):
|
||||||
"""
|
"""
|
||||||
Read class for the GRAMPS databases. Implements methods necessary to read
|
Read class for the GRAMPS databases. Implements methods necessary to read
|
||||||
@ -235,64 +182,64 @@ class DbBsddbRead(DbReadBase, Callback):
|
|||||||
DbReadBase.__init__(self)
|
DbReadBase.__init__(self)
|
||||||
Callback.__init__(self)
|
Callback.__init__(self)
|
||||||
|
|
||||||
self.engine = DbEngine(
|
self._tables = {
|
||||||
DbTable(
|
'Person':
|
||||||
"Person",
|
{
|
||||||
handle=self.get_person_from_handle,
|
"handle_func": self.get_person_from_handle,
|
||||||
gramps_id=self.get_person_from_gramps_id,
|
"gramps_id_func": self.get_person_from_gramps_id,
|
||||||
_class=Person,
|
"class_func": Person,
|
||||||
_cursor=self.get_person_cursor,
|
"cursor_func": self.get_person_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Family':
|
||||||
'Family',
|
{
|
||||||
handle=self.get_family_from_handle,
|
"handle_func": self.get_family_from_handle,
|
||||||
gramps_id=self.get_family_from_gramps_id,
|
"gramps_id_func": self.get_family_from_gramps_id,
|
||||||
_class=Family,
|
"class_func": Family,
|
||||||
_cursor=self.get_family_cursor,
|
"cursor_func": self.get_family_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Source':
|
||||||
'Source',
|
{
|
||||||
handle=self.get_source_from_handle,
|
"handle_func": self.get_source_from_handle,
|
||||||
gramps_id=self.get_source_from_gramps_id,
|
"gramps_id_func": self.get_source_from_gramps_id,
|
||||||
_class=Source,
|
"class_func": Source,
|
||||||
_cursor=self.get_source_cursor,
|
"cursor_func": self.get_source_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Event':
|
||||||
'Event',
|
{
|
||||||
handle=self.get_event_from_handle,
|
"handle_func": self.get_event_from_handle,
|
||||||
gramps_id=self.get_event_from_gramps_id,
|
"gramps_id_func": self.get_event_from_gramps_id,
|
||||||
_class=Event,
|
"class_func": Event,
|
||||||
_cursor=self.get_event_cursor,
|
"cursor_func": self.get_event_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Media':
|
||||||
'Media',
|
{
|
||||||
handle=self.get_object_from_handle,
|
"handle_func": self.get_object_from_handle,
|
||||||
gramps_id=self.get_object_from_gramps_id,
|
"gramps_id_func": self.get_object_from_gramps_id,
|
||||||
_class=MediaObject,
|
"class_func": MediaObject,
|
||||||
_cursor=self.get_media_cursor,
|
"cursor_func": self.get_media_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Place':
|
||||||
'Place',
|
{
|
||||||
handle=self.get_place_from_handle,
|
"handle_func": self.get_place_from_handle,
|
||||||
gramps_id=self.get_place_from_gramps_id,
|
"gramps_id_func": self.get_place_from_gramps_id,
|
||||||
_class=Place,
|
"class_func": Place,
|
||||||
_cursor=self.get_place_cursor,
|
"cursor_func": self.get_place_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Repository':
|
||||||
'Repository',
|
{
|
||||||
handle=self.get_repository_from_handle,
|
"handle_func": self.get_repository_from_handle,
|
||||||
gramps_id=self.get_repository_from_gramps_id,
|
"gramps_id_func": self.get_repository_from_gramps_id,
|
||||||
_class=Repository,
|
"class_func": Repository,
|
||||||
_cursor=self.get_repository_cursor,
|
"cursor_func": self.get_repository_cursor,
|
||||||
),
|
},
|
||||||
DbTable(
|
'Note':
|
||||||
'Note',
|
{
|
||||||
handle=self.get_note_from_handle,
|
"handle_func": self.get_note_from_handle,
|
||||||
gramps_id=self.get_note_from_gramps_id,
|
"gramps_id_func": self.get_note_from_gramps_id,
|
||||||
_class=Note,
|
"class_func": Note,
|
||||||
_cursor=self.get_note_cursor,
|
"cursor_func": self.get_note_cursor,
|
||||||
),
|
},
|
||||||
)
|
}
|
||||||
|
|
||||||
self.set_person_id_prefix('I%04d')
|
self.set_person_id_prefix('I%04d')
|
||||||
self.set_object_id_prefix('O%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 when the file has a supported version."""
|
||||||
return True
|
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):
|
def get_cursor(self, table, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
return DbReadCursor(table, self.txn)
|
return DbReadCursor(table, self.txn)
|
||||||
@ -528,21 +485,6 @@ class DbBsddbRead(DbReadBase, Callback):
|
|||||||
"""
|
"""
|
||||||
return self.__find_next_gramps_id(self.note_prefix,
|
return self.__find_next_gramps_id(self.note_prefix,
|
||||||
self.nmap_index, self.nid_trans)
|
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):
|
def get_from_handle(self, handle, class_type, data_map):
|
||||||
data = data_map.get(str(handle))
|
data = data_map.get(str(handle))
|
||||||
if data:
|
if data:
|
||||||
@ -551,6 +493,35 @@ class DbBsddbRead(DbReadBase, Callback):
|
|||||||
return newobj
|
return newobj
|
||||||
return None
|
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):
|
def get_person_from_handle(self, handle):
|
||||||
"""
|
"""
|
||||||
Find a Person in the database from the passed gramps' ID.
|
Find a Person in the database from the passed gramps' ID.
|
||||||
|
@ -119,8 +119,9 @@ class Gramplet(object):
|
|||||||
Assumes nav_type is one of the codes of Db.get_by_name.
|
Assumes nav_type is one of the codes of Db.get_by_name.
|
||||||
"""
|
"""
|
||||||
handle = self.uistate.get_active(nav_type, self.nav_group)
|
handle = self.uistate.get_active(nav_type, self.nav_group)
|
||||||
if nav_type in self.dbstate.db.engine and handle:
|
if nav_type in self.dbstate.db.get_table_names() and handle:
|
||||||
return self.dbstate.db.engine[nav_type].get(handle=handle)
|
return self.dbstate.db.get_from_name_and_handle(nav_type, handle)
|
||||||
|
return None
|
||||||
|
|
||||||
def set_active(self, nav_type, handle):
|
def set_active(self, nav_type, handle):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user