Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
add initial plug-in support to the rest api, allowing registration an…
Browse files Browse the repository at this point in the history
…d querying of plugins
  • Loading branch information
jeffbryner committed Jan 28, 2015
1 parent e72f359 commit dfdb7a0
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions rest/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import json
import MySQLdb
import netaddr
import os
import pyes
import pytz
import pynsive
import requests
import sys
from bottle import debug, route, run, response, request, default_app, post
Expand All @@ -30,7 +32,7 @@
options = None
dbcursor = None
mysqlconn = None

pluginList = list() # tuple of module,registration dict,priority

# cors decorator for rest/ajax
def enable_cors(fn):
Expand Down Expand Up @@ -184,6 +186,60 @@ def index():
return


@route('/plugins', methods=['GET'])
@route('/plugins/', methods=['GET'])
@route('/plugins/<endpoint>', methods=['GET'])
def getPluginList(endpoint=None):
# return a json representation of the plugin tuple
# (mname, mclass, mreg, mpriority)
# minus the actual class
# which isn't json-able
pluginResponse = list()
if endpoint is None:
for plugin in pluginList:
pdict = {}
pdict['title'] = plugin[0]
pdict['registration'] = plugin[2]
pdict['priority'] = plugin[3]
pluginResponse.append(pdict)
else:
# filter the list to just the endpoint requested
for plugin in pluginList:
if endpoint in plugin[2]:
pdict = {}
pdict['title'] = plugin[0]
pdict['registration'] = plugin[2]
pdict['priority'] = plugin[3]
pluginResponse.append(pdict)
return json.dumps(pluginResponse)


def registerPlugins():
'''walk the ./plugins directory
and register modules in pluginList
as a tuple: name,class,registration,priority
'''

plugin_manager = pynsive.PluginManager()
if os.path.exists('plugins'):
modules = pynsive.list_modules('plugins')
for mname in modules:
module = pynsive.import_module(mname)
reload(module)
if not module:
raise ImportError('Unable to load module {}'.format(mname))
else:
if 'message' in dir(module):
mclass = module.message()
mreg = mclass.registration
if 'priority' in dir(mclass):
mpriority = mclass.priority
else:
mpriority = 100
if isinstance(mreg, list):
print('[*] plugin {0} registered to receive messages from /{1}'.format(mname, mreg))
pluginList.append((mname, mclass, mreg, mpriority))

def toUTC(suspectedDate, localTimeZone="US/Pacific"):
'''make a UTC date out of almost anything'''
utc = pytz.UTC
Expand Down Expand Up @@ -481,7 +537,8 @@ def initConfig():
help="configuration file to use")
(options, args) = parser.parse_args()
initConfig()

registerPlugins()

run(host="localhost", port=8081)
else:
parser = OptionParser()
Expand All @@ -490,5 +547,6 @@ def initConfig():
help="configuration file to use")
(options, args) = parser.parse_args()
initConfig()

registerPlugins()

application = default_app()

0 comments on commit dfdb7a0

Please sign in to comment.