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

Allow with_plugins() to accept a string #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ python:
- 3.5
- 3.6
- 3.7
- 3.8
- 3.9

env:
matrix:
Expand Down
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ The only requirement is decorating ``click.group()`` with ``click_plugins.with_p
which handles attaching external commands and groups. In this case the core CLI developer
registers CLI plugins from ``core_package.cli_plugins``.

.. code-block:: python

import click
from click_plugins import with_plugins


@with_plugins('core_package.cli_plugins')
@click.group()
def cli():
"""Commandline interface for yourpackage."""

@cli.command()
def subcommand():
"""Subcommand that does something."""

Alternatively, an iterable producing one ``pkg_resources.EntryPoint()`` per
interation (like ``pkg_resources.iter_entry_points()``) can be used.

.. code-block:: python

from pkg_resources import iter_entry_points
Expand Down
4 changes: 1 addition & 3 deletions click_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
entry-points.


from pkg_resources import iter_entry_points

import click
from click_plugins import with_plugins


@with_plugins(iter_entry_points('entry_point.name'))
@with_plugins('entry_point.name')
@click.group()
def cli():
'''Commandline interface for something.'''
Expand Down
12 changes: 9 additions & 3 deletions click_plugins/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import sys
import traceback
from pkg_resources import iter_entry_points


def with_plugins(plugins):
Expand All @@ -18,8 +19,9 @@ def with_plugins(plugins):

Parameters
----------
plugins : iter
An iterable producing one `pkg_resources.EntryPoint()` per iteration.
plugins : str | iter
An entry point group name.
Alternatively an iterable producing one `pkg_resources.EntryPoint()` per iteration.
attrs : **kwargs, optional
Additional keyword arguments for instantiating `click.Group()`.

Expand All @@ -32,7 +34,11 @@ def decorator(group):
if not isinstance(group, click.Group):
raise TypeError("Plugins can only be attached to an instance of click.Group()")

for entry_point in plugins or ():
entry_points = plugins
if type(plugins) is str:
entry_points = iter_entry_points(plugins)

for entry_point in entry_points or ():
try:
group.add_command(entry_point.load())
except Exception:
Expand Down
4 changes: 1 addition & 3 deletions example/PrintIt/printit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
"""


from pkg_resources import iter_entry_points

import click
from click_plugins import with_plugins


@with_plugins(iter_entry_points('printit.plugins'))
@with_plugins('printit.plugins')
@click.group()
def cli():

Expand Down
14 changes: 11 additions & 3 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def broken_cli():
"""Broken CLI group."""
pass

# Passing the group name instead of the entry points iterator
@with_plugins('_test_click_plugins.test_plugins')
@click.group()
def good_cli_by_name():
"""Good CLI group referenced by name."""
pass


def test_registered():
# Make sure the plugins are properly registered. If this test fails it
Expand All @@ -75,13 +82,14 @@ def test_registered():
assert len([ep for ep in iter_entry_points('_test_click_plugins.broken_plugins')]) > 1


def test_register_and_run(runner):
@pytest.mark.parametrize("cli", [good_cli, good_cli_by_name])
def test_register_and_run(runner, cli):

result = runner.invoke(good_cli)
result = runner.invoke(cli)
assert result.exit_code == 0

for ep in iter_entry_points('_test_click_plugins.test_plugins'):
cmd_result = runner.invoke(good_cli, [ep.name, 'something'])
cmd_result = runner.invoke(cli, [ep.name, 'something'])
assert cmd_result.exit_code == 0
assert cmd_result.output.strip() == 'passed'

Expand Down