Plugin Manager

This module contains the primary plugin manager framework which is used throughout zootools from toolsets, hive, MayaCommand, artist palette, the list is rather extensive.

class PluginManager(interface=None, variableName=None, name=None)

Bases: object

This class manages a group of plugin instance’s in local scope therefore allowing the client to create as many plugin manager instances with varying interfaces as needed. It’s your responsible to manage the lifetime of the manager.

Plugins can be treated in two ways for a class instance.

  1. Client managed lifetime, The plugin manager simply filters plugins without instantiation. Therefore you handle the lifetime of the plugin instance, but we store the callable.

  2. Plugin singletons but scoped locally to the manager instance. Allows plugin classes to be instantiated once and reused.

To load a singleton of a plugin use loadPlugin(), after this call any calls to getPlugin() will result in the same instance being returned. getPlugin(). Default behaviour of getPlugin() gets the loaded plugin instance and if that’s not found returns the callable class.

For registering plugins.

  1. To register a class as a plugin use registerPlugin().

  2. To automatically discover plugin classes from a module object use registerByModule().

  3. To register any class which meets the interface filter by module or folder path use registerPaths().

  4. To register all valid paths from an environment variable use registerByEnv()

from zoo.core.plugin import pluginmanager, plugin

class MyPlugin(plugin.Plugin):
    uid = "myPlugin"
    def __init__(self):
        pass

# Interface here just tells the manager when registering plugins ensure it's a subclass
# of this object. You can use your own base class which doesn't inherent from Plugin if
# you like. variableName is the class variable to use for the key in the cache.
# Defaults to use the className if not provided.
manager = pluginmanager.PluginManager(interface=[plugin.Plugin], variableName="uid")
manager.registerPlugin(MyPlugin)

# Since we're using the base class from the example it's going to register the class Name
# it's best to use the :param:`variableName` argument.
pluginObject = manager.getPlugin("myPlugin")
pluginInstance = pluginObject() # lifetime is handle by us

# loads the plugin class ie. calls __init__
myPluginSingleton = manager.loadPlugin("myPlugin")

# getPlugin will now always return the same instance.
manager.getPlugin("myPlugin") == myPluginSingleton
# result: True
Parameters:

interface – A list of base class which will become the plugin interface for all plugins for the manager, this is used solely to filter classes out.

:type interface:list[callable] :param variableName: The class variable name to become the UUID of the class within the cache if none is specified the UUID will be the class name.

registerByEnv(env)

Recursively registers all found plugins based on the paths specified in the environment variable. Each must be separated by os.pathsep

Parameters:

env (str) – The environment variable key

registerPaths(paths)

This function is a helper function to register a list of paths.

Parameters:

paths (list(str)) – A list of module or package paths, see registerByModule() and registerByPackage() for the path format.

registerPath(modulePath)

Registers the given python module path.

Parameters:

modulePath (str) – The absolute full path to a python module

Returns:

Module Object

registerByModule(module)

This function registry a module by search all class members of the module and registers any class that is an instance of the plugin class ‘zoo.core.plugin.plugin.Plugin’

Parameters:

module (str) – the module path to registry, the path is a ‘.’ separated path eg. zoo.libs.apps.tooldefinitions

registerByPackage(pkg)

This function is similar to registerByModule() but works on packages, this is an expensive operation as it requires a recursive search by importing all submodules and searching them.

Parameters:

pkg (str) – The package path to register eg. zoo.libs.apps works with absolute paths as well.

registerPlugin(classObj)

Registers a plugin instance to the manager.

Parameters:

classObj (callable) – The plugin instance to registry.

loadPlugin(pluginName, **kwargs)

Loads a given plugin by name. eg plugin(manager=self)

Parameters:

pluginName (str) – The plugin to load by name.

loadAllPlugins(**pluginArguments)

Loops over all registered plugins and calls them eg. plugin(manager=self)

getPlugin(name)

Returns the plugin instance by name, first checks to see if it’s already loaded and return the instance otherwise returns the plugin callable or None

Parameters:

name (str) – the name of the plugin

Returns:

Returns the plugin isinstance or None

Return type:

Plugin instance or None

unload(name)

Unloads a plugin by name from the manager by simply remove it from the cache.

Parameters:

name (str) – The name of the registered plugin class.

Returns:

Return True if the plugin was successfully unloaded.

Return type:

bool

unloadAllPlugins()

Unloads all currently loaded plugins from the registry.

Plugin

This module contains the base Plugin class and the metric stats for the plugin.

class Plugin(manager=None)

Bases: object

Base plugin class that all plugins inherent from. The client should subclass this to provide a standard interface when needed.

class CustomPlugin(Plugin):
    id = "CustomPlugin.example"
    def execute(self):
        print "executed plugin: {}".format(self.id)
Parameters:

manager (zoo.core.plugin.pluginmanager.PluginManager or None) – The Plugin manager instance

id = ''
creator = ''
class PluginStats(plugin)

Bases: object

Parameters:

plugin (Plugin) – The Plugin instance

start()

Sets the start time for metrics.

finish(traceback=None)

Called when the plugin has finish executing.

Parameters:

traceback

Environment registry

class EnvRegistry

Bases: object

Registry class to register paths based on an environment variable. Should be overridden.

Usage: Override

registryEnv = ''
interface = None
variableName = None
discover()

Searches for the classes in the environment variable in the registryEnv paths

initPlugins(classes)

To be overridden

Parameters:

classes

Returns: