Database backends: bsddb, django, and dictionary

This commit is contained in:
Doug Blank 2015-05-14 06:31:59 -04:00
parent 73886e9232
commit ca88f37bb6
6 changed files with 230 additions and 18 deletions

View File

@ -225,7 +225,7 @@ class CLIDbManager(object):
""" """
print(_('Import finished...')) print(_('Import finished...'))
def create_new_db_cli(self, title=None, create_db=True): def create_new_db_cli(self, title=None, create_db=True, dbid=None):
""" """
Create a new database. Create a new database.
""" """
@ -244,8 +244,9 @@ class CLIDbManager(object):
if create_db: if create_db:
# write the version number into metadata # write the version number into metadata
if dbid is None:
newdb = self.dbstate.make_database("bsddb") dbid = "bsddb"
newdb = self.dbstate.make_database(dbid)
newdb.write_version(new_path) newdb.write_version(new_path)
(tval, last) = time_val(new_path) (tval, last) = time_val(new_path)
@ -254,11 +255,11 @@ class CLIDbManager(object):
last, tval, False, "")) last, tval, False, ""))
return new_path, title return new_path, title
def _create_new_db(self, title=None): def _create_new_db(self, title=None, dbid=None):
""" """
Create a new database, do extra stuff needed Create a new database, do extra stuff needed
""" """
return self.create_new_db_cli(title) return self.create_new_db_cli(title, dbid=dbid)
def import_new_db(self, filename, user): def import_new_db(self, filename, user):
""" """

View File

