diff --git a/sopel/cli/run.py b/sopel/cli/run.py index f569391d0..66fbf3406 100755 --- a/sopel/cli/run.py +++ b/sopel/cli/run.py @@ -219,18 +219,17 @@ def get_configuration(options): .. seealso:: The configuration file is loaded by - :func:`~sopel.cli.run.utils.load_settings` or created using the - configuration wizard. + :func:`~sopel.cli.run.utils.load_settings` """ try: settings = utils.load_settings(options) except config.ConfigurationNotFound as error: - print( - "Welcome to Sopel!\n" - "I can't seem to find the configuration file, " - "so let's generate it!\n") - settings = utils.wizard(error.filename) + raise config.ConfigurationError( + "%s\n" + "If you're just setting up Sopel, welcome! " + "You can use `sopel configure` to get started easily." % error + ) settings._is_daemonized = options.daemonize return settings diff --git a/sopel/cli/utils.py b/sopel/cli/utils.py index e6d68a22e..ebcee052b 100644 --- a/sopel/cli/utils.py +++ b/sopel/cli/utils.py @@ -198,47 +198,55 @@ def enumerate_configs(config_dir, extension='.cfg'): yield item -def find_config(config_dir, name, extension='.cfg'): +def find_config(config_dir: str, name: str, extension: str = '.cfg') -> str: """Build the absolute path for the given configuration file ``name``. - :param str config_dir: path to the configuration directory - :param str name: configuration file ``name`` - :param str extension: configuration file's extension (default to ``.cfg``) - :return: the path of the configuration file, either in the current - directory or from the ``config_dir`` directory + :param config_dir: path to the configuration directory + :param name: configuration file ``name`` + :param extension: configuration file's extension (defaults to ``.cfg``) + :return: the absolute path to the configuration file, either in the current + directory or in the ``config_dir`` directory - This function tries different locations: + This function appends the extension if absent before checking the following: - * the current directory - * the ``config_dir`` directory with the ``extension`` suffix - * the ``config_dir`` directory without a suffix + * If ``name`` is an absolute path, it is returned whether it exists or not + * If ``name`` exists in the ``config_dir``, the absolute path is returned + * If ``name`` exists in the current directory, its absolute path is returned + * Otherwise, the path to the nonexistent file within ``config_dir`` is returned Example:: >>> from sopel.cli import utils >>> from sopel import config + >>> os.getcwd() + '/sopel' >>> os.listdir() ['local.cfg', 'extra.ini'] >>> os.listdir(config.DEFAULT_HOMEDIR) - ['config.cfg', 'extra.ini', 'plugin.cfg', 'README'] + ['config.cfg', 'extra.ini', 'logs', 'plugins'] >>> utils.find_config(config.DEFAULT_HOMEDIR, 'local.cfg') - 'local.cfg' - >>> utils.find_config(config.DEFAULT_HOMEDIR, 'local') - '/home/username/.sopel/local' + '/sopel/local.cfg' >>> utils.find_config(config.DEFAULT_HOMEDIR, 'config') '/home/username/.sopel/config.cfg' >>> utils.find_config(config.DEFAULT_HOMEDIR, 'extra', '.ini') '/home/username/.sopel/extra.ini' + .. versionchanged:: 8.0 + Files in the ``config_dir`` are now preferred, and files without the + requested extension are no longer returned. + """ - if os.path.isfile(name): + if not name.endswith(extension): + name = name + extension + + if os.path.isabs(name): + return name + conf_dir_name = os.path.join(config_dir, name) + if os.path.isfile(conf_dir_name): + return conf_dir_name + elif os.path.isfile(name): return os.path.abspath(name) - name_ext = name + extension - for filename in enumerate_configs(config_dir, extension): - if name_ext == filename: - return os.path.join(config_dir, name_ext) - - return os.path.join(config_dir, name) + return conf_dir_name def add_common_arguments(parser): diff --git a/test/cli/test_cli_run.py b/test/cli/test_cli_run.py index a9bb39b6a..59e61827d 100644 --- a/test/cli/test_cli_run.py +++ b/test/cli/test_cli_run.py @@ -215,7 +215,9 @@ def test_get_configuration(tmpdir): ])) parser = build_parser() - options = parser.parse_args(['start', '-c', 'default.cfg']) + options = parser.parse_args( + ['start', '-c', str(working_dir.join('default.cfg'))] + ) with cd(working_dir.strpath): result = get_configuration(options) diff --git a/test/cli/test_cli_utils.py b/test/cli/test_cli_utils.py index 7a005773c..89ec4fc52 100644 --- a/test/cli/test_cli_utils.py +++ b/test/cli/test_cli_utils.py @@ -118,7 +118,10 @@ def test_find_config_local(tmpdir, config_dir): assert found_config == working_dir.join('local.cfg').strpath found_config = find_config(config_dir.strpath, 'local') - assert found_config == config_dir.join('local').strpath + assert found_config == working_dir.join('local.cfg').strpath + + found_config = find_config(config_dir.strpath, 'config') + assert found_config == config_dir.join('config.cfg').strpath def test_find_config_default(tmpdir, config_dir):