From f4fe85ae28e93669558e6e6bd6fff2860f137404 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 23 Jun 2014 12:22:46 +0200 Subject: [PATCH] Refactor global code in configuration module --- cfg.py | 88 +++++++++++++++++++++++++++++---------------- modules/lib/auth.py | 2 -- plinth.py | 4 ++- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/cfg.py b/cfg.py index deb3f943a..bab92ead3 100644 --- a/cfg.py +++ b/cfg.py @@ -4,39 +4,65 @@ import os import ConfigParser from ConfigParser import SafeConfigParser -def get_item(parser, section, name): - try: - return parser.get(section, name) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): - print ("Configuration does not contain the {}.{} option.".format( - section, name)) - raise - -parser = SafeConfigParser( - defaults={ - 'root':os.path.dirname(os.path.realpath(__file__)), - }) -parser.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'plinth.config')) - -product_name = get_item(parser, 'Name', 'product_name') -box_name = get_item(parser, 'Name', 'box_name') -root = get_item(parser, 'Path', 'root') -file_root = get_item(parser, 'Path', 'file_root') -python_root = get_item(parser, 'Path', 'python_root') -data_dir = get_item(parser, 'Path', 'data_dir') -store_file = get_item(parser, 'Path', 'store_file') -user_db = get_item(parser, 'Path', 'user_db') -status_log_file = get_item(parser, 'Path', 'status_log_file') -access_log_file = get_item(parser, 'Path', 'access_log_file') -pidfile = get_item(parser, 'Path', 'pidfile') -host = get_item(parser, 'Network', 'host') -port = int(get_item(parser, 'Network', 'port')) +product_name = None +box_name = None +root = None +file_root = None +python_root = None +data_dir = None +store_file = None +user_db = None +status_log_file = None +access_log_file = None +pidfile = None +host = None +port = None debug = False no_daemon = False +session_key = '_username' main_menu = Menu() -if store_file.endswith(".sqlite3"): - store_file = os.path.splitext(store_file)[0] -if user_db.endswith(".sqlite3"): - user_db = os.path.splitext(user_db)[0] + +def read(): + """Read configuration""" + directory = os.path.dirname(os.path.realpath(__file__)) + parser = SafeConfigParser( + defaults={ + 'root': directory, + }) + parser.read(os.path.join(directory, 'plinth.config')) + + config_items = {('Name', 'product_name'), + ('Name', 'box_name'), + ('Path', 'root'), + ('Path', 'file_root'), + ('Path', 'python_root'), + ('Path', 'data_dir'), + ('Path', 'store_file'), + ('Path', 'user_db'), + ('Path', 'status_log_file'), + ('Path', 'access_log_file'), + ('Path', 'pidfile'), + ('Network', 'host'), + ('Network', 'port')} + + for section, name in config_items: + try: + value = parser.get(section, name) + globals()[name] = value + except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): + print ('Configuration does not contain the {}.{} option.' + .format(section, name)) + raise + + global port # pylint: disable-msg=W0603 + port = int(port) + + global store_file # pylint: disable-msg=W0603 + if store_file.endswith(".sqlite3"): + store_file = os.path.splitext(store_file)[0] + + global user_db # pylint: disable-msg=W0603 + if user_db.endswith(".sqlite3"): + user_db = os.path.splitext(user_db)[0] diff --git a/modules/lib/auth.py b/modules/lib/auth.py index 61f5e0702..5625511e0 100644 --- a/modules/lib/auth.py +++ b/modules/lib/auth.py @@ -6,8 +6,6 @@ from passlib.exc import PasswordSizeError import cfg from model import User -cfg.session_key = '_username' - def add_user(username, passphrase, name='', email='', expert=False): """Add a new user with specified username and passphrase. diff --git a/plinth.py b/plinth.py index 19d0398c5..ca1e85642 100755 --- a/plinth.py +++ b/plinth.py @@ -3,7 +3,6 @@ import argparse import os import sys -import cfg import django.conf import django.core.wsgi @@ -11,6 +10,7 @@ import cherrypy from cherrypy import _cpserver from cherrypy.process.plugins import Daemonizer +import cfg import module_loader import plugin_mount import service @@ -148,6 +148,8 @@ def main(): """Intialize and start the application""" parse_arguments() + cfg.read() + setup_logging() service.init()