Skip to content

Commit

Permalink
cli: add -H/--homedir common option
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed May 12, 2019
1 parent a36a434 commit d3d12ea
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 36 deletions.
23 changes: 12 additions & 11 deletions sopel/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import argparse
import os

from sopel import tools, config
from sopel import tools
from . import utils


Expand All @@ -24,9 +24,11 @@ def build_parser():
'list',
help="List available configurations from Sopel's homedir",
description="""
List available configurations from Sopel's homedir ({homedir})
with the extension "{ext}".
""".format(homedir=config.DEFAULT_HOMEDIR, ext='.cfg'))
List available configurations from Sopel's homedir
with the extension "{ext}". Use option -H/--homedir to use a
specific homedir.
""".format(ext='.cfg'))
utils.add_common_arguments(list_parser)
list_parser.add_argument(
'-e', '--ext', '--extension',
dest='extension',
Expand Down Expand Up @@ -75,15 +77,16 @@ def handle_list(options):
It is possible to filter by extension using the ``-e/--ext/--extension``
option; default is ``.cfg`` (the ``.`` prefix is not required).
"""
display_path = getattr(options, 'display_path', False)
extension = getattr(options, 'extension', '.cfg')
homedir = options.homedir
display_path = options.display_path
extension = options.extension
if not extension.startswith('.'):
extension = '.' + extension
configs = utils.enumerate_configs(config.DEFAULT_HOMEDIR, extension)
configs = utils.enumerate_configs(homedir, extension)

for config_filename in configs:
if display_path:
print(os.path.join(config.DEFAULT_HOMEDIR, config_filename))
print(os.path.join(homedir, config_filename))
else:
name, _ = os.path.splitext(config_filename)
print(name)
Expand All @@ -101,9 +104,7 @@ def handle_init(options):
extension **must be** ``.cfg``.
"""
config_filename = utils.find_config(
config.DEFAULT_HOMEDIR,
getattr(options, 'config', None) or 'default')
config_filename = utils.find_config(options.homedir, options.config)
config_name, ext = os.path.splitext(config_filename)

if ext and ext != '.cfg':
Expand Down
27 changes: 15 additions & 12 deletions sopel/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ def add_legacy_options(parser):
"use `sopel restart` instead)"))
parser.add_argument("-l", '--list', action="store_true",
dest="list_configs",
help="List all config files found")
help=(
"List all config files found"
"(deprecated, and will be removed in Sopel 8; "
"use `sopel-config list` instead)"))
parser.add_argument('--quiet', action="store_true", dest="quiet",
help="Suppress all output")
parser.add_argument('-w', '--configure-all', action='store_true',
Expand Down Expand Up @@ -268,10 +271,10 @@ def print_version():
print('https://sopel.chat/')


def print_config():
def print_config(homedir):
"""Print list of available configurations from default homedir."""
configs = utils.enumerate_configs(config.DEFAULT_HOMEDIR)
print('Config files in %s:' % config.DEFAULT_HOMEDIR)
configs = utils.enumerate_configs(homedir)
print('Config files in %s:' % homedir)
configfile = None
for configfile in configs:
print('\t%s' % configfile)
Expand Down Expand Up @@ -324,7 +327,7 @@ def get_pid_filename(options, pid_dir):
``sopel-{basename}.pid`` instead.
"""
name = 'sopel.pid'
if options.config:
if options.config and options.config != 'default':
basename = os.path.basename(options.config)
if basename.endswith('.cfg'):
basename = basename[:-4]
Expand Down Expand Up @@ -406,9 +409,8 @@ def command_start(opts):

def command_configure(opts):
"""Sopel Configuration Wizard"""
configpath = utils.find_config(
config.DEFAULT_HOMEDIR, opts.config or 'default')
if getattr(opts, 'modules', False):
configpath = utils.find_config(opts.homedir, opts.config)
if opts.modules:
utils.plugins_wizard(configpath)
else:
utils.wizard(configpath)
Expand Down Expand Up @@ -520,9 +522,7 @@ def command_legacy(opts):
print_version()
return

# TODO: allow to use a different homedir
configpath = utils.find_config(
config.DEFAULT_HOMEDIR, opts.config or 'default')
configpath = utils.find_config(opts.homedir, opts.config)

