diff --git a/doc/plinth.xml b/doc/plinth.xml index 6a533b293..3110e0ac5 100644 --- a/doc/plinth.xml +++ b/doc/plinth.xml @@ -94,6 +94,10 @@ /etc/plinth/plinth.config. This means that Plinth will be available as http://localhost:8000/plinth by default. + When /etc/plinth/plinth.config is not + available, plinth.config from the current + working directory is used. + @@ -221,7 +225,9 @@ $ plinth --debug --develop Enable debug and development mode and run on terminal. This uses - the configuration and action files of the current working directory. + the configuration and action files of the current working directory and + gives debugging information like extended error pages, or not failing + silently on module initialization errors. diff --git a/plinth/__main__.py b/plinth/__main__.py index cbbe2befb..c4f916413 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -384,24 +384,15 @@ def adapt_config(arguments): setattr(cfg, argument_name, argument_value) -def get_relative_config_paths(): - """Get config paths of the current source code folder""" - root_directory = os.path.dirname(os.path.realpath(__file__)) - root_directory = os.path.join(root_directory, '..') - root_directory = os.path.realpath(root_directory) - file_path = os.path.join(root_directory, 'plinth.config') - return { - 'file_path': file_path, - 'root_directory': root_directory, - } - - def main(): """Intialize and start the application""" arguments = parse_arguments() config_paths = {} if arguments.develop: - config_paths = get_relative_config_paths() + # use the root and plinth.config of the current working directory + file_path, root_directory = cfg.get_fallback_config_paths() + config_paths['file_path'] = file_path + config_paths['root_directory'] = root_directory cfg.read(**config_paths) adapt_config(arguments) diff --git a/plinth/cfg.py b/plinth/cfg.py index d9944809e..1354823e2 100644 --- a/plinth/cfg.py +++ b/plinth/cfg.py @@ -45,22 +45,41 @@ DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config' DEFAULT_ROOT = '/' -def read(file_path=None, root_directory=None): +def get_fallback_config_paths(): + """Get config paths of the current source code folder""" + root_directory = os.path.dirname(os.path.realpath(__file__)) + root_directory = os.path.join(root_directory, '..') + root_directory = os.path.realpath(root_directory) + config_path = os.path.join(root_directory, 'plinth.config') + return config_path, root_directory + + +def get_config_paths(): + """Get config paths. + Return the fallback plinth config if the default one does not exist""" + root_directory = DEFAULT_ROOT + config_path = DEFAULT_CONFIG_FILE + if not os.path.isfile(config_path): + config_path, root_directory = get_fallback_config_paths() + return config_path, root_directory + + +def read(config_path=None, root_directory=None): """ Read configuration. - - file_path: path of plinth.config file + - config_path: path of plinth.config file - root_directory: path of plinth root folder """ - if not file_path and not root_directory: - root_directory = DEFAULT_ROOT - file_path = DEFAULT_CONFIG_FILE + if not config_path and not root_directory: + config_path, root_directory = get_config_paths() - if not os.path.isfile(file_path): - raise FileNotFoundError('No plinth.config file could be found.') + if not os.path.isfile(config_path): + msg = 'No plinth.config file could be found on path: %s' % config_path + raise FileNotFoundError(msg) global config_file # pylint: disable-msg=invalid-name,global-statement - config_file = file_path + config_file = config_path parser = configparser.ConfigParser( defaults={ diff --git a/plinth/tests/test_cfg.py b/plinth/tests/test_cfg.py index 4c14f7a6e..f81d8ddcf 100644 --- a/plinth/tests/test_cfg.py +++ b/plinth/tests/test_cfg.py @@ -41,7 +41,7 @@ class TestCfg(unittest.TestCase): @classmethod def setUpClass(cls): """Locate and copy the official plinth.config file.""" - cls.test_config_file, cls.test_config_dir = cfg.get_config_file() + cls.test_config_file, cls.test_config_dir = cfg.get_config_paths() @classmethod def tearDownClass(cls): @@ -62,42 +62,37 @@ class TestCfg(unittest.TestCase): self.compare_configurations(parser) def test_read_primary_config_file(self): - """Verify that the primary config file can be read correctly.""" - original_file_path = cfg.DEFAULT_CONFIG_FILE + """Verify that the primary config file is used by default.""" + original_config_path = cfg.DEFAULT_CONFIG_FILE original_root_directory = cfg.DEFAULT_ROOT - expected_file_path = CONFIG_FILE_WITH_MISSING_OPTIONS - expected_root_directory = 'x-default-root' + expected_config_path = CONFIG_FILE_WITH_MISSING_OPTIONS + root_directory = 'x-default-root' + expected_root_directory = os.path.realpath(root_directory) try: - cfg.DEFAULT_CONFIG_FILE = expected_file_path - cfg.DEFAULT_ROOT = expected_root_directory - file_path, root_directoy = cfg.get_config_file() - self.assertEqual(file_path, expected_file_path) - self.assertEqual(root_directoy, expected_root_directory) + cfg.DEFAULT_CONFIG_FILE = expected_config_path + cfg.DEFAULT_ROOT = root_directory + # reading the config file will fail, but still cfg.root and + # cfg.config_file will be set for parsing the config file + try: + cfg.read() + except configparser.NoOptionError: + pass + self.assertEqual(cfg.config_file, expected_config_path) + self.assertEqual(cfg.root, expected_root_directory) finally: - cfg.DEFAULT_CONFIG_FILE = original_file_path + cfg.DEFAULT_CONFIG_FILE = original_config_path cfg.DEFAULT_ROOT = original_root_directory def test_read_fallback_config_file(self): - """Verify that the fallback config file can be read correctly.""" - original_file_path = cfg.DEFAULT_CONFIG_FILE - original_root_directory = cfg.DEFAULT_ROOT - + """Verify that the correct fallback config file is used""" fallback_root = os.path.realpath('.') fallback_config_file = os.path.join(fallback_root, 'plinth.config') - expected_file_path = os.path.realpath(fallback_config_file) - expected_root_directory = fallback_root - - try: - cfg.DEFAULT_CONFIG_FILE = 'x-non-existant-file' - cfg.DEFAULT_ROOT = 'x-non-existant-directory' - file_path, root_directoy = cfg.get_config_file() - self.assertEqual(file_path, expected_file_path) - self.assertEqual(root_directoy, expected_root_directory) - finally: - cfg.DEFAULT_CONFIG_FILE = original_file_path - cfg.DEFAULT_ROOT = original_root_directory + config_path, root_directory = cfg.get_fallback_config_paths() + cfg.read(config_path, root_directory) + self.assertEqual(cfg.config_file, fallback_config_file) + self.assertEqual(cfg.root, fallback_root) def test_read_missing_config_file(self): """Verify that an exception is raised when there's no config file.""" diff --git a/plinth/tests/test_middleware.py b/plinth/tests/test_middleware.py index 9a54a2e84..56c420bc8 100644 --- a/plinth/tests/test_middleware.py +++ b/plinth/tests/test_middleware.py @@ -38,7 +38,6 @@ class TestSetupMiddleware(TestCase): def setUpClass(cls): """Setup all the test cases.""" super(TestSetupMiddleware, cls).setUpClass() - cfg.read() def setUp(self):