Skip to content

Commit

Permalink
Add new initialize hookspec to make suppress plugins easier
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Jun 19, 2022
1 parent 6160011 commit 8c58e95
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ocrmypdf/_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def get_parser_options_plugins(
plugin_manager = get_plugin_manager(pre_options.plugins)

parser = get_parser()
plugin_manager.hook.initialize( # pylint: disable=no-member
plugin_manager=plugin_manager
)
plugin_manager.hook.add_options(parser=parser) # pylint: disable=no-member

options = parser.parse_args(args=args)
Expand Down
27 changes: 27 additions & 0 deletions src/ocrmypdf/pluginspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ def get_logging_console() -> Handler:
"""


@hookspec
def initialize(plugin_manager: pluggy.PluginManager):
"""Called when this plugin is first loaded into OCRmyPDF.
The primary intended use of this is for plugins to check compatibility with other
plugins and possibly block other blocks, a plugin that wishes to block ocrmypdf's
built-in optimize plugin could do:
.. code-block::
plugin_manager.set_blocked('ocrmypdf.builtin_plugins.optimize')
It would also be reasonable for an plugin implementation to check if it is unable
to proceed, for example, because a required dependency is missing. (If the plugin's
ability to proceed depends on options and arguments, use ``validate`` instead.)
Raises:
ocrmypdf.exceptions.ExitCodeException: If options are not acceptable
and the application should terminate gracefully with an informative
message and error code.
Note:
This hook will be called from the main process, and may modify global state
before child worker processes are forked.
"""


@hookspec
def add_options(parser: ArgumentParser) -> None:
"""Allows the plugin to add its own command line and API arguments.
Expand Down

0 comments on commit 8c58e95

Please sign in to comment.