-
Notifications
You must be signed in to change notification settings - Fork 8
Home
nacx edited this page Jan 24, 2012
·
23 revisions
All kahuna features are developed as a plugin, to make it easy to extend. Adding a new plugin is as simple as creating the plugin script and place it in the plugins folder.
An example plugin can be found in kahuna/examples/skel.py and can be copied directly into the kahuna/plugins directory to see how it behaves. It can be then edited to make it perform the desired operations. The following code snipped shows the contents of the skel.py example plugin:
#!/usr/bin/env jython
# This is an example plugin that can be used as a
# skeleton for new plugins.
# To test it just rename it as desired, place it in the
# kahuna/plugins folder and run kahuna. You will see the
# new plugin in the help.
class SkeletonPlugin:
""" An example plugin that prints dummy messages. """
def __init__(self):
pass
def commands(self):
""" Returns the commands provided by the plugin, mapped to the handler methods. """
commands = {}
# Bind the 'print' plugin command to the 'print_handler' method
commands['print'] = self.print_handler
return commands
# The documentation string in command methods will be used to
# print the help of the command.
def print_handler(self, args):
""" Prints a dummy message. """
print "This is the print_handler in the example plugin"
# Each plugin must provide a load method at module level that will be
# used to instantiate the plugin
def load():
""" Loads the current plugin. """
return SkeletonPlugin()
As you can see, it is quite easy to create a plugin. There are currently two requirements:
- The plugin script must have a method called load which takes no arguments and returns a plugin instance:
def load():
""" Loads the current plugin. """
return SkeletonPlugin()
- The plugin object must have a commands method which takes no arguments and returns a dictionary with all the available commands for the plugin in the keys and the bound function in the values:
def commands(self):
""" Returns the commands provided by the plugin, mapped to the handler methods. """
commands = {}
# Bind the 'print' plugin command to the 'print_handler' method
commands['print'] = self.print_handler
return commands
Once the plugin script has been written, it just needs to be placed in the kahuna/plugins folder. having done this, invoking kahuna without parameters should show the plugin help in the output:
$ kahuna
Usage: kahuna <plugin> <command> [<options>]
The following plugins are available:
(...)
An example plugin that prints dummy messages.
skel print Prints a dummy message.
(...)
That's it!