-
Notifications
You must be signed in to change notification settings - Fork 2
Plugin
Plugins are the driving force behind SuperBot2. They provide all of the end functionality and are the main reason for SuperBot2 to exist.
Plugins in SuperBot2 are significantly different from SuperBots plugins.
All plugin should be located in the Plugins directory of a Provider. A plugin "Foo" should be named Foo.py, and contain a class 'Foo'. The classes constructor should NOT take any arguments. Plugins make use of provided decorators to let SuperBot2 know about requirements for the plugin, and what to test Events for to see if a function should be called.
Example MyPlugin.py
from Hook import bindFunction
class MyPlugin:
@bindFunction(method="GET")
def func1(self):
"""This function only gets called if method matches "GET" """
print "GET event fired"
@bindFunction(method="GET")
@bindFunction(method="POST")
def func2(self):
"""This function will get called if method matches "GET" OR "POST", separate binds are OR'd """
print "GET or POST event fired"
@bindFunction(path="view/(?P<num>\\d+)/")
def handleView(self,response,num):
"""This function matches events where path is like "view/1234/", and puts the number into 'num'
This also shows using 'return' to tell the connector what to respond with."""
print "View",num,"requested."
return response.NotFound() # See [[ResponseObject]] for more details about this
@bindFunction(method="GET",path="serve/(?P<fname>.+)"):
def serveContent(self,response,fname):
"""Each argument in a bind is AND'd together, this only serves GET requests to "serve/filename"
This function also shows the use of 'yield' to return more than one response from a single event"""
fhandle = file(fname)
for line in fhandle:
yield response.writeLine(line)
Plugins can also require or prefer services:
from Hook import *
@requires("ServiceName")
@prefers("OtherService")
class MyPlugin:
pass
If a Service is required and absent, the PluginManager will attempt to load it. If it fails the plugin will not be loaded. However, if a plugin prefers a service, and the service is absent and can not be loaded, the plugin will still be loaded.
- Look into allowing the classes constructor to take arguments
- Read from a config file?
- Add in the ability to pass data other than strings for testing
- integers
- functions
- possibly look into getting fields of the events object such as .toLower()=="test"
- I believe django does this?