octoprint.plugin

This module represents OctoPrint’s plugin subsystem. This includes management and helper methods as well as the registered plugin types.

octoprint.plugin.plugin_manager(init=False, plugin_folders=None, plugin_types=None, plugin_entry_points=None, plugin_disabled_list=None, plugin_restart_needing_hooks=None, plugin_obsolete_hooks=None, plugin_validators=None)

Factory method for initially constructing and consecutively retrieving the PluginManager singleton.

Parameters:
  • init (boolean) – A flag indicating whether this is the initial call to construct the singleton (True) or not (False, default). If this is set to True and the plugin manager has already been initialized, a ValueError will be raised. The same will happen if the plugin manager has not yet been initialized and this is set to False.
  • plugin_folders (list) – A list of folders (as strings containing the absolute path to them) in which to look for potential plugin modules. If not provided this defaults to the configured plugins base folder and src/plugins within OctoPrint’s code base.
  • plugin_types (list) – A list of recognized plugin types for which to look for provided implementations. If not provided this defaults to the plugin types found in octoprint.plugin.types without OctoPrintPlugin.
  • plugin_entry_points (list) – A list of entry points pointing to modules which to load as plugins. If not provided this defaults to the entry point octoprint.plugin.
  • plugin_disabled_list (list) – A list of plugin identifiers that are currently disabled. If not provided this defaults to all plugins for which enabled is set to False in the settings.
  • plugin_restart_needing_hooks (list) – A list of hook namespaces which cause a plugin to need a restart in order be enabled/disabled. Does not have to contain full hook identifiers, will be matched with startswith similar to logging handlers
  • plugin_obsolete_hooks (list) – A list of hooks that have been declared obsolete. Plugins implementing them will not be enabled since they might depend on functionality that is no longer available.
  • plugin_validators (list) – A list of additional plugin validators through which to process each plugin.
Returns:

A fully initialized PluginManager instance to be used for plugin management tasks.

Return type:

PluginManager

Raises:

ValueErrorinit was True although the plugin manager was already initialized, or it was False although the plugin manager was not yet initialized.

octoprint.plugin.plugin_settings(plugin_key, defaults=None, get_preprocessors=None, set_preprocessors=None)

Factory method for creating a PluginSettings instance.

Parameters:
  • plugin_key (string) – The plugin identifier for which to create the settings instance.
  • defaults (dict) – The default settings for the plugin.
  • get_preprocessors (dict) – The getter preprocessors for the plugin.
  • set_preprocessors (dict) – The setter preprocessors for the plugin.
Returns:

A fully initialized PluginSettings instance to be used to access the plugin’s settings

Return type:

PluginSettings

octoprint.plugin.call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None)

Helper method to invoke the indicated method on all registered plugin implementations implementing the indicated types. Allows providing method arguments and registering callbacks to call in case of success and/or failure of each call which can be used to return individual results to the calling code.

Example:

def my_success_callback(name, plugin, result):
    print("{name} was called successfully and returned {result!r}".format(**locals()))

def my_error_callback(name, plugin, exc):
    print("{name} raised an exception: {exc!s}".format(**locals()))

octoprint.plugin.call_plugin(
    [octoprint.plugin.StartupPlugin],
    "on_startup",
    args=(my_host, my_port),
    callback=my_success_callback,
    error_callback=my_error_callback
)
Parameters:
  • types (list) – A list of plugin implementation types to match against.
  • method (string) – Name of the method to call on all matching implementations.
  • args (tuple) – A tuple containing the arguments to supply to the called method. Optional.
  • kwargs (dict) – A dictionary containing the keyword arguments to supply to the called method. Optional.
  • callback (function) – A callback to invoke after an implementation has been called successfully. Will be called with the three arguments name, plugin and result. name will be the plugin identifier, plugin the plugin implementation instance itself and result the result returned from the method invocation.
  • error_callback (function) – A callback to invoke after the call of an implementation resulted in an exception. Will be called with the three arguments name, plugin and exc. name will be the plugin identifier, plugin the plugin implementation instance itself and exc the caught exception.
class octoprint.plugin.PluginSettings(settings, plugin_key, defaults=None, get_preprocessors=None, set_preprocessors=None)

The PluginSettings class is the interface for plugins to their own or globally defined settings.

