Skip to content

Commit

Permalink
cli: utils.get_many_text
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Jul 20, 2019
1 parent 6ec2dea commit ce41e85
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
46 changes: 18 additions & 28 deletions sopel/cli/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,12 @@ def _handle_disable_plugin(settings, plugin_name, force):

def display_unknown_plugins(unknown_plugins):
# at least one of the plugins does not exist
unknown_count = len(unknown_plugins)
if unknown_count == 1:
tools.stderr('No plugin named %s.' % unknown_plugins[0])
elif unknown_count == 2:
tools.stderr('No plugin named %s or %s.' % unknown_plugins)
else:
left = ', '.join(unknown_plugins[:-1])
last = unknown_plugins[-1]
tools.stderr('No plugin named %s, or %s.' % (left, last))
tools.stderr(utils.get_many_text(
unknown_plugins,
one='No plugin named {item}.',
two='No plugin named {first} or {second}.',
many='No plugin named {left}, or {last}.'
))


def handle_disable(options):
Expand Down Expand Up @@ -393,15 +390,12 @@ def handle_disable(options):
return 0 # nothing to disable or save, but not an error case

# display plugins actually disabled by the command
plugins_count = len(actually_disabled)
if plugins_count == 1:
print('Plugin %s disabled.' % actually_disabled[0])
elif plugins_count == 2:
print('Plugin %s and %s disabled.' % actually_disabled)
else:
left = ', '.join(actually_disabled[:-1])
last = actually_disabled[-1]
print('Plugin %s, and %s disabled.' % (left, last))
print(utils.get_many_text(
actually_disabled,
one='Plugin {item} disabled.',
two='Plugin {first} and {second} disabled.',
many='Plugin {left}, and {last} disabled.'
))

return 0

Expand Down Expand Up @@ -481,16 +475,12 @@ def handle_enable(options):
return 0 # nothing to disable or save, but not an error case

# display plugins actually disabled by the command
plugins_count = len(actually_enabled)
if plugins_count == 1:
print('Plugin %s enabled.' % actually_enabled[0])
elif plugins_count == 2:
print('Plugin %s and %s enabled.' % actually_enabled)
else:
left = ', '.join(actually_enabled[:-1])
last = actually_enabled[-1]
print('Plugin %s, and %s enabled.' % (left, last))

print(utils.get_many_text(
actually_enabled,
one='Plugin {item} enabled.',
two='Plugin {first} and {second} enabled.',
many='Plugin {left}, and {last} enabled.'
))
return 0


Expand Down
20 changes: 20 additions & 0 deletions sopel/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,23 @@ def redirect_outputs(settings, is_quiet=False):
logfile = os.path.os.path.join(settings.core.logdir, settings.basename + '.stdio.log')
sys.stderr = tools.OutputRedirect(logfile, True, is_quiet)
sys.stdout = tools.OutputRedirect(logfile, False, is_quiet)


def get_many_text(items, one, two, many):
"""Get the right text based on the number of ``items``."""
message = ''
if not items:
return message

items_count = len(items)

if items_count == 1:
message = one.format(item=items[0], items=items)
elif items_count == 2:
message = two.format(first=items[0], second=items[1], items=items)
else:
left = ', '.join(items[:-1])
last = items[-1]
message = many.format(left=left, last=last, items=items)

return message
26 changes: 25 additions & 1 deletion test/cli/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

import pytest

from sopel.cli.utils import enumerate_configs, find_config, add_common_arguments
from sopel.cli.utils import (
add_common_arguments,
enumerate_configs,
find_config,
get_many_text,
)


@contextmanager
Expand Down Expand Up @@ -124,3 +129,22 @@ def test_add_common_arguments_subparser():

options = parser.parse_args(['sub', '--config', 'test-long'])
assert options.config == 'test-long'


MANY_TEXTS = (
([], ''),
(['a'], 'the a element'),
(['a', 'b'], 'elements a and b'),
(['a', 'b', 'c'], 'elements a, b, and c'),
(['a', 'b', 'c', 'd'], 'elements a, b, c, and d'),
)


@pytest.mark.parametrize('items, expected', MANY_TEXTS)
def test_get_many_text(items, expected):
result = get_many_text(
items,
'the {item} element',
'elements {first} and {second}',
'elements {left}, and {last}')
assert result == expected

0 comments on commit ce41e85

Please sign in to comment.