Commit 1442ee50 authored by Patrik Dufresne's avatar Patrik Dufresne

Change config loading

parent d85528aa
Pipeline #664 passed with stages
in 36 minutes and 48 seconds
......@@ -173,15 +173,15 @@ def start():
if not os.path.isfile(configfile):
print("configuration file %s doesn't exists" % configfile, file=sys.stderr)
exit(1)
tmp_cfg = rdw_config.Configuration(configfile)
log_file = args.get('log_file', None) or tmp_cfg.get_config('LogFile', False)
log_access_file = args.get('log_access_file', None) or tmp_cfg.get_config('LogAccessFile', None)
cfg = rdw_config.Configuration(configfile)
log_file = args.get('log_file', None) or cfg.get_config('LogFile', False)
log_access_file = args.get('log_access_file', None) or cfg.get_config('LogAccessFile', None)
if args.get('debug', False):
environment = 'development'
log_level = "DEBUG"
else:
environment = tmp_cfg.get_config('Environment', 'production')
log_level = tmp_cfg.get_config('LogLevel', 'INFO')
environment = cfg.get_config('Environment', 'production')
log_level = cfg.get_config('LogLevel', 'INFO')
# Configure logging
setup_logging(
......@@ -193,7 +193,7 @@ def start():
logger.info("START")
# Create App.
app = rdw_app.RdiffwebApp(configfile)
app = rdw_app.RdiffwebApp(cfg)
# Get configuration
serverHost = app.cfg.get_config("ServerHost", default=b"0.0.0.0")
......
......@@ -31,11 +31,11 @@ import sys
from rdiffweb import filter_authentication # @UnusedImport
from rdiffweb import i18n # @UnusedImport
from rdiffweb import rdw_config, page_main
from rdiffweb import page_main
from rdiffweb import rdw_plugin
from rdiffweb import rdw_templating
from rdiffweb.api import ApiPage
from rdiffweb.dispatch import static, empty
from rdiffweb.dispatch import static, empty # @UnusedImport
from rdiffweb.page_admin import AdminPage
from rdiffweb.page_browse import BrowsePage
from rdiffweb.page_history import HistoryPage
......@@ -45,14 +45,12 @@ from rdiffweb.page_restore import RestorePage
from rdiffweb.page_settings import SettingsPage
from rdiffweb.page_status import StatusPage
from rdiffweb.user import UserManager
from rdiffweb.page_main import MainPage
from rdiffweb.page_main import MainPage # @UnusedImport
from rdiffweb.librdiff import DoesNotExistError, AccessDeniedError
# Define the logger
logger = logging.getLogger(__name__)
PY3 = sys.version_info[0] == 3
......@@ -91,10 +89,16 @@ class Root(LocationsPage):
class RdiffwebApp(Application):
"""This class represent the application context."""
def __init__(self, configfile=None):
def __init__(self, cfg):
# Initialise the configuration
self.load_config(configfile)
assert cfg
self.cfg = cfg
# Define TEMP env
tempdir = self.cfg.get_config("TempDir", default="")
if tempdir:
os.environ["TMPDIR"] = tempdir
# Initialise the template engine.
self.templates = rdw_templating.TemplateManager()
......@@ -195,15 +199,6 @@ class RdiffwebApp(Application):
self._version = "DEV"
return self._version
def load_config(self, configfile=None):
"""Called during app creating to load the configuration from file."""
self.cfg = rdw_config.Configuration(configfile)
# Define TEMP env
tempdir = self.cfg.get_config("TempDir", default="")
if tempdir:
os.environ["TMPDIR"] = tempdir
def _localedirs(self):
"""
Return a list of locales directory where to search for mo files. This
......
......@@ -37,9 +37,9 @@ import tarfile
import tempfile
import unittest
from rdiffweb import rdw_config
from rdiffweb.rdw_app import RdiffwebApp
try:
from urllib.parse import urlencode # @UnresolvedImport @UnusedImport
except:
......@@ -47,14 +47,33 @@ except:
class MockRdiffwebApp(RdiffwebApp):
def __init__(self, enabled_plugins=['SQLite'], default_config={}):
assert enabled_plugins is None or isinstance(enabled_plugins, list)
self.enabled_plugins = enabled_plugins
assert default_config is None or isinstance(default_config, dict)
self.default_config = default_config
# Define config
cfg = rdw_config.Configuration()
for plugin_name in self.enabled_plugins:
cfg.set_config('%sEnabled' % plugin_name, 'True')
# database in memory
if 'SQLite' in self.enabled_plugins:
self.database_dir = tempfile.mkdtemp(prefix='rdiffweb_tests_db_')
cfg.set_config('SQLiteDBFile', os.path.join(self.database_dir, 'rdiffweb.tmp.db'))
if 'Ldap' in self.enabled_plugins:
cfg.set_config('LdapUri', '__default__')
cfg.set_config('LdapBaseDn', 'dc=nodomain')
# Set config
for key, val in list(self.default_config.items()):
cfg.set_config(key, val)
# Call parent constructor
RdiffwebApp.__init__(self)
RdiffwebApp.__init__(self, cfg)
def clear_db(self):
if hasattr(self, 'database_dir'):
......@@ -66,26 +85,6 @@ class MockRdiffwebApp(RdiffwebApp):
shutil.rmtree(native_str(self.testcases))
delattr(self, 'testcases')
def load_config(self, configfile=None):
RdiffwebApp.load_config(self, None)
# Enabled given plugins
for plugin_name in self.enabled_plugins:
self.cfg.set_config('%sEnabled' % plugin_name, 'True')
# database in memory
if 'SQLite' in self.enabled_plugins:
self.database_dir = tempfile.mkdtemp(prefix='rdiffweb_tests_db_')
self.cfg.set_config('SQLiteDBFile', os.path.join(self.database_dir, 'rdiffweb.tmp.db'))
if 'Ldap' in self.enabled_plugins:
self.cfg.set_config('LdapUri', '__default__')
self.cfg.set_config('LdapBaseDn', 'dc=nodomain')
# Set config
for key, val in list(self.default_config.items()):
self.cfg.set_config(key, val)
def reset(self, username=None, password=None):
"""
Reset the application. Delete all data from database.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment