DB-API 2.0 can now load/save from file; need to load/save metadata

This commit is contained in:
Doug Blank 2015-05-23 02:15:44 -04:00
parent 720664818f
commit 83c853726d
2 changed files with 35 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import time
import re import re
import os import os
import logging import logging
import shutil
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -1261,6 +1262,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
self.dbapi.execute("""insert into person(handle, gramps_id, blob) self.dbapi.execute("""insert into person(handle, gramps_id, blob)
values(?, ?, ?);""", values(?, ?, ?);""",
[person.handle, person.gramps_id, pickle.dumps(person.serialize())]) [person.handle, person.gramps_id, pickle.dumps(person.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([person.handle],)) self.emit(emit, ([person.handle],))
@ -1281,6 +1283,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
self.dbapi.execute("insert into family values(?, ?, ?);", self.dbapi.execute("insert into family values(?, ?, ?);",
[family.handle, family.gramps_id, [family.handle, family.gramps_id,
pickle.dumps(family.serialize())]) pickle.dumps(family.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([family.handle],)) self.emit(emit, ([family.handle],))
@ -1300,6 +1303,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "citation-add" emit = "citation-add"
self.dbapi.execute("insert into citation values(?, ?, ?);", self.dbapi.execute("insert into citation values(?, ?, ?);",
[citation.handle, citation.gramps_id, pickle.dumps(citation.serialize())]) [citation.handle, citation.gramps_id, pickle.dumps(citation.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([citation.handle],)) self.emit(emit, ([citation.handle],))
@ -1319,6 +1323,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "source-add" emit = "source-add"
self.dbapi.execute("insert into source values(?, ?, ?);", self.dbapi.execute("insert into source values(?, ?, ?);",
[source.handle, source.gramps_id, pickle.dumps(source.serialize())]) [source.handle, source.gramps_id, pickle.dumps(source.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([source.handle],)) self.emit(emit, ([source.handle],))
@ -1338,6 +1343,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "repository-add" emit = "repository-add"
self.dbapi.execute("insert into repository values(?, ?, ?);", self.dbapi.execute("insert into repository values(?, ?, ?);",
[repository.handle, repository.gramps_id, pickle.dumps(repository.serialize())]) [repository.handle, repository.gramps_id, pickle.dumps(repository.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([repository.handle],)) self.emit(emit, ([repository.handle],))
@ -1357,6 +1363,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "note-add" emit = "note-add"
self.dbapi.execute("insert into note values(?, ?, ?);", self.dbapi.execute("insert into note values(?, ?, ?);",
[note.handle, note.gramps_id, pickle.dumps(note.serialize())]) [note.handle, note.gramps_id, pickle.dumps(note.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([note.handle],)) self.emit(emit, ([note.handle],))
@ -1376,6 +1383,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "place-add" emit = "place-add"
self.dbapi.execute("insert into place values(?, ?, ?);", self.dbapi.execute("insert into place values(?, ?, ?);",
[place.handle, place.gramps_id, pickle.dumps(place.serialize())]) [place.handle, place.gramps_id, pickle.dumps(place.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([place.handle],)) self.emit(emit, ([place.handle],))
@ -1395,6 +1403,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "event-add" emit = "event-add"
self.dbapi.execute("insert into event values(?, ?, ?);", self.dbapi.execute("insert into event values(?, ?, ?);",
[event.handle, event.gramps_id, pickle.dumps(event.serialize())]) [event.handle, event.gramps_id, pickle.dumps(event.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([event.handle],)) self.emit(emit, ([event.handle],))
@ -1412,6 +1421,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "tag-add" emit = "tag-add"
self.dbapi.execute("insert into tag values(?, ?);", self.dbapi.execute("insert into tag values(?, ?);",
[tag.handle, pickle.dumps(tag.serialize())]) [tag.handle, pickle.dumps(tag.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([tag.handle],)) self.emit(emit, ([tag.handle],))
@ -1431,6 +1441,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
emit = "media-add" emit = "media-add"
self.dbapi.execute("insert into media values(?, ?, ?);", self.dbapi.execute("insert into media values(?, ?, ?);",
[media.handle, media.gramps_id, pickle.dumps(media.serialize())]) [media.handle, media.gramps_id, pickle.dumps(media.serialize())])
self.dbapi.commit()
# Emit after added: # Emit after added:
if emit: if emit:
self.emit(emit, ([media.handle],)) self.emit(emit, ([media.handle],))
@ -1706,6 +1717,7 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
writer.write(filename) writer.write(filename)
filename = os.path.join(self._directory, "meta_data.db") filename = os.path.join(self._directory, "meta_data.db")
touch(filename) touch(filename)
self.dbapi.close()
def find_backlink_handles(self, handle, include_classes=None): def find_backlink_handles(self, handle, include_classes=None):
## FIXME ## FIXME
@ -1907,9 +1919,15 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
force_bsddb_downgrade=False, force_bsddb_downgrade=False,
force_python_upgrade=False): force_python_upgrade=False):
# Run code from directory # Run code from directory
import sqlite3 default_settings = {"__file__":
self.dbapi = sqlite3.connect(':memory:') os.path.join(directory, "default_settings.py")}
self.dbapi.row_factory = sqlite3.Row # allows access by name settings_file = os.path.join(directory, "default_settings.py")
with open(settings_file) as f:
code = compile(f.read(), settings_file, 'exec')
exec(code, globals(), default_settings)
self.dbapi = default_settings["dbapi"]
# make sure schema is up to date: # make sure schema is up to date:
self.dbapi.execute("""CREATE TABLE IF NOT EXISTS person ( self.dbapi.execute("""CREATE TABLE IF NOT EXISTS person (
handle TEXT PRIMARY KEY NOT NULL, handle TEXT PRIMARY KEY NOT NULL,
@ -1990,6 +2008,13 @@ class DBAPI(DbWriteBase, DbReadBase, UpdateCallback, Callback):
_LOG.debug("Write database backend file to 'dbapi'") _LOG.debug("Write database backend file to 'dbapi'")
with open(versionpath, "w") as version_file: with open(versionpath, "w") as version_file:
version_file.write("dbapi") version_file.write("dbapi")
# Write default_settings, sqlite.db
defaults = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"dbapi_support", "defaults")
_LOG.debug("Copy defaults from: " + defaults)
for filename in os.listdir(defaults):
fullpath = os.path.abspath(os.path.join(defaults, filename))
shutil.copy2(fullpath, directory)
def report_bm_change(self): def report_bm_change(self):
""" """

View File

@ -0,0 +1,7 @@
import os
import sqlite3
path_to_db = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'sqlite.db')
dbapi = sqlite3.connect(path_to_db)
dbapi.row_factory = sqlite3.Row # allows access by name