UsageΒΆ

plugz will let you define custom types of plugins. You will then derive from your new type to create plugin classes and provide plugz with the location of these new plugins. plugz can then provide you with a loading mechanism for these plugin classes.

The main idea is to derive your custom plugin type from plugz.PluginTypeBase and declare all the methods that plugins will need to implement as abstract. Look at how a type that plugz ships with is implemented:

from abc import abstractmethod

from plugz import PluginTypeBase

class StandardPluginType(PluginTypeBase):
    """ Simple plugin type that requires an activate() method.

    Plugins that derive from this type will have to implement
    activate() or plugz will refuse to load the plugin.

    The plugintype needs to be set as well. Set it to a string
    that describes the class. It does not have to be the class
    name but it might make things easier for you.

    """

    plugintype = __name__

    @abstractmethod
    def activate(self): pass

Just include the implementation of your custom type in your application and pass it on to plugz when necessary. How to do that is described later when we show the load_plugins() function.

The according plugins can look like the following. Note the usage of plugz.register_plugin.

import plugz

@plugz.register_plugin
class MyFirstValidPlugin(plugz.StandardPluginType):
    """ Basic plugin to show as an example.

    Inherit from StandardPluginType and implement activate(). If
    activate is not implemented here, plugz would refuse to load the
    plugin.

    """

    def __init__(self, ):
        """ Initialize the plugin.

        You can copy files, request database entries or do other preprocessing
        here. Usually, the host will only hold one instance of this class
        but it could equally as well create multiple, if it needs to for some reason.

        """
        pass

    def activate(self):
        """ Activate the plugin.

        You can require a licence here or do other things that are necessary
        to get this plugin up and running.

        """
        print __name__, 'has just been activated!'

Given that you defined a plugin type like described above, as well as plugins that derive correctly from it, the following simple instructions will return a list of all registered, valid plugin classes:

>>> import plugz
>>> import my_host_app.MyNewPluginType
>>> plugins = plugz.load_plugins(['/path/to/my/plugins/'], my_host_app.MyNewPluginType)

Note

plugz will return a list of class objects and not a list of instances of these objects! You will need to initialize your plugins yourself if you need an actual instance.

Previous topic

Installation

Next topic

Contributing

This Page