From 8d2c33bf711b2120a0d202f7cfeca44ea51e1b15 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 23 Jun 2020 20:44:59 -0700 Subject: [PATCH] cfg: For develop mode, overlay on top of regular configuration Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- conftest.py | 4 ++-- plinth/__main__.py | 7 +++---- plinth/cfg.py | 19 +++++++++++-------- plinth/tests/test_cfg.py | 14 +++++++------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/conftest.py b/conftest.py index c250213c8..3e6e47364 100644 --- a/conftest.py +++ b/conftest.py @@ -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') diff --git a/plinth/__main__.py b/plinth/__main__.py index 6d3abd820..3fc6e1adc 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -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) diff --git a/plinth/cfg.py b/plinth/cfg.py index c09afb951..70a861e6d 100644 --- a/plinth/cfg.py +++ b/plinth/cfg.py @@ -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 diff --git a/plinth/tests/test_cfg.py b/plinth/tests/test_cfg.py index 0f709cc27..bb710dc69 100644 --- a/plinth/tests/test_cfg.py +++ b/plinth/tests/test_cfg.py @@ -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'