From 746cf0ee1e033c7a105af67c9e0ddd23c2cb713f Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sun, 3 Jul 2016 17:40:15 +0100 Subject: [PATCH] Move DBAPI default settings into an XML file --- gramps/gen/config.py | 5 ++ gramps/plugins/database/dbapi.py | 74 ++++++++++++++----- .../dbapi_support/defaults/__init__.py | 19 ----- .../defaults/default_settings.py | 44 ----------- 4 files changed, 59 insertions(+), 83 deletions(-) delete mode 100644 gramps/plugins/database/dbapi_support/defaults/__init__.py delete mode 100644 gramps/plugins/database/dbapi_support/defaults/default_settings.py diff --git a/gramps/gen/config.py b/gramps/gen/config.py index cbfa71b60..115cda126 100644 --- a/gramps/gen/config.py +++ b/gramps/gen/config.py @@ -159,6 +159,11 @@ register('behavior.addons-url', "https://raw.githubusercontent.com/gramps-projec register('database.backend', 'bsddb') register('database.compress-backup', True) register('database.path', os.path.join(HOME_DIR, 'grampsdb')) +register('database.dbtype', 'sqlite') # Used in dbapi backend only +register('database.dbname', 'gramps') +register('database.host', 'localhost') +register('database.user', 'user') +register('database.password', 'password') register('export.proxy-order', [["privacy", 0], diff --git a/gramps/plugins/database/dbapi.py b/gramps/plugins/database/dbapi.py index bb6a98634..739887ce4 100644 --- a/gramps/plugins/database/dbapi.py +++ b/gramps/plugins/database/dbapi.py @@ -27,6 +27,9 @@ import time import sys import pickle from operator import itemgetter +import logging +import xml.dom.minidom +from html import escape #------------------------------------------------------------------------ # @@ -36,7 +39,7 @@ from operator import itemgetter from gramps.gen.db.generic import * from gramps.gen.db.dbconst import DBLOGNAME import dbapi_support -import logging +from gramps.gen.config import config from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -123,28 +126,59 @@ class DBAPI(DbGeneric): _LOG.debug("Write database backend file to 'dbapi'") with open(versionpath, "w") as version_file: 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): - if filename in ["__init__.py"]: # skip these - continue - fullpath = os.path.abspath(os.path.join(defaults, filename)) - if os.path.isfile(fullpath): - shutil.copy2(fullpath, directory) + + # Write settings + settings_file = os.path.join(directory, "settings.xml") + with open(settings_file, "w") as f: + f.write("\n") + f.write(" %s\n" % + escape(config.get('database.dbtype'))) + f.write(" %s\n" % + escape(config.get('database.dbname'))) + f.write(" %s\n" % + escape(config.get('database.host'))) + f.write(" %s\n" % + escape(config.get('database.user'))) + f.write(" %s\n" % + escape(config.get('database.password'))) + f.write("\n") + + def __parse_settings(self, settings_file): + """ + Parse the database settings file and return a dictionary of settings. + """ + settings = {} + dom = xml.dom.minidom.parse(settings_file) + top = dom.getElementsByTagName('settings') + if len(top) != 1: + return settings + for key in ('dbtype', 'dbname', 'host', 'user', 'password'): + elements = top[0].getElementsByTagName(key) + if len(elements) == 1: + settings[key] = elements[0].childNodes[0].data + return settings def initialize_backend(self, directory): - # Run code from directory - default_settings = {"__file__": - os.path.join(directory, "default_settings.py"), - "dbapi_support": dbapi_support} - 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) + # Read settings + settings_file = os.path.join(directory, "settings.xml") + settings = self.__parse_settings(settings_file) + + if settings['dbtype'] == 'sqlite': + from dbapi_support.sqlite import Sqlite + path_to_db = os.path.join(directory, 'sqlite.db') + self.dbapi = Sqlite(path_to_db) + elif settings['dbtype'] == 'mysql': + from dbapi_support.mysql import MySQL + self.dbapi = MySQL(settings['host'], settings['user'], + settings['password'], settings['dbname'], + charset='utf8', use_unicode=True) + elif settings['dbtype'] == 'postgres': + from dbapi_support.postgresql import Postgresql + self.dbapi = Postgresql(dbname=settings['dbname'], + user=settings['user'], + host=settings['host'], + password=settings['password']) - self.dbapi = default_settings["dbapi"] self.update_schema() def update_schema(self): diff --git a/gramps/plugins/database/dbapi_support/defaults/__init__.py b/gramps/plugins/database/dbapi_support/defaults/__init__.py deleted file mode 100644 index a051c5cb9..000000000 --- a/gramps/plugins/database/dbapi_support/defaults/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -# -# Gramps - a GTK+/GNOME based genealogy program -# -# Copyright (C) 2015-2016 Douglas S. Blank -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# diff --git a/gramps/plugins/database/dbapi_support/defaults/default_settings.py b/gramps/plugins/database/dbapi_support/defaults/default_settings.py deleted file mode 100644 index a8640da93..000000000 --- a/gramps/plugins/database/dbapi_support/defaults/default_settings.py +++ /dev/null @@ -1,44 +0,0 @@ -# -# Gramps - a GTK+/GNOME based genealogy program -# -# Copyright (C) 2015-2016 Douglas S. Blank -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -## ---------------------------------------------- -## Postgresql -## ---------------------------------------------- - -#from dbapi_support.postgresql import Postgresql -#dbapi = Postgresql(dbname='mydb', user='postgres', -# host='localhost', password='PASSWORD') - -## ---------------------------------------------- -## MySQL -## ---------------------------------------------- - -#from dbapi_support.mysql import MySQL -#dbapi = MySQL("localhost", "root", "PASSWORD", "mysqldb", -# charset='utf8', use_unicode=True) - -## ---------------------------------------------- -## Sqlite -## ---------------------------------------------- - -from dbapi_support.sqlite import Sqlite -path_to_db = os.path.join(os.path.dirname(os.path.realpath(__file__)), - 'sqlite.db') -dbapi = Sqlite(path_to_db)