Core API

class openpaperwork_core.Core(auto_load_dependencies=False)[source]

Manage plugins and their callbacks.

call_all(callback_name, *args, **kwargs)[source]

Call all the methods of all the plugins that have callback_name as name. Arguments are passed as is. Returned values are dropped (use callbacks for return values if required)

Method call order is defined by the plugin priorities: Plugins with a higher priority get their methods called first.

When we need a return value from callbacks called with call_all(), we need a way to get the results from all of them. The usual way to do that is to instantiate an empty list or set, and pass it as first argument of the callbacks (argument out). Callbacks can then complete this list or set using list.append() or set.add().

Caller -> Core: call "func"
Core -> "Plugin A": plugin.func()
Core <- "Plugin A": returns "something_a"
Core -> "Plugin B": plugin.func()
Core <- "Plugin B": returns "something_b"
Core -> "Plugin C": plugin.func()
Core <- "Plugin C": returns "something_c"
Caller <- Core: returns 3

call_one(callback_name, *args, **kwargs)[source]

Look for a plugin method called callback_name and calls it. Raises an error if no such method exists. If many exists, call one at random. Returns the value return by the callback.

Method call order is defined by the plugin priorities: Plugins with a higher priority get their methods called first.

Caller -> Core: call "func"
Core -> "Plugin A": plugin.func()
Core <- "Plugin A": returns X
Caller <- Core: returns X

You’re advised to use call_all() or call_success() instead whenever possible. This method is only provided as convenience for when you’re fairly sure there should be only one plugin with such callback (example: mainloop plugins).

call_success(callback_name, *args, **kwargs)[source]

Call methods of all the plugins that have callback_name as name until one of them return a value that is not None. Arguments are passed as is. First value to be different from None is returned. If none of the callbacks returned a value different from None or if no callback has the specified name, this method will return None.

Method call order is defined by the plugin priorities: Plugins with a higher priority get their methods called first.

Callbacks should never raise any exception.

Caller -> Core: call "func"
Core -> "Plugin A": plugin.func()
Core <- "Plugin A": returns None
Core -> "Plugin B": plugin.func()
Core <- "Plugin B": returns None
Core -> "Plugin C": plugin.func()
Core <- "Plugin C": returns "something"
Caller <- Core: returns "something"

get_active_plugins()[source]
get_by_interface(interface_name)[source]
get_by_name(module_name)[source]

Returns a Plugin instance based on the corresponding module name (assuming it has been loaded).

You shouldn’t use this function, except for: - unit tests - configuration (see cmd.plugins)

get_deps(plugin_name)[source]
get_plugins()[source]

You shouldn’t use this function, except for: - unit tests - configuration (see cmd.plugins)

init()[source]
  • Make sure all the dependencies of all the plugins are satisfied.

  • Call the method init() of each plugin following the dependency order (those without dependencies are called first).

BEWARE of dependency loops !

load(module_name)[source]
  • Load the specified module

  • Instantiate the class ‘Plugin()’ of this module

  • Register all the methods of this plugin object (except those starting by ‘_’ and those from the class PluginBase) as callbacks

BEWARE of dependency loops !

Arguments:
  • module_name: name of the Python module to load

exception openpaperwork_core.DependencyException[source]

Failed to satisfy dependencies.

class openpaperwork_core.PluginBase[source]

Indicates all the methods that must be implemented by any plugin managed by OpenPaperwork core. Also provides default implementations for each method.

PRIORITY = 0
USER_VISIBLE = False
get_deps()[source]

Return the dependencies required by this plugin.

Example:

[
  {
    "interface": "some_interface_name",  # required
    "defaults": ['plugin_a', 'plugin_b'],  # required
    "expected_already_satisfied": False,  # optional, default: True
  },
]
get_interfaces()[source]

Indicates the list of interfaces implemented by this plugin. Interface names are arbitrarily defined. Methods provided by each interface are arbitrarily defined (and no checks are done).

Returns a list of string.

init(core)[source]

Plugins can initialize whatever they want here. When called, all dependencies have been loaded and initialized, so using them is safe. Does nothing by default.