Update documentation for plugins

This commit is contained in:
Will Kahn-Greene 2012-05-16 21:04:52 -04:00
parent 29b6f91740
commit 355fd6770d
2 changed files with 85 additions and 24 deletions

View File

@ -2,8 +2,8 @@
Plugins Plugins
========= =========
GNU MediaGoblin supports plugins that, when installed, allow you to GNU MediaGoblin supports plugins that allow you to augment MediaGoblin's
augment MediaGoblin's behavior. behavior.
This chapter covers discovering, installing, configuring and removing This chapter covers discovering, installing, configuring and removing
plugins. plugins.
@ -18,19 +18,28 @@ come with MediaGoblin, you don't have to install them, but you do have
to add them to your config file if you're interested in using them. to add them to your config file if you're interested in using them.
You can also write your own plugins and additionally find plugins You can also write your own plugins and additionally find plugins
elsewhere on the Internet. Since these plugins don't come with elsewhere on the Internet. Once you find a plugin you like, you need
MediaGoblin, you must first install them, then add them to your to first install it, then add it to your configuration.
configuration.
.. todo:: how do you find plugins on the internet?
Installing plugins Installing plugins
================== ==================
MediaGoblin core plugins don't need to be installed. For core plugins, Core plugins
you can skip installation! ------------
If the plugin is not a core plugin and is packaged and available on MediaGoblin core plugins don't need to be installed because they come
the Python Package Index, then you can install the plugin with pip:: with MediaGoblin. Further, when you upgrade MediaGoblin, you will also
get updates to the core plugins.
Other plugins
-------------
If the plugin is available on the `Python Package Index
<http://pypi.python.org/pypi>`_, then you can install the plugin with pip::
pip install <plugin-name> pip install <plugin-name>
@ -43,7 +52,8 @@ For example, if we wanted to install the plugin named
If you're using a virtual environment, make sure to activate the If you're using a virtual environment, make sure to activate the
virtual environment before installing with pip. Otherwise the virtual environment before installing with pip. Otherwise the
plugin may get installed in a different environment. plugin may get installed in a different environment than the one
MediaGoblin is installed in.
Once you've installed the plugin software, you need to tell Once you've installed the plugin software, you need to tell
MediaGoblin that this is a plugin you want MediaGoblin to use. To do MediaGoblin that this is a plugin you want MediaGoblin to use. To do
@ -62,8 +72,9 @@ the ``plugins`` section as a subsection::
Configuring plugins Configuring plugins
=================== ===================
Generally, configuration goes in the ``.ini`` file. Configuration for Configuration for a plugin goes in the subsection for that plugin. Core
a specific plugin, goes in a subsection of the ``plugins`` section. plugins are documented in the administration guide. Other plugins
should come with documentation that tells you how to configure them.
Example 1: Core MediaGoblin plugin Example 1: Core MediaGoblin plugin
@ -75,6 +86,7 @@ to your ``.ini`` file like this::
[[mediagoblin.plugins.flatpages]] [[mediagoblin.plugins.flatpages]]
# configuration for flatpages plugin here! # configuration for flatpages plugin here!
directory = /srv/mediagoblin/flatpages
Example 2: Plugin that is not a core MediaGoblin plugin Example 2: Plugin that is not a core MediaGoblin plugin
@ -103,3 +115,23 @@ To remove a plugin, use ``pip uninstall``. For example::
If you're using a virtual environment, make sure to activate the If you're using a virtual environment, make sure to activate the
virtual environment before uninstalling with pip. Otherwise the virtual environment before uninstalling with pip. Otherwise the
plugin may get installed in a different environment. plugin may get installed in a different environment.
Upgrading plugins
=================
Core plugins
------------
Core plugins get upgraded automatically when you upgrade MediaGoblin
because they come with MediaGoblin.
Other plugins
-------------
For plugins that you install with pip, you can upgrade them with pip::
pip install -U <plugin-name>
The ``-U`` tells pip to upgrade the package.

View File

@ -29,26 +29,36 @@ Two things about things in this module:
How do plugins work? How do plugins work?
==================== ====================
You create a Python package. In that package, you define a high-level Plugins are structured like any Python project. You create a Python package.
``__init__.py`` that either defines or imports modules that define In that package, you define a high-level ``__init__.py`` that either defines
classes that inherit from the ``Plugin`` class. or imports modules that define classes that inherit from the ``Plugin`` class.
Additionally, you want a LICENSE file that specifies the license and a
``setup.py`` that specifies the metadata for packaging your plugin. A rough
file structure could look like this::
myplugin/
|- setup.py # plugin project packaging metadata
|- README # holds plugin project information
|- LICENSE # holds license information
|- myplugin/ # plugin package directory
|- __init__.py # imports myplugin.main
|- main.py # code for plugin
Lifecycle Lifecycle
========= =========
1. All the modules listed as subsections of the ``plugins`` section in 1. All the modules listed as subsections of the ``plugins`` section in
the config file are imported and any ``Plugin`` subclasses are the config file are imported. This causes any ``Plugin`` subclasses in
loaded causing it to be registered with the ``PluginCache``. those modules to be defined and when the classes are defined they get
automatically registered with the ``PluginCache``.
2. After all plugin modules are imported, registered plugins are 2. After all plugin modules are imported, registered plugin classes are
instantiated and ``setup_plugin`` is called with the configuration. instantiated and ``setup_plugin`` is called for each plugin object.
Plugins can do any setup they need to do in their ``setup_plugin``
How to build a plugin method.
=====================
See the documentation on building plugins.
""" """
import logging import logging
@ -98,6 +108,20 @@ class MetaPluginClass(type):
class Plugin(object): class Plugin(object):
"""Exttend this class for plugins.
Example::
from mediagoblin.tools.pluginapi import Plugin
class MyPlugin(Plugin):
...
def setup_plugin(self):
....
"""
__metaclass__ = MetaPluginClass __metaclass__ = MetaPluginClass
def setup_plugin(self): def setup_plugin(self):
@ -111,6 +135,11 @@ def get_config(key):
>>> get_config('mediagoblin.plugins.sampleplugin') >>> get_config('mediagoblin.plugins.sampleplugin')
{'foo': 'bar'} {'foo': 'bar'}
>>> get_config('myplugin')
{}
>>> get_config('flatpages')
{'directory': '/srv/mediagoblin/pages', 'nesting': 1}}
""" """
global_config = mg_globals.global_config global_config = mg_globals.global_config