1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-09-17 20:12:22 +00:00

Merge branch 'master' 0.8.7 into patch-6

This commit is contained in:
Ian Wood
2017-05-05 15:02:15 +01:00
16 changed files with 304 additions and 58 deletions

View File

@@ -22,15 +22,16 @@ the server.
from flask import Flask
from senpy.extensions import Senpy
from gevent.wsgi import WSGIServer
from gevent.monkey import patch_all
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
import logging
import os
import argparse
import senpy
patch_all(thread=False)
SERVER_PORT = os.environ.get("PORT", 5000)
@@ -92,9 +93,10 @@ def main():
print('Server running on port %s:%d. Ctrl+C to quit' % (args.host,
args.port))
if not app.debug:
http_server = WSGIServer((args.host, args.port), app)
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(args.port, address=args.host)
try:
http_server.serve_forever()
IOLoop.instance().start()
except KeyboardInterrupt:
print('Bye!')
http_server.stop()

View File

@@ -1,6 +1,7 @@
import requests
import logging
from . import models
from .plugins import default_plugin_type
logger = logging.getLogger(__name__)
@@ -12,6 +13,10 @@ class Client(object):
def analyse(self, input, method='GET', **kwargs):
return self.request('/', method=method, input=input, **kwargs)
def plugins(self, ptype=default_plugin_type):
resp = self.request(path='/plugins', plugin_type=ptype).plugins
return {p.name: p for p in resp}
def request(self, path=None, method='GET', **params):
url = '{}{}'.format(self.endpoint, path)
response = requests.request(method=method, url=url, params=params)

View File

@@ -183,7 +183,7 @@ class Senpy(object):
return resp
def _conversion_candidates(self, fromModel, toModel):
candidates = self.filter_plugins(**{'@type': 'emotionConversionPlugin'})
candidates = self.filter_plugins(plugin_type='emotionConversionPlugin')
for name, candidate in candidates.items():
for pair in candidate.onyx__doesConversion:
logging.debug(pair)
@@ -303,6 +303,7 @@ class Senpy(object):
else:
th = Thread(target=act)
th.start()
return th
def deactivate_plugin(self, plugin_name, sync=False):
try:
@@ -327,6 +328,7 @@ class Senpy(object):
else:
th = Thread(target=deact)
th.start()
return th
@classmethod
def validate_info(cls, info):
@@ -417,33 +419,7 @@ class Senpy(object):
return self._plugin_list
def filter_plugins(self, **kwargs):
""" Filter plugins by different criteria """
ptype = kwargs.pop('plugin_type', None)
logger.debug('#' * 100)
logger.debug('ptype {}'.format(ptype))
if ptype:
try:
ptype = ptype[0].upper() + ptype[1:]
pclass = getattr(plugins, ptype)
logger.debug('Class: {}'.format(pclass))
candidates = filter(lambda x: isinstance(x, pclass),
self.plugins.values())
except AttributeError:
raise Error('{} is not a valid type'.format(ptype))
else:
candidates = self.plugins.values()
logger.debug(candidates)
def matches(plug):
res = all(getattr(plug, k, None) == v for (k, v) in kwargs.items())
logger.debug(
"matching {} with {}: {}".format(plug.name, kwargs, res))
return res
if kwargs:
candidates = filter(matches, candidates)
return {p.name: p for p in candidates}
return plugins.pfilter(self.plugins, **kwargs)
@property
def analysis_plugins(self):

View File

@@ -9,6 +9,7 @@ import logging
import tempfile
import copy
from .. import models
from ..api import API_PARAMS
logger = logging.getLogger(__name__)
@@ -117,3 +118,40 @@ class ShelfMixin(object):
if hasattr(self, '_sh') and self._sh is not None:
with open(self.shelf_file, 'wb') as f:
pickle.dump(self._sh, f)
default_plugin_type = API_PARAMS['plugin_type']['default']
def pfilter(plugins, **kwargs):
""" Filter plugins by different criteria """
if isinstance(plugins, models.Plugins):
plugins = plugins.plugins
elif isinstance(plugins, dict):
plugins = plugins.values()
ptype = kwargs.pop('plugin_type', default_plugin_type)
logger.debug('#' * 100)
logger.debug('ptype {}'.format(ptype))
if ptype:
try:
ptype = ptype[0].upper() + ptype[1:]
pclass = globals()[ptype]
logger.debug('Class: {}'.format(pclass))
candidates = filter(lambda x: isinstance(x, pclass),
plugins)
except KeyError:
raise models.Error('{} is not a valid type'.format(ptype))
else:
candidates = plugins
logger.debug(candidates)
def matches(plug):
res = all(getattr(plug, k, None) == v for (k, v) in kwargs.items())
logger.debug(
"matching {} with {}: {}".format(plug.name, kwargs, res))
return res
if kwargs:
candidates = filter(matches, candidates)
return {p.name: p for p in candidates}

View File

@@ -37,12 +37,12 @@
"@type": "@id",
"@container": "@set"
},
"plugins": {
"@container": "@list"
},
"options": {
"@container": "@set"
},
"plugins": {
"@container": "@set"
},
"prov:wasGeneratedBy": {
"@type": "@id"
},

View File

@@ -10,8 +10,6 @@
"items": {
"$ref": "plugin.json"
}
},
"@type": {
}
}
}

View File

@@ -18,10 +18,16 @@
"type": "string"
},
"analysis": {
"type": "array",
"default": [],
"type": "array",
"items": {
"$ref": "analysis.json"
"anyOf": [
{
"$ref": "analysis.json"
},{
"type": "string"
}
]
}
},
"entries": {