-
Notifications
You must be signed in to change notification settings - Fork 343
Register plugin from entry points #1872
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
Open
Ni-g-3l
wants to merge
24
commits into
AcademySoftwareFoundation:main
Choose a base branch
from
Ni-g-3l:feat/register_plugin_from_entry_points
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
91b3a3c
Add test data to load plugins from entry points
Ni-g-3l f8a8f49
Load REZ plugins from setuptools entry points
Ni-g-3l da9e66c
Fix flake8 report
Ni-g-3l 7fbed7c
Fix import of importlib metadata in python 3.8, 3.9
Ni-g-3l e776bb9
Signed-off-by: Nig3l <[email protected]>
Ni-g-3l 55ed2ce
Remove dupplicated plugin definition
Ni-g-3l ee6f9bf
Install test plugin to test data to speed up test
Ni-g-3l e6a3256
Add try except on load plugin from entry points
Ni-g-3l e30e7cd
Update plugin install doc
Ni-g-3l 0742acf
use modern build backend, use dist-info, fix tests and simplify file …
JeanChristopheMorinPerso 4e6a47a
Update src/rez/tests/util.py
JeanChristopheMorinPerso 25c020a
Remove vendored code for safety reason.
JeanChristopheMorinPerso 9579331
Re-add importlib-metadata (this time with the right version to suppor…
JeanChristopheMorinPerso ac70f9a
Make it compatible with python 3.8 and 3.9
JeanChristopheMorinPerso ffbe7f6
Fix python compat again...
JeanChristopheMorinPerso ab8e162
Last fix for real
JeanChristopheMorinPerso a7fa195
Fix flake8
JeanChristopheMorinPerso cab8f04
Add new plugin install methods
Ni-g-3l 9d939f1
Organize entrypoints by plugin type to avoid having plugins being reg…
JeanChristopheMorinPerso 46219f0
Use entrypoint name for plugin name instead of module name
JeanChristopheMorinPerso fc381b0
Add debug logs to match the other plugin loading modes
JeanChristopheMorinPerso 861e0f1
Fix tests
JeanChristopheMorinPerso 8ed6899
First pass at improving the plugins documentation
JeanChristopheMorinPerso cfc7599
Merge branch 'main' into feat/register_plugin_from_entry_points
JeanChristopheMorinPerso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
========================== | ||
Developing your own plugin | ||
========================== | ||
|
||
This guide will walk you through writing a rez plugin. | ||
|
||
Structure | ||
========= | ||
|
||
If you decide to register your plugin using the :ref:`entry-points method <plugin-entry-points>`, you are free | ||
to structure your plugin however you like. | ||
|
||
If you decide to implement your plugins using the ``rezplugins`` namespace package, please | ||
refer to :ref:`rezplugins-structure` to learn about the file structure that you will need to follow. | ||
|
||
Registering subcommands | ||
======================= | ||
|
||
Optionally, plugins can provide new ``rez`` subcommands. | ||
|
||
To register a plugin and expose a new subcommand, the plugin module: | ||
|
||
- MUST have a module-level docstring (used as the command help) | ||
- MUST provide a ``setup_parser()`` function | ||
- MUST provide a ``command()`` function | ||
- MUST provide a ``register_plugin()`` function | ||
- SHOULD have a module-level attribute ``command_behavior`` | ||
|
||
For example, a plugin named ``foo`` and this is the ``foo.py`` in the plugin type | ||
root directory: | ||
|
||
.. code-block:: python | ||
:caption: foo.py | ||
|
||
"""The docstring for command help, this is required.""" | ||
import argparse | ||
|
||
command_behavior = { | ||
"hidden": False, # optional: bool | ||
"arg_mode": None, # optional: None, "passthrough", "grouped" | ||
} | ||
|
||
|
||
def setup_parser(parser: argparse.ArgumentParser): | ||
parser.add_argument("--hello", action="store_true") | ||
|
||
|
||
def command( | ||
opts: argparse.Namespace, | ||
parser: argparse.ArgumentParser, | ||
extra_arg_groups: list[list[str]], | ||
): | ||
if opts.hello: | ||
print("world") | ||
|
||
|
||
def register_plugin(): | ||
"""This function is your plugin entry point. Rez will call this function.""" | ||
|
||
# import here to avoid circular imports. | ||
from rez.command import Command | ||
|
||
class CommandFoo(Command): | ||
# This is where you declare the settings the plugin accepts. | ||
schema_dict = { | ||
"str_option": str, | ||
"int_option": int, | ||
} | ||
|
||
@classmethod | ||
def name(cls): | ||
return "foo" | ||
|
||
return CommandFoo | ||
|
||
Install plugins | ||
=============== | ||
|
||
1. Copy directly to rez install folder | ||
|
||
To make your plugin available to rez, you can install it directly under | ||
``src/rezplugins`` (that's called a namespace package). | ||
|
||
2. Add the source path to :envvar:`REZ_PLUGIN_PATH` | ||
|
||
Add the source path to the REZ_PLUGIN_PATH environment variable in order to make your plugin available to rez. | ||
|
||
3. Add entry points to pyproject.toml | ||
|
||
To make your plugin available to rez, you can also create an entry points section in your | ||
``pyproject.toml`` file, that will allow you to install your plugin with `pip install` command. | ||
|
||
.. code-block:: toml | ||
:caption: pyproject.toml | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "foo" | ||
version = "0.1.0" | ||
|
||
[project.entry-points."rez.plugins"] | ||
foo_cmd = "foo" | ||
|
||
|
||
4. Create a setup.py | ||
|
||
To make your plugin available to rez, you can also create a ``setup.py`` file, | ||
that will allow you to install your plugin with `pip install` command. | ||
|
||
.. code-block:: python | ||
:caption: setup.py | ||
|
||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name="foo", | ||
version="0.1.0", | ||
package_dir={ | ||
"foo": "foo" | ||
}, | ||
packages=find_packages(where="."), | ||
entry_points={ | ||
'rez.plugins': [ | ||
'foo_cmd = foo', | ||
] | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ This section contains various user guides. | |
:maxdepth: 1 | ||
|
||
update_to_3 | ||
developing_your_own_plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/rez/data/tests/extensions/baz/baz-0.1.0.dist-info/METADATA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Metadata-Version: 2.4 | ||
Name: baz | ||
Version: 0.1.0 |
2 changes: 2 additions & 0 deletions
2
src/rez/data/tests/extensions/baz/baz-0.1.0.dist-info/entry_points.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[rez.plugins] | ||
baz_cmd = baz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
baz plugin | ||
""" | ||
|
||
from rez.command import Command | ||
|
||
# This attribute is optional, default behavior will be applied if not present. | ||
command_behavior = { | ||
"hidden": False, # (bool): default False | ||
"arg_mode": None, # (str): "passthrough", "grouped", default None | ||
} | ||
|
||
|
||
def setup_parser(parser, completions=False): | ||
parser.add_argument( | ||
"-m", "--message", action="store_true", help="Print message from world." | ||
) | ||
|
||
|
||
def command(opts, parser=None, extra_arg_groups=None): | ||
from baz import core | ||
|
||
if opts.message: | ||
msg = core.get_message_from_baz() | ||
print(msg) | ||
return | ||
|
||
print("Please use '-h' flag to see what you can do to this world !") | ||
|
||
|
||
class BazCommand(Command): | ||
@classmethod | ||
def name(cls): | ||
return "baz_cmd" | ||
|
||
|
||
def register_plugin(): | ||
return BazCommand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def get_message_from_baz(): | ||
from rez.config import config | ||
message = config.plugins.command.baz.message | ||
return message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
baz = { | ||
"message": "welcome to this world." | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section still needs some work. I didn't touch it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the sections that are need improvements ? Have you specific things in mind ?