Dont determine paths at module import time

This commit is contained in:
Philipp Hörist 2018-04-25 21:26:02 +02:00
parent 8c23d5a4b5
commit 707599dd86
2 changed files with 22 additions and 23 deletions

View File

@ -49,9 +49,6 @@ from gajim.common.const import (
JIDConstant, KindConstant, ShowConstant, TypeConstant, JIDConstant, KindConstant, ShowConstant, TypeConstant,
SubscriptionConstant) SubscriptionConstant)
LOG_DB_PATH = configpaths.get('LOG_DB')
LOG_DB_FOLDER, LOG_DB_FILE = os.path.split(LOG_DB_PATH)
CACHE_DB_PATH = configpaths.get('CACHE_DB')
LOGS_SQL_STATEMENT = ''' LOGS_SQL_STATEMENT = '''
CREATE TABLE jids( CREATE TABLE jids(
@ -132,6 +129,8 @@ class Logger:
self._jid_ids = {} self._jid_ids = {}
self._con = None self._con = None
self._commit_timout_id = None self._commit_timout_id = None
self._log_db_path = configpaths.get('LOG_DB')
self._cache_db_path = configpaths.get('CACHE_DB')
self._create_databases() self._create_databases()
self._migrate_databases() self._migrate_databases()
@ -139,23 +138,23 @@ class Logger:
self._get_jid_ids_from_db() self._get_jid_ids_from_db()
def _create_databases(self): def _create_databases(self):
if os.path.isdir(LOG_DB_PATH): if os.path.isdir(self._log_db_path):
log.error(_('%s is a directory but should be a file'), log.error(_('%s is a directory but should be a file'),
LOG_DB_PATH) self._log_db_path)
sys.exit() sys.exit()
if os.path.isdir(CACHE_DB_PATH): if os.path.isdir(self._cache_db_path):
log.error(_('%s is a directory but should be a file'), log.error(_('%s is a directory but should be a file'),
CACHE_DB_PATH) self._cache_db_path)
sys.exit() sys.exit()
if not os.path.exists(LOG_DB_PATH): if not os.path.exists(self._log_db_path):
if os.path.exists(CACHE_DB_PATH): if os.path.exists(self._cache_db_path):
os.remove(CACHE_DB_PATH) os.remove(self._cache_db_path)
self._create(LOGS_SQL_STATEMENT, LOG_DB_PATH) self._create(LOGS_SQL_STATEMENT, self._log_db_path)
if not os.path.exists(CACHE_DB_PATH): if not os.path.exists(self._cache_db_path):
self._create(CACHE_SQL_STATEMENT, CACHE_DB_PATH) self._create(CACHE_SQL_STATEMENT, self._cache_db_path)
@staticmethod @staticmethod
def _create(statement, path): def _create(statement, path):
@ -181,11 +180,11 @@ class Logger:
def _migrate_databases(self): def _migrate_databases(self):
try: try:
con = sqlite.connect(LOG_DB_PATH) con = sqlite.connect(self._log_db_path)
self._migrate_logs(con) self._migrate_logs(con)
con.close() con.close()
con = sqlite.connect(CACHE_DB_PATH) con = sqlite.connect(self._cache_db_path)
self._migrate_cache(con) self._migrate_cache(con)
con.close() con.close()
except Exception: except Exception:
@ -267,7 +266,7 @@ class Logger:
def _connect_databases(self): def _connect_databases(self):
self._con = sqlite.connect( self._con = sqlite.connect(
LOG_DB_PATH, timeout=20.0, isolation_level='IMMEDIATE') self._log_db_path, timeout=20.0, isolation_level='IMMEDIATE')
self._con.row_factory = self.namedtuple_factory self._con.row_factory = self.namedtuple_factory
@ -278,7 +277,7 @@ class Logger:
self._set_synchronous(False) self._set_synchronous(False)
try: try:
self._con.execute("ATTACH DATABASE '%s' AS cache" % self._con.execute("ATTACH DATABASE '%s' AS cache" %
CACHE_DB_PATH.replace("'", "''")) self._cache_db_path.replace("'", "''"))
except Exception as error: except Exception as error:
log.exception('Error') log.exception('Error')
self._con.close() self._con.close()
@ -680,7 +679,7 @@ class Logger:
sql, tuple(jids) + (restore, pending)).fetchall() sql, tuple(jids) + (restore, pending)).fetchall()
except sqlite.DatabaseError: except sqlite.DatabaseError:
self.dispatch('DB_ERROR', self.dispatch('DB_ERROR',
exceptions.DatabaseMalformed(LOG_DB_PATH)) exceptions.DatabaseMalformed(self._log_db_path))
return [] return []
messages.reverse() messages.reverse()

View File

@ -84,7 +84,6 @@ if is_standalone():
from gajim.common import app from gajim.common import app
from gajim import gtkgui_helpers from gajim import gtkgui_helpers
from gajim.common.logger import LOG_DB_PATH
from gajim.common.const import JIDConstant, KindConstant from gajim.common.const import JIDConstant, KindConstant
from gajim.common import helpers from gajim.common import helpers
from gajim import dialogs from gajim import dialogs
@ -109,9 +108,10 @@ class HistoryManager:
# set the icon to all windows # set the icon to all windows
Gtk.Window.set_default_icon_list(pixs) Gtk.Window.set_default_icon_list(pixs)
if not os.path.exists(LOG_DB_PATH): log_db_path = configpaths.get('LOG_DB')
if not os.path.exists(log_db_path):
dialogs.ErrorDialog(_('Cannot find history logs database'), dialogs.ErrorDialog(_('Cannot find history logs database'),
'%s does not exist.' % LOG_DB_PATH) '%s does not exist.' % log_db_path)
sys.exit() sys.exit()
xml = gtkgui_helpers.get_gtk_builder('history_manager.ui') xml = gtkgui_helpers.get_gtk_builder('history_manager.ui')
@ -128,8 +128,8 @@ class HistoryManager:
self.jids_already_in = [] # holds jids that we already have in DB self.jids_already_in = [] # holds jids that we already have in DB
self.AT_LEAST_ONE_DELETION_DONE = False self.AT_LEAST_ONE_DELETION_DONE = False
self.con = sqlite3.connect(LOG_DB_PATH, timeout=20.0, self.con = sqlite3.connect(
isolation_level='IMMEDIATE') log_db_path, timeout=20.0, isolation_level='IMMEDIATE')
self.cur = self.con.cursor() self.cur = self.con.cursor()
self._init_jids_listview() self._init_jids_listview()