Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: look harder for $PWD/cfg, don't automatically start wizard #2299

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions sopel/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
half-duplex marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
50 changes: 29 additions & 21 deletions sopel/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
dgw marked this conversation as resolved.
Show resolved Hide resolved
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):
Exirel marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand Down
4 changes: 3 additions & 1 deletion test/cli/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion test/cli/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down