Remove DB-API XML config; replace with settings.py/.ini
This commit is contained in:
parent
b43808c9d5
commit
05bd51b425
@ -159,11 +159,6 @@ register('behavior.addons-url', "https://raw.githubusercontent.com/gramps-projec
|
|||||||
register('database.backend', 'bsddb')
|
register('database.backend', 'bsddb')
|
||||||
register('database.compress-backup', True)
|
register('database.compress-backup', True)
|
||||||
register('database.path', os.path.join(HOME_DIR, 'grampsdb'))
|
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',
|
register('export.proxy-order',
|
||||||
[["privacy", 0],
|
[["privacy", 0],
|
||||||
|
@ -28,8 +28,6 @@ import sys
|
|||||||
import pickle
|
import pickle
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import logging
|
import logging
|
||||||
import xml.dom.minidom
|
|
||||||
from html import escape
|
|
||||||
|
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -125,59 +123,38 @@ class DBAPI(DbGeneric):
|
|||||||
_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 settings.py and settings.ini:
|
||||||
# Write settings
|
settings_py = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||||
settings_file = os.path.join(directory, "settings.xml")
|
"settings.py")
|
||||||
with open(settings_file, "w") as f:
|
settings_ini = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||||
f.write("<settings>\n")
|
"settings.ini")
|
||||||
f.write(" <dbtype>%s</dbtype>\n" %
|
LOG.debug("Copy settings.py from: " + settings_py)
|
||||||
escape(config.get('database.dbtype')))
|
LOG.debug("Copy settings.ini from: " + settings_py)
|
||||||
f.write(" <dbname>%s</dbname>\n" %
|
shutil.copy2(settings_py, directory)
|
||||||
escape(config.get('database.dbname')))
|
shutil.copy2(settings_ini, directory)
|
||||||
f.write(" <host>%s</host>\n" %
|
|
||||||
escape(config.get('database.host')))
|
|
||||||
f.write(" <user>%s</user>\n" %
|
|
||||||
escape(config.get('database.user')))
|
|
||||||
f.write(" <password>%s</password>\n" %
|
|
||||||
escape(config.get('database.password')))
|
|
||||||
f.write("</settings>\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):
|
def initialize_backend(self, directory):
|
||||||
# Read settings
|
# Run code from directory
|
||||||
settings_file = os.path.join(directory, "settings.xml")
|
from gramps.gen.utils.configmanager import ConfigManager
|
||||||
settings = self.__parse_settings(settings_file)
|
config_file = os.path.join(directory, 'settings.ini')
|
||||||
|
config = ConfigManager(config_file)
|
||||||
if settings['dbtype'] == 'sqlite':
|
config.register('database.dbtype', 'sqlite')
|
||||||
from gramps.plugins.db.dbapi.sqlite import Sqlite
|
config.register('database.dbname', 'gramps')
|
||||||
path_to_db = os.path.join(directory, 'sqlite.db')
|
config.register('database.host', 'localhost')
|
||||||
self.dbapi = Sqlite(path_to_db)
|
config.register('database.user', 'user')
|
||||||
elif settings['dbtype'] == 'mysql':
|
config.register('database.password', 'password')
|
||||||
from gramps.plugins.db.dbapi.mysql import MySQL
|
config.register('database.port', 'port')
|
||||||
self.dbapi = MySQL(settings['host'], settings['user'],
|
config.load() # load from settings.ini
|
||||||
settings['password'], settings['dbname'],
|
settings = {
|
||||||
charset='utf8', use_unicode=True)
|
"__file__":
|
||||||
elif settings['dbtype'] == 'postgres':
|
os.path.join(directory, "settings.py"),
|
||||||
from gramps.plugins.db.dbapi.postgresql import Postgresql
|
"config": config
|
||||||
self.dbapi = Postgresql(dbname=settings['dbname'],
|
}
|
||||||
user=settings['user'],
|
settings_file = os.path.join(directory, "settings.py")
|
||||||
host=settings['host'],
|
with open(settings_file) as f:
|
||||||
password=settings['password'])
|
code = compile(f.read(), settings_file, 'exec')
|
||||||
|
exec(code, globals(), settings)
|
||||||
|
self.dbapi = settings["dbapi"]
|
||||||
self.update_schema()
|
self.update_schema()
|
||||||
|
|
||||||
def update_schema(self):
|
def update_schema(self):
|
||||||
|
11
gramps/plugins/db/dbapi/settings.ini
Normal file
11
gramps/plugins/db/dbapi/settings.ini
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
;; Gramps key file
|
||||||
|
;; Automatically created at 2016/07/12 13:06:48
|
||||||
|
|
||||||
|
[database]
|
||||||
|
;;dbname='gramps'
|
||||||
|
;;dbtype='sqlite'
|
||||||
|
;;host='localhost'
|
||||||
|
;;password='password'
|
||||||
|
;;port='port'
|
||||||
|
;;user='user'
|
||||||
|
|
55
gramps/plugins/db/dbapi/settings.py
Normal file
55
gramps/plugins/db/dbapi/settings.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#
|
||||||
|
# Gramps - a GTK+/GNOME based genealogy program
|
||||||
|
#
|
||||||
|
# Copyright (C) 2015-2016 Douglas S. Blank <doug.blank@gmail.com>
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
|
||||||
|
## This file is copied from gramps/plugins/db/dbapi/settings.py
|
||||||
|
## into each grampsdb/*/ directory. You can edit each copy
|
||||||
|
## to connect to different databases, or with different
|
||||||
|
## parameters.
|
||||||
|
|
||||||
|
## The database options are saved in settings.ini.
|
||||||
|
# NOTE: config is predefined
|
||||||
|
|
||||||
|
# NOTE: you can override this in settings.ini or here:
|
||||||
|
dbtype = config.get('database.dbtype')
|
||||||
|
|
||||||
|
if dbtype == "sqlite":
|
||||||
|
from gramps.plugins.db.dbapi.sqlite import Sqlite
|
||||||
|
path_to_db = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
||||||
|
'sqlite.db')
|
||||||
|
dbapi = Sqlite(path_to_db)
|
||||||
|
else:
|
||||||
|
# NOTE: you can override these settings here or in settings.ini:
|
||||||
|
dbname = config.get('database.dbname')
|
||||||
|
host = config.get('database.host')
|
||||||
|
user = config.get('database.user')
|
||||||
|
password = config.get('database.password')
|
||||||
|
port = config.get('database.port')
|
||||||
|
if dbtype == "mysql":
|
||||||
|
from gramps.plugins.db.dbapi.mysql import MySQL
|
||||||
|
dbapi = MySQL(host, user, password, dbname,
|
||||||
|
charset='utf8', use_unicode=True)
|
||||||
|
elif dbtype == "postgresql":
|
||||||
|
from gramps.plugins.db.dbapi.postgresql import Postgresql
|
||||||
|
dbapi = Postgresql(dbname=dbname, user=user,
|
||||||
|
host=host, password=password)
|
||||||
|
else:
|
||||||
|
raise AttributeError(("invalid DB-API dbtype: '%s'. " +
|
||||||
|
"Should be 'sqlite', 'mysql', " +
|
||||||
|
"or 'postgresql'") % dbtype)
|
Loading…
Reference in New Issue
Block a user