diff --git a/plinth/cfg.py b/plinth/cfg.py index 75f8a7bc6..d03d2cadd 100644 --- a/plinth/cfg.py +++ b/plinth/cfg.py @@ -50,6 +50,19 @@ develop = False config_files = [] +def expand_to_dot_d_paths(file_paths): + """Expand a list of file paths to include file.d/* also.""" + final_list = [] + for file_path in file_paths: + final_list.append(str(file_path)) + path = pathlib.Path(file_path) + path_d = path.with_suffix(path.suffix + '.d') + for dot_d_file in sorted(path_d.glob('*' + path.suffix)): + final_list.append(str(dot_d_file)) + + return final_list + + def get_develop_config_path(): """Return config path of current source folder for development mode.""" root_directory = os.path.dirname(os.path.realpath(__file__)) @@ -58,15 +71,20 @@ def get_develop_config_path(): return config_path -def get_config_path(): +def get_config_paths(): """Get default config paths.""" - return '/etc/freedombox/freedombox.config' + return [ + '/usr/share/freedombox/freedombox.config', + '/etc/plinth/plinth.config', + '/etc/freedombox/freedombox.config', + ] def read(): """Read all configuration files.""" - config_path = get_config_path() - read_file(config_path) + config_paths = get_config_paths() + for config_path in expand_to_dot_d_paths(config_paths): + read_file(config_path) def read_file(config_path): diff --git a/plinth/tests/data/configs/freedombox.config.d/01_first.config b/plinth/tests/data/configs/freedombox.config.d/01_first.config new file mode 100644 index 000000000..df319c2a9 --- /dev/null +++ b/plinth/tests/data/configs/freedombox.config.d/01_first.config @@ -0,0 +1,2 @@ +[Misc] +box_name = FreedomBox01 diff --git a/plinth/tests/data/configs/freedombox.config.d/02_second.config b/plinth/tests/data/configs/freedombox.config.d/02_second.config new file mode 100644 index 000000000..af3a01772 --- /dev/null +++ b/plinth/tests/data/configs/freedombox.config.d/02_second.config @@ -0,0 +1,2 @@ +[Misc] +box_name = FreedomBox02 diff --git a/plinth/tests/data/configs/freedombox.config.d/03_third.foo b/plinth/tests/data/configs/freedombox.config.d/03_third.foo new file mode 100644 index 000000000..ff7355ae2 --- /dev/null +++ b/plinth/tests/data/configs/freedombox.config.d/03_third.foo @@ -0,0 +1,2 @@ +[Misc] +box_name = FreedomBox03 diff --git a/plinth/tests/test_cfg.py b/plinth/tests/test_cfg.py index f512df28a..3c087c704 100644 --- a/plinth/tests/test_cfg.py +++ b/plinth/tests/test_cfg.py @@ -45,15 +45,30 @@ def test_read_default_config_file(): compare_configurations(parser) -@patch('plinth.cfg.get_config_path') -def test_read_primary_config_file(get_config_path): +@patch('plinth.cfg.get_config_paths') +def test_read_primary_config_file(get_config_paths): """Verify that the primary config file is used by default.""" config_path = CONFIG_FILE_WITH_MISSING_OPTIONS - get_config_path.return_value = config_path + get_config_paths.return_value = [config_path] cfg.read() assert cfg.config_files[-1] == config_path +@patch('plinth.cfg.get_config_paths') +def test_read_dot_d_config_files(get_config_paths): + """Verify that the configuration is read from .d directories.""" + root_dir = pathlib.Path(__file__).resolve().parent + config_path = root_dir / 'data' / 'configs' / 'freedombox.config' + config_path_d = config_path.with_suffix(config_path.suffix + '.d') + + get_config_paths.return_value = [str(config_path)] + cfg.read() + assert cfg.config_files[-3] == str(config_path) + assert cfg.config_files[-2] == str(config_path_d / '01_first.config') + assert cfg.config_files[-1] == str(config_path_d / '02_second.config') + assert cfg.box_name == 'FreedomBox02' + + def test_read_develop_config_file(): """Verify that the correct develop config file is used.""" test_dir = os.path.dirname(os.path.realpath(__file__))