From 9ed02e3b6b7260a3c2d745fd958623b8ab5e0373 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 12 Jul 2018 18:02:02 +0100 Subject: [PATCH] Provide a sensible default configuration Signed-off-by: Michael Tremer --- src/backend/base.py | 19 ++++++++++++++++++- src/web/__init__.py | 26 ++++++++++++++++++-------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/backend/base.py b/src/backend/base.py index 2497f4db..47d22c8b 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -1,6 +1,7 @@ #!/usr/bin/python import ConfigParser as configparser +import StringIO import accounts import ads @@ -20,11 +21,22 @@ import talk from . import zeiterfassung +DEFAULT_CONFIG = StringIO.StringIO(""" +[global] +debug = false + +data_dir = +static_dir = %(data_dir)s/static +templates_dir = %(data_dir)s/templates +""") + class Backend(object): def __init__(self, configfile, debug=False): # Read configuration file. self.config = self.read_config(configfile) - self.debug = debug + + # Enable debug logging if configured + self.debug = debug or self.config.getboolean("global", "debug") # Setup database. self.setup_database() @@ -52,6 +64,11 @@ class Backend(object): def read_config(self, configfile): cp = configparser.ConfigParser() + + # Initialize configuration with some sensible defaults + cp.readfp(DEFAULT_CONFIG) + + # Parse file cp.read(configfile) return cp diff --git a/src/web/__init__.py b/src/web/__init__.py index fdd5b477..5f94d905 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -18,14 +18,25 @@ class Application(tornado.web.Application): # Initialize backend self.backend = ipfire.Backend(config) - settings = dict( - debug = tornado.options.options.debug, - login_url = "/login", - template_path = os.path.join(BASEDIR, "templates"), - ui_methods = { + settings = { + # Do not compress responses + "gzip" : False, + + # Enable XSRF cookies + "xsrf_cookies" : True, + + # Login + "login_url" : "/login", + + # Setup directory structure + "static_path" : self.backend.config.get("global", "static_dir"), + "template_path" : self.backend.config.get("global", "templates_dir"), + + # UI Modules + "ui_methods" : { "format_month_name" : self.format_month_name, }, - ui_modules = { + "ui_modules" : { "Advertisement" : AdvertisementModule, "DonationBox" : DonationBoxModule, "DonationButton" : DonationButtonModule, @@ -58,8 +69,7 @@ class Application(tornado.web.Application): "TalkLines" : TalkLinesModule, "TalkOngoingCalls" : TalkOngoingCallsModule, }, - xsrf_cookies = True, - ) + } settings.update(kwargs) tornado.web.Application.__init__(self, **settings) -- 2.47.3