@ -72,7 +72,7 @@ from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
from gramps.gen.const import URL_WIKISTRING from gramps.gen.const import URL_WIKISTRING
from .user import User from .user import User
from .dialog import ErrorDialog, QuestionDialog, QuestionDialog2 from .dialog import ErrorDialog, QuestionDialog, QuestionDialog2, ICON
from .pluginmanager import GuiPluginManager from .pluginmanager import GuiPluginManager
from gramps.cli.clidbman import CLIDbManager, NAME_FILE, time_val from gramps.cli.clidbman import CLIDbManager, NAME_FILE, time_val
from .ddtargets import DdTargets from .ddtargets import DdTargets
@ -80,7 +80,6 @@ from gramps.gen.recentfiles import rename_filename, remove_filename
from .glade import Glade from .glade import Glade
from gramps.gen.db.exceptions import DbException from gramps.gen.db.exceptions import DbException
_RETURN = Gdk.keyval_from_name("Return") _RETURN = Gdk.keyval_from_name("Return")
_KP_ENTER = Gdk.keyval_from_name("KP_Enter") _KP_ENTER = Gdk.keyval_from_name("KP_Enter")
@ -104,6 +103,25 @@ STOCK_COL = 6
RCS_BUTTON = { True : _('_Extract'), False : _('_Archive') } RCS_BUTTON = { True : _('_Extract'), False : _('_Archive') }
class DatabaseDialog(Gtk.MessageDialog):
def __init__(self, parent=None):
Gtk.MessageDialog.__init__(self,
parent,
flags=Gtk.DialogFlags.MODAL,
type=Gtk.MessageType.QUESTION,
)
self.set_icon(ICON)
self.set_title('')
self.set_markup('<span size="larger" weight="bold">%s</span>' %
_('Database Backend'))
self.format_secondary_text(
_("Please select a database backend type"))
self.add_button("BSDDB Database (standard)", 1)
self.add_button("Dictionary (in-memory)", 2)
self.add_button("Django Database", 3)
class DbManager(CLIDbManager): class DbManager(CLIDbManager):
""" """
Database Manager. Opens a database manager window that allows users to Database Manager. Opens a database manager window that allows users to
@ -765,19 +783,27 @@ class DbManager(CLIDbManager):
message. message.
""" """
self.new.set_sensitive(False) self.new.set_sensitive(False)
try: # popup window and ask for dbid types, if more than one
self._create_new_db() ## FIXME: autoload from plugins
except (OSError, IOError) as msg: dbid = "bsddb"
DbManager.ERROR(_("Could not create Family Tree"), d = DatabaseDialog(self.top)
str(msg)) database = d.run()
d.destroy()
if database >= 0:
dbid = {1:"bsddb",2:"dictionarydb",3:"djangodb"}[database]
try:
self._create_new_db(dbid=dbid)
except (OSError, IOError) as msg:
DbManager.ERROR(_("Could not create Family Tree"),
str(msg))
self.new.set_sensitive(True) self.new.set_sensitive(True)
def _create_new_db(self, title=None, create_db=True): def _create_new_db(self, title=None, create_db=True, dbid=None):
""" """
Create a new database, append to model Create a new database, append to model
""" """
new_path, title = self.create_new_db_cli(conv_to_unicode(title, 'utf8'), new_path, title = self.create_new_db_cli(conv_to_unicode(title, 'utf8'),
create_db) create_db, dbid)
path_name = os.path.join(new_path, NAME_FILE) path_name = os.path.join(new_path, NAME_FILE)
(tval, last) = time_val(new_path) (tval, last) = time_val(new_path)
node = self.model.append(None, [title, new_path, path_name, node = self.model.append(None, [title, new_path, path_name,

View File

@ -21,7 +21,7 @@
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
# Gramps Modules # Python Modules
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import pickle import pickle
@ -29,7 +29,15 @@ import base64
import time import time
import re import re
import os import os
import logging
#------------------------------------------------------------------------
#
# Gramps Modules
#
#------------------------------------------------------------------------
from gramps.gen.db import DbReadBase, DbWriteBase, DbTxn, KEY_TO_NAME_MAP from gramps.gen.db import DbReadBase, DbWriteBase, DbTxn, KEY_TO_NAME_MAP
from gramps.gen.db.dbconst import *
from gramps.gen.utils.callback import Callback from gramps.gen.utils.callback import Callback
from gramps.gen.updatecallback import UpdateCallback from gramps.gen.updatecallback import UpdateCallback
from gramps.gen.db import (PERSON_KEY, from gramps.gen.db import (PERSON_KEY,
@ -56,6 +64,8 @@ from gramps.gen.lib.repo import Repository
from gramps.gen.lib.note import Note from gramps.gen.lib.note import Note
from gramps.gen.lib.tag import Tag from gramps.gen.lib.tag import Tag
_LOG = logging.getLogger(DBLOGNAME)
class Environment(object): class Environment(object):
""" """
Implements the Environment API. Implements the Environment API.
@ -1612,3 +1622,11 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback):
def undo(self, update_history=True): def undo(self, update_history=True):
pass pass
def write_version(self, directory):
"""Write files for a newly created DB."""
versionpath = os.path.join(directory, str(DBBACKEND))
_LOG.debug("Write database backend file to 'dictionarydb'")
with open(versionpath, "w") as version_file:
version_file.write("dictionarydb")

View File

@ -0,0 +1,150 @@
import os
from gramps.gen.const import DATA_DIR
WEB_DIR = os.path.dirname(os.path.realpath(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INTERNAL_IPS = ('127.0.0.1',)
ADMINS = (
('admin', 'your_email@domain.com'),
)
MANAGERS = ADMINS
DATABASE_ROUTERS = []
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(WEB_DIR, 'sqlite.db'),
}
}
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = os.path.join(WEB_DIR, 'sqlite.db')
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
TIME_ZONE = 'America/New_York'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
MEDIA_ROOT = ''
MEDIA_URL = ''
ADMIN_MEDIA_PREFIX = '/gramps-media/'
SECRET_KEY = 'zd@%vslj5sqhx94_8)0hsx*rk9tj3^ly$x+^*tq4bggr&uh$ac'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader', # 1.4
'django.template.loaders.app_directories.Loader', # 1.4
#'django.template.loaders.filesystem.load_template_source',
#'django.template.loaders.app_directories.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# 'debug_toolbar.middleware.DebugToolbarMiddleware',
)
ROOT_URLCONF = 'gramps.webapp.urls'
STATIC_URL = '/static/' # 1.4
TEMPLATE_DIRS = (
# Use absolute paths, not relative paths.
os.path.join(DATA_DIR, "templates"),
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth", # 1.4
"django.contrib.messages.context_processors.messages", # 1.4
# "django.core.context_processors.auth",
# "django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"gramps.webapp.grampsdb.views.context_processor",
"gramps.webapp.context.messages",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.messages', # 1.4
'django.contrib.sites',
'django.contrib.admin',
'gramps.webapp.grampsdb',
# 'django_extensions',
# 'debug_toolbar',
)
DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.version.VersionDebugPanel',
'debug_toolbar.panels.timer.TimerDebugPanel',
'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
'debug_toolbar.panels.headers.HeaderDebugPanel',
'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
'debug_toolbar.panels.template.TemplateDebugPanel',
'debug_toolbar.panels.sql.SQLDebugPanel',
'debug_toolbar.panels.signals.SignalDebugPanel',
'debug_toolbar.panels.logger.LoggingPanel',
)
def custom_show_toolbar(request):
return True # Always show toolbar, for example purposes only.
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
# 'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
# 'EXTRA_SIGNALS': ['myproject.signals.MySignal'],
'HIDE_DJANGO_SQL': False,
}
AUTH_PROFILE_MODULE = "grampsdb.Profile"
# Had to add these to use settings.configure():
DATABASE_OPTIONS = ''
URL_VALIDATOR_USER_AGENT = ''
DEFAULT_INDEX_TABLESPACE = ''
DEFAULT_TABLESPACE = ''
CACHE_BACKEND = 'locmem://'
TRANSACTIONS_MANAGED = False
LOCALE_PATHS = tuple()
# Changes for Django 1.3:
USE_L10N = True
FORMAT_MODULE_PATH = ""
## End Changes for Django 1.3
# Changes for Django 1.4:
USE_TZ = False
## End Changes for Django 1.4
# Changes for Django 1.5:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
DEFAULT_CHARSET = "utf-8"
## End Changes for Django 1.5
## Changes for Django 1.5.4:
LOGGING_CONFIG = None
AUTH_USER_MODEL = 'auth.User'
## End Changes for Django 1.5.4
LOGIN_URL = "/login/"
LOGOUT_URL = "/logout"
LOGIN_REDIRECT_URL = "/"
## Changes for Django 1.6:
LOGGING = None
## Changes for Django 1.7.1:
ABSOLUTE_URL_OVERRIDES = {}

View File

@ -30,6 +30,9 @@ import re
import base64 import base64
import pickle import pickle
import os import os
import logging
import shutil
from django.db import transaction
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -53,13 +56,13 @@ from gramps.gen.db import (PERSON_KEY,
REPOSITORY_KEY, REPOSITORY_KEY,
NOTE_KEY) NOTE_KEY)
from gramps.gen.utils.id import create_id from gramps.gen.utils.id import create_id
from django.db import transaction from gramps.gen.db.dbconst import *
## add this directory to sys path, so we can find django_support later: ## add this directory to sys path, so we can find django_support later:
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__))) sys.path.append(os.path.dirname(os.path.abspath(__file__)))
_LOG = logging.getLogger(DBLOGNAME)
class Environment(object): class Environment(object):
""" """
Implements the Environment API. Implements the Environment API.
@ -2041,3 +2044,17 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
def restore(self): def restore(self):
pass pass
def write_version(self, directory):
"""Write files for a newly created DB."""
versionpath = os.path.join(directory, str(DBBACKEND))
_LOG.debug("Write database backend file to 'djangodb'")
with open(versionpath, "w") as version_file:
version_file.write("djangodb")
# Write default_settings, sqlite.db
defaults = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"django_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)