if opts.wizard:
tools.stderr(
Expand All @@ -539,7 +539,10 @@ def command_legacy(opts):
return

if opts.list_configs:
print_config()
tools.stderr(
'WARNING: option --list is deprecated; '
'use `sopel-config list` instead')
print_config(opts.homedir)
return

# Step Two: Get the configuration file and prepare to run
Expand Down
22 changes: 16 additions & 6 deletions sopel/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,21 @@ def add_common_arguments(parser):
:type parser: argparse.ArgumentParser
This functions adds the common arguments for Sopel's command line tools.
At the moment, this functions adds only one argument to the parser: the
argument used as the standard way to define a configuration filename.
It adds the following arguments:
* ``-c/--config``: the name of the Sopel config, or its absolute path
* ``-H/--homedir``: the directory to look for Sopel config
This can be used on an argument parser, or an argument subparser, to handle
these cases::
[sopel-command] -c [filename]
[sopel-command] [action] -c [filename]
[sopel-command] --homedir [directory] -c [name]
Then, when the parser parses the command line arguments, it will expose
a ``config`` option to be used to find and load Sopel's settings.
``config`` and ``homedir`` options that can be used to find and load
Sopel's settings.
.. seealso::
Expand All @@ -219,10 +223,15 @@ def add_common_arguments(parser):
"""
parser.add_argument(
'-c', '--config',
default=None,
default='default',
metavar='filename',
dest='config',
help='Use a specific configuration file')
help='Use a specific configuration file.')
parser.add_argument(
'-H', '--homedir',
default=config.DEFAULT_HOMEDIR,
dest='homedir',
help='Look for configuration from this Sopel homedir.')


def load_settings(options):
Expand Down Expand Up @@ -262,7 +271,8 @@ def load_settings(options):
elif 'SOPEL_CONFIG' in os.environ:
name = os.environ['SOPEL_CONFIG'] or name # use default if empty

filename = find_config(config.DEFAULT_HOMEDIR, name)
filename = find_config(
getattr(options, 'homedir', config.DEFAULT_HOMEDIR), name)

if not os.path.isfile(filename):
raise config.ConfigurationNotFound(filename=filename)
Expand Down
65 changes: 60 additions & 5 deletions test/cli/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_build_parser_legacy():

assert isinstance(options, argparse.Namespace)
assert hasattr(options, 'config')
assert hasattr(options, 'homedir')
assert hasattr(options, 'daemonize')
assert hasattr(options, 'quiet')
assert hasattr(options, 'quit')
Expand All @@ -57,7 +58,8 @@ def test_build_parser_legacy():
assert hasattr(options, 'mod_wizard')
assert hasattr(options, 'list_configs')

assert options.config is None
assert options.config == 'default'
assert options.homedir == config.DEFAULT_HOMEDIR
assert options.daemonize is False
assert options.quiet is False
assert options.quit is False
Expand All @@ -79,6 +81,15 @@ def test_build_parser_legacy_config():
assert options.config == 'custom'


def test_build_parser_legacy_homedir():
parser = build_parser()
options = parser.parse_args(['legacy', '-H', 'custom'])
assert options.homedir == 'custom'

options = parser.parse_args(['legacy', '--homedir', 'custom'])
assert options.homedir == 'custom'


def test_build_parser_legacy_daemonize():
parser = build_parser()
options = parser.parse_args(['legacy', '-d'])
Expand Down Expand Up @@ -167,10 +178,12 @@ def test_build_parser_start():

assert isinstance(options, argparse.Namespace)
assert hasattr(options, 'config')
assert hasattr(options, 'homedir')
assert hasattr(options, 'daemonize')
assert hasattr(options, 'quiet')

assert options.config is None
assert options.config == 'default'
assert options.homedir == config.DEFAULT_HOMEDIR
assert options.daemonize is False
assert options.quiet is False

Expand All @@ -185,6 +198,15 @@ def test_build_parser_start_config():
assert options.config == 'custom'


def test_build_parser_start_homedir():
parser = build_parser()
options = parser.parse_args(['start', '-H', 'custom'])
assert options.homedir == 'custom'

options = parser.parse_args(['start', '--homedir', 'custom'])
assert options.homedir == 'custom'


def test_build_parser_start_daemonize():
parser = build_parser()

Expand All @@ -209,10 +231,12 @@ def test_build_parser_stop():

assert isinstance(options, argparse.Namespace)
assert hasattr(options, 'config')
assert hasattr(options, 'homedir')
assert hasattr(options, 'kill')
assert hasattr(options, 'quiet')

assert options.config is None
assert options.config == 'default'
assert options.homedir == config.DEFAULT_HOMEDIR
assert options.kill is False
assert options.quiet is False

Expand All @@ -227,6 +251,15 @@ def test_build_parser_stop_config():
assert options.config == 'custom'


def test_build_parser_stop_homedir():
parser = build_parser()
options = parser.parse_args(['stop', '-H', 'custom'])
assert options.homedir == 'custom'

options = parser.parse_args(['stop', '--homedir', 'custom'])
assert options.homedir == 'custom'


def test_build_parser_stop_kill():
parser = build_parser()

Expand All @@ -251,9 +284,11 @@ def test_build_parser_restart():

assert isinstance(options, argparse.Namespace)
assert hasattr(options, 'config')
assert hasattr(options, 'homedir')
assert hasattr(options, 'quiet')

assert options.config is None
assert options.config == 'default'
assert options.homedir == config.DEFAULT_HOMEDIR
assert options.quiet is False


Expand All @@ -267,6 +302,15 @@ def test_build_parser_restart_config():
assert options.config == 'custom'


def test_build_parser_restart_homedir():
parser = build_parser()
options = parser.parse_args(['restart', '-H', 'custom'])
assert options.homedir == 'custom'

options = parser.parse_args(['restart', '--homedir', 'custom'])
assert options.homedir == 'custom'


def test_build_parser_restart_quiet():
parser = build_parser()

Expand All @@ -281,9 +325,11 @@ def test_build_parser_configure():

assert isinstance(options, argparse.Namespace)
assert hasattr(options, 'config')
assert hasattr(options, 'homedir')
assert hasattr(options, 'modules')

assert options.config is None
assert options.config == 'default'
assert options.homedir == config.DEFAULT_HOMEDIR
assert options.modules is False


Expand All @@ -297,6 +343,15 @@ def test_build_parser_configure_config():
assert options.config == 'custom'


def test_build_parser_configure_homedir():
parser = build_parser()
options = parser.parse_args(['configure', '-H', 'custom'])
assert options.homedir == 'custom'

options = parser.parse_args(['configure', '--homedir', 'custom'])
assert options.homedir == 'custom'


def test_build_parser_configure_modules():
parser = build_parser()

Expand Down
Loading

0 comments on commit d3d12ea

Please sign in to comment.