cfg: For develop mode, overlay on top of regular configuration

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-06-23 20:44:59 -07:00 committed by James Valleroy
parent 2a38e60d1c
commit 8d2c33bf71
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 23 additions and 21 deletions

View File

@ -53,9 +53,9 @@ def fixture_load_cfg():
root_dir = pathlib.Path(__file__).resolve().parent
test_data_dir = root_dir / 'plinth' / 'tests' / 'data'
cfg_file = test_data_dir / 'etc' / 'plinth' / 'plinth.config'
cfg.read(str(cfg_file), str(root_dir))
cfg.read_file(str(cfg_file), str(root_dir))
yield cfg
cfg.read(str(cfg_file), str(root_dir))
cfg.read_file(str(cfg_file), str(root_dir))
@pytest.fixture(name='develop_mode')

View File

@ -122,12 +122,11 @@ def main():
"""Initialize and start the application"""
arguments = parse_arguments()
cfg.read()
if arguments.develop:
# use the root and plinth.config of the current working directory
config_path, root_directory = cfg.get_fallback_config_paths()
cfg.read(config_path, root_directory)
else:
cfg.read()
config_path, root_directory = cfg.get_develop_config_paths()
cfg.read_file(config_path, root_directory)
adapt_config(arguments)

View File

@ -50,8 +50,8 @@ develop = False
config_files = []
def get_fallback_config_paths():
"""Get config paths of the current source code folder"""
def get_develop_config_paths():
"""Return config paths of current source folder for development mode."""
root_directory = os.path.dirname(os.path.realpath(__file__))
root_directory = os.path.join(root_directory, '..')
root_directory = os.path.realpath(root_directory)
@ -64,16 +64,19 @@ def get_config_paths():
return '/etc/plinth/plinth.config', '/'
def read(config_path=None, root_directory=None):
"""
Read configuration.
def read():
"""Read all configuration files."""
config_path, root_directory = get_config_paths()
read_file(config_path, root_directory)
def read_file(config_path, root_directory):
"""Read and merge into defaults a single configuration file.
- config_path: path of plinth.config file
- root_directory: path of plinth root folder
"""
if not config_path and not root_directory:
config_path, root_directory = get_config_paths()
"""
if not os.path.isfile(config_path):
# Ignore missing configuration files
return

View File

@ -26,14 +26,14 @@ pytestmark = pytest.mark.usefixtures('load_cfg')
def test_read_default_config_file():
"""Verify that the default config file can be read correctly."""
config_file, config_dir = cfg.get_fallback_config_paths()
config_file, config_dir = cfg.get_develop_config_paths()
# Read the plinth.config file directly
parser = configparser.ConfigParser(defaults={'root': config_dir})
parser.read(config_file)
# Read the plinth.config file via the cfg module
cfg.read(config_file, config_dir)
cfg.read_file(config_file, config_dir)
# Compare the two results
compare_configurations(parser)
@ -58,26 +58,26 @@ def test_read_fallback_config_file():
test_dir = os.path.dirname(os.path.realpath(__file__))
fallback_root = os.path.realpath(os.path.join(test_dir, '..', '..'))
fallback_config_file = os.path.join(fallback_root, 'plinth.config')
config_path, root_directory = cfg.get_fallback_config_paths()
cfg.read(config_path, root_directory)
config_path, root_directory = cfg.get_develop_config_paths()
cfg.read_file(config_path, root_directory)
assert cfg.config_files[-1] == fallback_config_file
assert cfg.root == fallback_root
def test_read_missing_config_file():
"""Verify that an exception is raised when there's no config file."""
cfg.read('x-non-existant-file', 'x-root-directory')
cfg.read_file('x-non-existant-file', 'x-root-directory')
def test_read_config_file_with_missing_sections():
"""Verify that missing configuration sections can be detected."""
cfg.read(CONFIG_FILE_WITH_MISSING_SECTIONS, TEST_CONFIG_DIR)
cfg.read_file(CONFIG_FILE_WITH_MISSING_SECTIONS, TEST_CONFIG_DIR)
assert cfg.box_name == 'FreedomBoxTestMissingSections'
def test_read_config_file_with_missing_options():
"""Verify that missing configuration options can be detected."""
cfg.read(CONFIG_FILE_WITH_MISSING_OPTIONS, TEST_CONFIG_DIR)
cfg.read_file(CONFIG_FILE_WITH_MISSING_OPTIONS, TEST_CONFIG_DIR)
assert cfg.box_name == 'FreedomBoxTestMissingOptions'