Refactor global code in configuration module

This commit is contained in:
Sunil Mohan Adapa 2014-06-23 12:22:46 +02:00
parent d9bebe67f5
commit f4fe85ae28
3 changed files with 60 additions and 34 deletions

88
cfg.py
View File

@ -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]

View File

@ -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.

View File

@ -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()