It provides a couple of convenience methods for directly accessing plugin settings via the regular octoprint.settings.Settings interfaces as well as means to access plugin specific folder locations.

All getter and setter methods will ensure that plugin settings are stored in their correct location within the settings structure by modifying the supplied paths accordingly.

Parameters:
  • settings (Settings) – The Settings instance on which to operate.
  • plugin_key (str) – The plugin identifier of the plugin for which to create this instance.
  • defaults (dict) – The plugin’s defaults settings, will be used to determine valid paths within the plugin’s settings structure
get(path, merged=False, asdict=False)

Retrieves a raw value from the settings for path, optionally merging the raw value with the default settings if merged is set to True.

Parameters:
  • path (list, tuple) – The path for which to retrieve the value.
  • merged (boolean) – Whether to merge the returned result with the default settings (True) or not (False, default).
Returns:

The retrieved settings value.

Return type:

object

get_int(path)

Like get() but tries to convert the retrieved value to int.

get_float(path)

Like get() but tries to convert the retrieved value to float.

get_boolean(path)

Like get() but tries to convert the retrieved value to boolean.

set(path, value, force=False)

Sets the raw value on the settings for path.

Parameters:
  • path (list, tuple) – The path for which to retrieve the value.
  • value (object) – The value to set.
  • force (boolean) – If set to True, the modified configuration will even be written back to disk if the value didn’t change.
set_int(path, value, force=False)

Like set() but ensures the value is an int through attempted conversion before setting it.

set_float(path, value, force=False)

Like set() but ensures the value is an float through attempted conversion before setting it.

set_boolean(path, value, force=False)

Like set() but ensures the value is an boolean through attempted conversion before setting it.

