|
| 1 | +******* |
| 2 | +Plugins |
| 3 | +******* |
| 4 | + |
| 5 | +What are plugins? |
| 6 | +----------------- |
| 7 | + |
| 8 | +COMPAS has an extensible architecture based on plugins that allows to |
| 9 | +customize and extend the functionality of the core framework. |
| 10 | + |
| 11 | +For a plugin to work, there needs to exist a counterpart to be connected to. |
| 12 | +This means there are two components involved: |
| 13 | + |
| 14 | +* :meth:`compas.plugins.pluggable` interface: the *extension point* that COMPAS defines |
| 15 | + as the counterpart for plugins to connect to. |
| 16 | +* :meth:`compas.plugins.plugin` implementation: a *concrete implementation* of the |
| 17 | + ``pluggable`` interface. |
| 18 | + |
| 19 | +Both of these components are declared using decorators: |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + @pluggable |
| 24 | + def do_hard_stuff(input): |
| 25 | + pass |
| 26 | +
|
| 27 | + @plugin(pluggable_name='do_hard_stuff') |
| 28 | + def do_hard_stuff_numpy(input): |
| 29 | + # NOTE: Here use the power of numpy to do hard stuff very fast |
| 30 | + # .. |
| 31 | +
|
| 32 | +Once these parts are implemented, the program could simply |
| 33 | +call the function ``do_hard_stuff`` and the appropriate plugin |
| 34 | +implementation using ``numpy`` would be called automatically. |
| 35 | + |
| 36 | + |
| 37 | +Why are plugins important? |
| 38 | +-------------------------- |
| 39 | + |
| 40 | +The example above is just a single code block, but the power of plugins comes |
| 41 | +from the ability to split those two parts -the :meth:`compas.plugins.pluggable` |
| 42 | +and the :meth:`compas.plugins.plugin`- into completely different files, folders |
| 43 | +or even entire projects and still work the same way. |
| 44 | + |
| 45 | +Additionally, COMPAS is able to pick the most suitable plugin implementation |
| 46 | +for its current execution context. For instance, one could have two implementations |
| 47 | +of the same :meth:`compas.plugins.pluggable` definition, one using ``numpy`` and |
| 48 | +another one using *Rhino SDK* and have the correct one automatically selected |
| 49 | +based on where your script is executing. |
| 50 | + |
| 51 | + |
| 52 | +How to make plugins discoverable? |
| 53 | +--------------------------------- |
| 54 | + |
| 55 | +COMPAS plugin discovery is based on naming conventions. This is mainly due to |
| 56 | +the need to support IronPython inside Rhino, which lacks ``setuptools`` |
| 57 | +infrastructure. For more details, check |
| 58 | +`these python guidelines <https://packaging.python.org/guides/creating-and-discovering-plugins/#using-naming-convention>`_. |
| 59 | + |
| 60 | +A COMPAS plugin needs to fulfill two conditions: |
| 61 | + |
| 62 | +* **Name**: The package name should be prefixed with ``compas``, eg. ``compas_cgal``. |
| 63 | +* **Metadata**: The package should define a bit of metadata listing the modules that contain plugins. |
| 64 | + This is done declaring a variable called ``__all_plugins__``, |
| 65 | + eg. ``__all_plugins__ = ['compas_cgal.booleans']``. |
| 66 | + |
| 67 | +COMPAS automatically discovers plugins searching over all available packages in the system, |
| 68 | +and picks up those prefixed with the ``compas`` word. |
| 69 | +All packages are included in the search: packages installed with ``pip``, packages made |
| 70 | +available through the ``PYTHONPATH`` / ``IRONPYTHONPATH``, local packages, etc. |
| 71 | + |
| 72 | +Once a package is found, the metadata in ``__all_plugins__`` is read and all modules |
| 73 | +listed are analyzed to look for functions decorated with the :meth:`compas.plugins.plugin` |
| 74 | +decorator. |
| 75 | + |
| 76 | + |
| 77 | +Two kinds of extension points |
| 78 | +----------------------------- |
| 79 | + |
| 80 | +An extension point, or *pluggable* interface can be declared as being one of two types |
| 81 | +based on how they select which implementation to pick if there are multiple available. |
| 82 | + |
| 83 | +* ``selector='first_match'``: this type of extension point will pick the first plugin |
| 84 | + implementation that satisfies the requirements. |
| 85 | +* ``selector='collect_all'``: extension points defined with this selector will instead |
| 86 | + collect all plugin implementations and execute them all, collecting the return |
| 87 | + values into a list. An example of this is the Rhino install extension |
| 88 | + point: :meth:`compas_rhino.install.installable_rhino_packages`. |
| 89 | + |
| 90 | + |
| 91 | +A complete example |
| 92 | +------------------ |
| 93 | + |
| 94 | +Let's explore a complete example to gain a better understanding. |
| 95 | + |
| 96 | + |
| 97 | +Extension point |
| 98 | +^^^^^^^^^^^^^^^ |
| 99 | + |
| 100 | +For the sake of example, we are going to assume that ``compas`` core defines |
| 101 | +the following :meth:`compas.plugins.pluggable` interface in |
| 102 | + |
| 103 | +**compas/geometry/booleans/__init__.py** |
| 104 | + |
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + @pluggable(category='booleans') |
| 108 | + def boolean_union_mesh_mesh(A, B): |
| 109 | + pass |
| 110 | +
|
| 111 | +
|
| 112 | +Plugin |
| 113 | +^^^^^^ |
| 114 | + |
| 115 | +Now let's write a plugin that implements this interface: |
| 116 | + |
| 117 | +**compas_plugin_sample/__init__.py** |
| 118 | + |
| 119 | +.. code-block:: python |
| 120 | +
|
| 121 | + __all_plugins__ = ['compas_plugin_sample.boolean_trimesh'] |
| 122 | +
|
| 123 | +
|
| 124 | +**compas_plugin_sample/boolean_trimesh.py** |
| 125 | + |
| 126 | +.. code-block:: python |
| 127 | +
|
| 128 | + import trimesh |
| 129 | +
|
| 130 | + @plugin(category='booleans', requires=['trimesh']) |
| 131 | + def boolean_union_mesh_mesh(A, B): |
| 132 | + va, fa = A |
| 133 | + at = trimesh.Trimesh(vertices=va, faces=fa) |
| 134 | +
|
| 135 | + vb, fb = B |
| 136 | + bt = trimesh.Trimesh(vertices=vb, faces=fb) |
| 137 | +
|
| 138 | + r = at.union(bt, engine='scad') |
| 139 | +
|
| 140 | + return r.vertices, r.faces |
| 141 | +
|
| 142 | +Voilà! We have a trimesh-based boolean union plugin! |
| 143 | + |
| 144 | + |
| 145 | +Advanced options |
| 146 | +---------------- |
| 147 | + |
| 148 | +There are a few additional options that plugins can use: |
| 149 | + |
| 150 | +* ``requires``: List of requirements. COMPAS will filter out plugins if their |
| 151 | + requirements list is not satisfied at runtime. This allows to have multiple implementations |
| 152 | + of the same operation and have them selected based on different criteria. |
| 153 | + The requirement can either be a package name string (e.g. ``requires=['scipy']``) or |
| 154 | + a ``callable`` with a boolean return value, in which any arbitrary check can be implemented |
| 155 | + (e.g. ``requires=[lambda: is_rhino_active()]``). |
| 156 | +* ``tryfirst`` and ``trylast``: Plugins cannot control the exact priority they will have |
| 157 | + but they can indicate whether to try to prioritize them or demote them as fallback using |
| 158 | + these two boolean parameters. |
| 159 | +* ``pluggable_name``: Usually, the name of the decorated plugin method matches that of the |
| 160 | + pluggable interface. When that is not the case, the pluggable name can be specified via |
| 161 | + this parameter. |
| 162 | +* ``domain``: extension points are unambiguously identified by a URL that combines domain, |
| 163 | + category and pluggable name. All COMPAS core plugins use the same domain, but other |
| 164 | + packages could potentially decide to use a different domain to ensure collision-free |
| 165 | + naming of pluggable extension points. |
| 166 | + |
| 167 | +While developing plugins, it is also possible to enable print output to understand what |
| 168 | +how plugin selection works behind the scenes. To enable that, set ``DEBUG`` flag |
| 169 | +accordingly: |
| 170 | + |
| 171 | +.. code-block:: python |
| 172 | +
|
| 173 | + from compas.plugins import plugin_manager |
| 174 | + plugin_manager.DEBUG = True |
| 175 | +
|
0 commit comments