mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
config: Refactor for easy testing
- Split the read method into two separate methods for getting the config file and for reading config file. - Use logging module for printing error. - Fix global variable naming. - Get/set/show the realpath of the config file. - Convert config items into a list so that the order is more predictable. This is the reason for unpredictable failures in test cases.
This commit is contained in:
parent
db548a4e24
commit
a1d5486c82
@ -310,7 +310,7 @@ def main():
|
|||||||
|
|
||||||
configure_django()
|
configure_django()
|
||||||
|
|
||||||
logger.info('Configuration loaded from file - %s', cfg.CONFIG_FILE)
|
logger.info('Configuration loaded from file - %s', cfg.config_file)
|
||||||
logger.info('Script prefix - %s', cfg.server_dir)
|
logger.info('Script prefix - %s', cfg.server_dir)
|
||||||
|
|
||||||
module_loader.load_modules()
|
module_loader.load_modules()
|
||||||
|
|||||||
@ -16,10 +16,13 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from plinth.menu import Menu
|
from plinth.menu import Menu
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
box_name = None
|
box_name = None
|
||||||
root = None
|
root = None
|
||||||
file_root = None
|
file_root = None
|
||||||
@ -42,60 +45,74 @@ danube_edition = False
|
|||||||
|
|
||||||
main_menu = Menu()
|
main_menu = Menu()
|
||||||
|
|
||||||
CONFIG_FILE = None
|
config_file = None
|
||||||
|
|
||||||
DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config'
|
DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config'
|
||||||
DEFAULT_ROOT = '/'
|
DEFAULT_ROOT = '/'
|
||||||
|
|
||||||
|
|
||||||
def read():
|
def get_config_file():
|
||||||
"""Read configuration"""
|
"""Return the configuration file to read."""
|
||||||
global CONFIG_FILE # pylint: disable-msg=W0603
|
|
||||||
if os.path.isfile(DEFAULT_CONFIG_FILE):
|
if os.path.isfile(DEFAULT_CONFIG_FILE):
|
||||||
CONFIG_FILE = DEFAULT_CONFIG_FILE
|
root_directory = DEFAULT_ROOT
|
||||||
directory = DEFAULT_ROOT
|
file_path = DEFAULT_CONFIG_FILE
|
||||||
else:
|
else:
|
||||||
directory = os.path.dirname(os.path.realpath(__file__))
|
root_directory = os.path.dirname(os.path.realpath(__file__))
|
||||||
directory = os.path.join(directory, '..')
|
root_directory = os.path.join(root_directory, '..')
|
||||||
CONFIG_FILE = os.path.join(directory, 'plinth.config')
|
root_directory = os.path.realpath(root_directory)
|
||||||
if not os.path.isfile(CONFIG_FILE):
|
file_path = os.path.join(root_directory, 'plinth.config')
|
||||||
raise FileNotFoundError('No plinth.config file could be found.')
|
|
||||||
|
return file_path, root_directory
|
||||||
|
|
||||||
|
|
||||||
|
def read(file_path=None, root_directory=None):
|
||||||
|
"""Read configuration."""
|
||||||
|
if not file_path and not root_directory:
|
||||||
|
file_path, root_directory = get_config_file()
|
||||||
|
|
||||||
|
if not os.path.isfile(file_path):
|
||||||
|
raise FileNotFoundError('No plinth.config file could be found.')
|
||||||
|
|
||||||
|
global config_file # pylint: disable-msg=invalid-name,global-statement
|
||||||
|
config_file = file_path
|
||||||
|
|
||||||
parser = configparser.ConfigParser(
|
parser = configparser.ConfigParser(
|
||||||
defaults={
|
defaults={
|
||||||
'root': os.path.realpath(directory),
|
'root': os.path.realpath(root_directory),
|
||||||
})
|
})
|
||||||
parser.read(CONFIG_FILE)
|
parser.read(config_file)
|
||||||
|
|
||||||
config_items = {('Path', 'root', 'string'),
|
config_items = (
|
||||||
('Path', 'file_root', 'string'),
|
('Path', 'root', 'string'),
|
||||||
('Path', 'config_dir', 'string'),
|
('Path', 'file_root', 'string'),
|
||||||
('Path', 'data_dir', 'string'),
|
('Path', 'config_dir', 'string'),
|
||||||
('Path', 'store_file', 'string'),
|
('Path', 'data_dir', 'string'),
|
||||||
('Path', 'actions_dir', 'string'),
|
('Path', 'store_file', 'string'),
|
||||||
('Path', 'doc_dir', 'string'),
|
('Path', 'actions_dir', 'string'),
|
||||||
('Path', 'status_log_file', 'string'),
|
('Path', 'doc_dir', 'string'),
|
||||||
('Path', 'access_log_file', 'string'),
|
('Path', 'status_log_file', 'string'),
|
||||||
('Path', 'pidfile', 'string'),
|
('Path', 'access_log_file', 'string'),
|
||||||
('Path', 'server_dir', 'string'),
|
('Path', 'pidfile', 'string'),
|
||||||
('Network', 'host', 'string'),
|
('Path', 'server_dir', 'string'),
|
||||||
('Network', 'port', 'int'),
|
('Network', 'host', 'string'),
|
||||||
('Network', 'secure_proxy_ssl_header', 'string'),
|
('Network', 'port', 'int'),
|
||||||
('Network', 'use_x_forwarded_host', 'bool'),
|
('Network', 'secure_proxy_ssl_header', 'string'),
|
||||||
('Misc', 'box_name', 'string'),
|
('Network', 'use_x_forwarded_host', 'bool'),
|
||||||
('Misc', 'danube_edition', 'bool')}
|
('Misc', 'box_name', 'string'),
|
||||||
|
('Misc', 'danube_edition', 'bool'),
|
||||||
|
)
|
||||||
|
|
||||||
for section, name, datatype in config_items:
|
for section, name, datatype in config_items:
|
||||||
try:
|
try:
|
||||||
value = parser.get(section, name)
|
value = parser.get(section, name)
|
||||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||||
print('Configuration does not contain the {}.{} option.'
|
logger.error('Configuration does not contain option: %s.%s',
|
||||||
.format(section, name))
|
section, name)
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
if datatype == 'int':
|
if datatype == 'int':
|
||||||
value = int(value)
|
value = int(value)
|
||||||
|
elif datatype == 'bool':
|
||||||
if datatype == 'bool':
|
|
||||||
value = (value.lower() == 'true')
|
value = (value.lower() == 'true')
|
||||||
|
|
||||||
globals()[name] = value
|
globals()[name] = value
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user