getPluginLogfilePath(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by get_plugin_logfile_path()

get_plugin_data_folder(*args, **kwargs)

Deprecated since version 1.2.0: Replaced by get_plugin_data_folder()

get_plugin_logfile_path(postfix=None)

Retrieves the path to a logfile specifically for the plugin. If postfix is not supplied, the logfile will be named plugin_<plugin identifier>.log and located within the configured logs folder. If a postfix is supplied, the name will be plugin_<plugin identifier>_<postfix>.log at the same location.

Plugins may use this for specific logging tasks. For example, a SlicingPlugin might want to create a log file for logging the output of the slicing engine itself if some debug flag is set.

Parameters:postfix (str) – Postfix of the logfile for which to create the path. If set, the file name of the log file will be plugin_<plugin identifier>_<postfix>.log, if not it will be plugin_<plugin identifier>.log.
Returns:Absolute path to the log file, directly usable by the plugin.
Return type:str
globalGet(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_get()

globalGetBaseFolder(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_get_basefolder()

globalGetBoolean(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_get_boolean()

globalGetFloat(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_get_float()

globalGetInt(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_get_int()

globalSet(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_set()

globalSetBoolean(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_set_boolean()

globalSetFloat(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_set_float()

globalSetInt(*args, **kwargs)

Deprecated since version 1.2.0-dev-546: Replaced by global_set_int()

global_get(path, **kwargs)

Getter for retrieving settings not managed by the plugin itself from the core settings structure. Use this to access global settings outside of your plugin.

Directly forwards to octoprint.settings.Settings.get().

global_get_basefolder(folder_type, **kwargs)

Retrieves a globally defined basefolder of the given folder_type. Directly forwards to octoprint.settings.Settings.getBaseFolder().

global_get_boolean(path, **kwargs)

Like global_get() but directly orwards to octoprint.settings.Settings.getBoolean().

global_get_float(path, **kwargs)

Like global_get() but directly forwards to octoprint.settings.Settings.getFloat().

global_get_int(path, **kwargs)

Like global_get() but directly forwards to octoprint.settings.Settings.getInt().

global_set(path, value, **kwargs)

Setter for modifying settings not managed by the plugin itself on the core settings structure. Use this to modify global settings outside of your plugin.

Directly forwards to octoprint.settings.Settings.set().

global_set_boolean(path, value, **kwargs)

Like global_set() but directly forwards to octoprint.settings.Settings.setBoolean().

global_set_float(path, value, **kwargs)

Like global_set() but directly forwards to octoprint.settings.Settings.setFloat().

global_set_int(path, value, **kwargs)

Like global_set() but directly forwards to octoprint.settings.Settings.setInt().

octoprint.plugin.core

In this module resides the core data structures and logic of the plugin system. It is implemented in an OctoPrint-agnostic way and could be extracted into a separate Python module in the future.

class octoprint.plugin.core.PluginManager(plugin_folders, plugin_types, plugin_entry_points, logging_prefix=None, plugin_disabled_list=None, plugin_restart_needing_hooks=None, plugin_obsolete_hooks=None, plugin_validators=None)

The PluginManager is the central component for finding, loading and accessing plugins provided to the system.

It is able to discover plugins both through possible file system locations as well as customizable entry points.

get_filtered_implementations(f, *types)

Get all mixin implementation that implementat all of the provided types and match the provided filter f.

Parameters:
  • f (callable) – A filter function returning True for implementations to return and False for those to exclude.
  • types (one or more type) – The types a mixin implementation needs to implement in order to be returned.
Returns:

A list of all found and matching implementations.

Return type:

list

get_helpers(name, *helpers)

Retrieves the named helpers for the plugin with identifier name.

If the plugin is not available, returns None. Otherwise returns a dict with the requested plugin helper names mapped to the method - if a helper could not be resolved, it will be missing from the dict.

Parameters:
  • name (str) – Identifier of the plugin for which to look up the helpers.
  • helpers (one or more str) – Identifiers of the helpers of plugin name to return.
Returns:

A dictionary of all resolved helpers, mapped by their identifiers, or None if the plugin was not registered with the system.

Return type:

dict

get_hooks(hook)

Retrieves all registered handlers for the specified hook.

Parameters:hook (str) – The hook for which to retrieve the handlers.
Returns:A dict containing all registered handlers mapped by their plugin’s identifier.
Return type:dict
get_implementations(*types)

Get all mixin implementations that implement all of the provided types.

Parameters:types (one or more type) – The types a mixin implementation needs to implement in order to be returned.
Returns:A list of all found implementations
Return type:list
get_plugin(identifier, require_enabled=True)

Retrieves the module of the plugin identified by identifier. If the plugin is not registered or disabled and required_enabled is True (the default) None will be returned.

Parameters:
  • identifier (str) – The identifier of the plugin to retrieve.
  • require_enabled (boolean) – Whether to only return the plugin if is enabled (True, default) or also if it’s disabled.
Returns:

The requested plugin module or None

Return type:

module

get_plugin_info(identifier, require_enabled=True)

Retrieves the PluginInfo instance identified by identifier. If the plugin is not registered or disabled and required_enabled is True (the default) None will be returned.

Parameters:
  • identifier (str) – The identifier of the plugin to retrieve.
  • require_enabled (boolean) – Whether to only return the plugin if is enabled (True, default) or also if it’s disabled.
Returns:

The requested PluginInfo or None

Return type:

PluginInfo

register_message_receiver(client)

Registers a client for receiving plugin messages. The client needs to be a callable accepting two input arguments, plugin (the sending plugin’s identifier) and data (the message itself).

send_plugin_message(plugin, data)

Sends data in the name of plugin to all currently registered message receivers by invoking them with the two arguments.

Parameters:
  • plugin (str) – The sending plugin’s identifier.
  • data (object) – The message.
unregister_message_receiver(client)

Unregisters a client for receiving plugin messages.

class octoprint.plugin.core.PluginInfo(key, location, instance, name=None, version=None, description=None, author=None, url=None, license=None)

The PluginInfo class wraps all available information about a registered plugin.

This includes its meta data (like name, description, version, etc) as well as the actual plugin extensions like implementations, hooks and helpers.

It works on Python module objects and extracts the relevant data from those via accessing the control properties.

Parameters:
  • key (str) – Identifier of the plugin
  • location (str) – Installation folder of the plugin
  • instance (module) – Plugin module instance
  • name (str) – Human readable name of the plugin
  • version (str) – Version of the plugin
  • description (str) – Description of the plugin
  • author (str) – Author of the plugin
  • url (str) – URL of the website of the plugin
  • license (str) – License of the plugin
attr_author = '__plugin_author__'

Module attribute from which to retrieve the plugin’s author.

attr_check = '__plugin_check__'

Module attribute which to call to determine if the plugin can be loaded.

attr_description = '__plugin_description__'

Module attribute from which to retrieve the plugin’s description.

attr_disable = '__plugin_disable__'

Module attribute which to call when disabling the plugin.

attr_enable = '__plugin_enable__'

Module attribute which to call when enabling the plugin.

attr_helpers = '__plugin_helpers__'

Module attribute from which to retrieve the plugin’s provided helpers.

attr_hooks = '__plugin_hooks__'

Module attribute from which to retrieve the plugin’s provided hooks.

attr_implementation = '__plugin_implementation__'

Module attribute from which to retrieve the plugin’s provided mixin implementation.

attr_implementations = '__plugin_implementations__'

Module attribute from which to retrieve the plugin’s provided implementations.

This deprecated attribute will only be used if a plugin does not yet offer attr_implementation. Only the first entry will be evaluated.

Deprecated since version 1.2.0-dev-694: Use attr_implementation instead.

attr_init = '__plugin_init__'

Module attribute which to call when loading the plugin.

This deprecated attribute will only be used if a plugin does not yet offer attr_load.

Deprecated since version 1.2.0-dev-720: Use attr_load instead.

attr_license = '__plugin_license__'

Module attribute from which to retrieve the plugin’s license.

attr_load = '__plugin_load__'

Module attribute which to call when loading the plugin.

attr_name = '__plugin_name__'

Module attribute from which to retrieve the plugin’s human readable name.

attr_unload = '__plugin_unload__'

Module attribute which to call when unloading the plugin.

attr_url = '__plugin_url__'

Module attribute from which to retrieve the plugin’s website URL.

attr_version = '__plugin_version__'

Module attribute from which to retrieve the plugin’s version.

author

Author of the plugin. Will be taken from the author attribute of the plugin module as defined in attr_author if available, otherwise from the author supplied during construction. May be None.

Returns:Author of the plugin.
Return type:str or None
check

Method for pre-load check of plugin. Will be taken from the check attribute of the plugin module as defined in attr_check if available, otherwise a lambda always returning True is returned.

Returns:Check method for the plugin module which should return True if the plugin can be loaded, False otherwise.
Return type:callable
description

Description of the plugin. Will be taken from the description attribute of the plugin module as defined in attr_description if available, otherwise from the description supplied during construction. May be None.

Returns:Description of the plugin.
Return type:str or None
disable

Method for disabling the plugin module. Will be taken from the disable attribute of the plugin module as defined in attr_disable if available, otherwise a no-operation lambda will be returned.

Returns:Disable method for the plugin module.
Return type:callable
enable

Method for enabling the plugin module. Will be taken from the enable attribute of the plugin module as defined in attr_enable if available, otherwise a no-operation lambda will be returned.

Returns:Enable method for the plugin module.
Return type:callable
get_hook(hook)
Parameters:hook (str) – Hook to return.
Returns:Handler for the requested hook or None if no handler is registered.
Return type:callable or None
get_implementation(*types)
Parameters:types (list) – List of Plugin sub classes all returned implementations need to implement.
Returns:The plugin’s implementation if it matches all of the requested types, None otherwise.
Return type:object
helpers

Helpers provided by the plugin. Will be taken from the helpers attribute of the plugin module as defined in attr_helpers if available, otherwise an empty list is returned.

Returns:Helpers provided by the plugin.
Return type:dict
hooks

Hooks provided by the plugin. Will be taken from the hooks attribute of the plugin module as defiend in attr_hooks if available, otherwise an empty dictionary is returned.

Returns:Hooks provided by the plugin.
Return type:dict
implementation

Implementation provided by the plugin. Will be taken from the implementation attribute of the plugin module as defined in attr_implementation if available, otherwise None is returned.

Returns:Implementation provided by the plugin.
Return type:object
license

License of the plugin. Will be taken from the license attribute of the plugin module as defined in attr_license if available, otherwise from the license supplied during construction. May be None.

Returns:License of the plugin.
Return type:str or None
load

Method for loading the plugin module. Will be taken from the load attribute of the plugin module as defined in attr_load if available, otherwise a no-operation lambda will be returned.

Returns:Load method for the plugin module.
Return type:callable
long_str(show_bundled=False, bundled_strs=(' [B]', ''), show_location=False, location_str=' - {location}', show_enabled=False, enabled_strs=('* ', ' '))

Long string representation of the plugin’s information. Will return a string of the format <enabled><str(self)><bundled><location>.

enabled, bundled and location will only be displayed if the corresponding flags are set to True. The will be filled from enabled_str, bundled_str and location_str as follows:

enabled_str
a 2-tuple, the first entry being the string to insert when the plugin is enabled, the second entry the string to insert when it is not.
bundled_str
a 2-tuple, the first entry being the string to insert when the plugin is bundled, the second entry the string to insert when it is not.
location_str
a format string (to be parsed with str.format), the {location} placeholder will be replaced with the plugin’s installation folder on disk.
Parameters:
  • show_enabled (boolean) – whether to show the enabled part
  • enabled_strs (tuple) – the 2-tuple containing the two possible strings to use for displaying the enabled state
  • show_bundled (boolean) – whether to show the bundled part
  • bundled_strs (tuple) – the 2-tuple containing the two possible strings to use for displaying the bundled state
  • show_location (boolean) – whether to show the location part
  • location_str (str) – the format string to use for displaying the plugin’s installation location
Returns:

The long string representation of the plugin as described above

Return type:

str

name

Human readable name of the plugin. Will be taken from name attribute of the plugin module if available, otherwise from the name supplied during construction with a fallback to key.

Returns:Name of the plugin, fallback is the plugin’s identifier.
Return type:str
unload

Method for unloading the plugin module. Will be taken from the unload attribute of the plugin module as defined in attr_unload if available, otherwise a no-operation lambda will be returned.

Returns:Unload method for the plugin module.
Return type:callable
url

Website URL for the plugin. Will be taken from the url attribute of the plugin module as defined in attr_url if available, otherwise from the url supplied during construction. May be None.

Returns:Website URL for the plugin.
Return type:str or None
version

Version of the plugin. Will be taken from the version attribute of the plugin module as defined in attr_version if available, otherwise from the version supplied during construction. May be None.

Returns:Version of the plugin.
Return type:str or None
class octoprint.plugin.core.Plugin

The parent class of all plugin implementations.

_identifier

The identifier of the plugin. Injected by the plugin core system upon initialization of the implementation.

_plugin_name

The name of the plugin. Injected by the plugin core system upon initialization of the implementation.

_plugin_version

The version of the plugin. Injected by the plugin core system upon initialization of the implementation.

_basefolder

The base folder of the plugin. Injected by the plugin core system upon initialization of the implementation.

_logger

The logger instance to use, with the logging name set to the PluginManager.logging_prefix of the PluginManager concatenated with _identifier. Injected by the plugin core system upon initialization of the implementation.

initialize()

Called by the plugin core after performing all injections. Override this to initialize your implementation.

octoprint.plugin.types

This module bundles all of OctoPrint’s supported plugin implementation types as well as their common parent class, OctoPrintPlugin.

Please note that the plugin implementation types are documented in the section Available plugin mixins.

class octoprint.plugin.types.OctoPrintPlugin

Bases: octoprint.plugin.core.Plugin

The parent class of all OctoPrint plugin mixins.

_plugin_manager

The PluginManager instance. Injected by the plugin core system upon initialization of the implementation.

_printer_profile_manager

The PrinterProfileManager instance. Injected by the plugin core system upon initialization of the implementation.

_event_bus

The EventManager instance. Injected by the plugin core system upon initialization of the implementation.

_analysis_queue

The AnalysisQueue instance. Injected by the plugin core system upon initialization of the implementation.

_slicing_manager

The SlicingManager instance. Injected by the plugin core system upon initialization of the implementation.

_file_manager

The FileManager instance. Injected by the plugin core system upon initialization of the implementation.

_printer

The PrinterInterface instance. Injected by the plugin core system upon initialization of the implementation.

_app_session_manager

The SessionManager instance. Injected by the plugin core system upon initialization of the implementation.

_plugin_lifecycle_manager

The LifecycleManager instance. Injected by the plugin core system upon initialization of the implementation.

_data_folder

Path to the data folder for the plugin to use for any data it might have to persist. Should always be accessed through get_plugin_data_folder() since that function will also ensure that the data folder actually exists and if not creating it before returning it. Injected by the plugin core system upon initialization of the implementation.

get_plugin_data_folder()

Retrieves the path to a data folder specifically for the plugin, ensuring it exists and if not creating it before returning it.

Plugins may use this folder for storing additional data they need for their operation.