1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-25 01:22:28 +00:00

Switched to Yapsy

This commit is contained in:
J. Fernando Sánchez 2014-11-26 22:09:02 +01:00
parent 2f7a8d7267
commit dabf444607
11 changed files with 129 additions and 43 deletions

2
app.py
View File

@ -29,5 +29,7 @@ sp = Senpy()
sp.init_app(app) sp.init_app(app)
if __name__ == '__main__': if __name__ == '__main__':
import logging
logging.basicConfig(level=config.DEBUG)
app.debug = config.DEBUG app.debug = config.DEBUG
app.run(host="0.0.0.0", use_reloader=False) app.run(host="0.0.0.0", use_reloader=False)

View File

@ -0,0 +1,45 @@
import requests
import json
from senpy.plugins import SentimentPlugin
from senpy.models import Response, Opinion, Entry
class Sentiment140Plugin(SentimentPlugin):
EXTRA_PARAMS = {
"language": {"aliases": ["language", "l"],
"required": False,
"options": ["es", "en", "auto"],
}
}
def __init__(self, **kwargs):
super(Sentiment140Plugin, self).__init__(name="sentiment140",
version="2.0",
extraparams=self.EXTRA_PARAMS,
**kwargs)
def analyse(self, **params):
lang = params.get("language", "auto")
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
json.dumps({"language": lang,
"data": [{"text": params["input"]}]
}
)
)
response = Response()
polarity_value = int(res.json()["data"][0]["polarity"]) * 25
polarity = "marl:Neutral"
if polarity_value > 50:
polarity = "marl:Positive"
elif polarity_value < 50:
polarity = "marl:Negative"
entry = Entry(text=params["input"])
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)
return response
plugin = Sentiment140Plugin()

View File

@ -0,0 +1,8 @@
[Core]
Name = Test plugin of Yapsy
Module = prueba
[Documentation]
Description = What my plugin broadly does
Author = My very own name
Version = 0.1
Website = My very own website

View File

@ -2,3 +2,4 @@ Flask==0.10.1
gunicorn==19.0.0 gunicorn==19.0.0
requests==2.4.1 requests==2.4.1
GitPython==0.3.2.RC1 GitPython==0.3.2.RC1
Yapsy>=1.10.423

View File

@ -120,10 +120,10 @@ def plugins(plugin=None, action="list"):
dic = {plug: plugs[plug].jsonable(with_params) for plug in plugs} dic = {plug: plugs[plug].jsonable(with_params) for plug in plugs}
return jsonify(dic) return jsonify(dic)
if action == "disable": if action == "disable":
current_app.senpy.disable_plugin(plugin) current_app.senpy.deactivate_plugin(plugin)
return "Ok" return "Ok"
elif action == "enable": elif action == "enable":
current_app.senpy.enable_plugin(plugin) current_app.senpy.activate_plugin(plugin)
return "Ok" return "Ok"
elif action == "reload": elif action == "reload":
current_app.senpy.reload_plugin(plugin) current_app.senpy.reload_plugin(plugin)

View File

@ -8,6 +8,8 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from .plugins import SentimentPlugin, EmotionPlugin from .plugins import SentimentPlugin, EmotionPlugin
from yapsy.PluginFileLocator import PluginFileLocator, PluginFileAnalyzerWithInfoFile
from yapsy.PluginManager import PluginManager
try: try:
from flask import _app_ctx_stack as stack from flask import _app_ctx_stack as stack
@ -50,11 +52,13 @@ class Senpy(object):
app.register_blueprint(nif_blueprint) app.register_blueprint(nif_blueprint)
def add_folder(self, folder): def add_folder(self, folder):
logger.debug("Adding folder: %s", folder)
if os.path.isdir(folder): if os.path.isdir(folder):
self._search_folders.add(folder) self._search_folders.add(folder)
self._outdated = True self._outdated = True
return True return True
else: else:
logger.debug("Not a folder: %s", folder)
return False return False
def analyse(self, **params): def analyse(self, **params):
@ -64,18 +68,25 @@ class Senpy(object):
algo = params["algorithm"] algo = params["algorithm"]
elif self.plugins: elif self.plugins:
algo = self.default_plugin algo = self.default_plugin
if algo in self.plugins and self.plugins[algo].enabled: if algo in self.plugins:
if self.plugins[algo].is_activated:
plug = self.plugins[algo] plug = self.plugins[algo]
resp = plug.analyse(**params) resp = plug.analyse(**params)
resp.analysis.append(plug.jsonable()) resp.analysis.append(plug.jsonable())
return resp return resp
logger.debug("Plugin not activated: {}".format(algo))
else: else:
logger.debug("The algorithm '{}' is not valid\nValid algorithms: {}".format(algo, self.plugins.keys()))
return {"status": 400, "message": "The algorithm '{}' is not valid".format(algo)} return {"status": 400, "message": "The algorithm '{}' is not valid".format(algo)}
def activate_all(self):
for plug in self.plugins.values():
plug.activate()
@property @property
def default_plugin(self): def default_plugin(self):
candidates = self.filter_plugins(enabled=True) candidates = self.filter_plugins(is_activated=True)
if len(candidates) > 1: if len(candidates) > 0:
candidate = candidates.keys()[0] candidate = candidates.keys()[0]
logger.debug("Default: {}".format(candidate)) logger.debug("Default: {}".format(candidate))
return candidate return candidate
@ -85,11 +96,11 @@ class Senpy(object):
def parameters(self, algo): def parameters(self, algo):
return getattr(self.plugins.get(algo or self.default_plugin), "params", {}) return getattr(self.plugins.get(algo or self.default_plugin), "params", {})
def enable_plugin(self, plugin): def activate_plugin(self, plugin):
self.plugins[plugin].enable() self.plugins[plugin].activate()
def disable_plugin(self, plugin): def deactivate_plugin(self, plugin):
self.plugins[plugin].disable() self.plugins[plugin].deactivate()
def reload_plugin(self, plugin): def reload_plugin(self, plugin):
logger.debug("Reloading {}".format(plugin)) logger.debug("Reloading {}".format(plugin))
@ -99,7 +110,7 @@ class Senpy(object):
self.plugins[nplug.name] = nplug self.plugins[nplug.name] = nplug
@staticmethod @staticmethod
def _load_plugin(plugin, search_folder, enabled=True): def _load_plugin(plugin, search_folder, is_activated=True):
logger.debug("Loading plugins") logger.debug("Loading plugins")
sys.path.append(search_folder) sys.path.append(search_folder)
(fp, pathname, desc) = imp.find_module(plugin) (fp, pathname, desc) = imp.find_module(plugin)
@ -112,8 +123,8 @@ class Senpy(object):
tmp.repo = Repo(repo_path) tmp.repo = Repo(repo_path)
except InvalidGitRepositoryError: except InvalidGitRepositoryError:
tmp.repo = None tmp.repo = None
if not hasattr(tmp, "enabled"): if not hasattr(tmp, "is_activated"):
tmp.enabled = enabled tmp.is_activated = is_activated
tmp.module = plugin tmp.module = plugin
except Exception as ex: except Exception as ex:
tmp = None tmp = None
@ -140,7 +151,20 @@ class Senpy(object):
def enable_all(self): def enable_all(self):
for plugin in self.plugins: for plugin in self.plugins:
self.enable_plugin(plugin) self.activate_plugin(plugin)
@property
def manager(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'senpy_manager'):
logger.debug("Loading manager: %s", self._search_folders)
ctx.senpy_manager = PluginManager(plugin_info_ext="senpy")
ctx.senpy_manager.getPluginLocator().setPluginPlaces(self._search_folders)
ctx.senpy_manager.locatePlugins()
ctx.senpy_manager.loadPlugins()
self.activate_all()
return ctx.senpy_manager
@property @property
def plugins(self): def plugins(self):
@ -148,7 +172,7 @@ class Senpy(object):
ctx = stack.top ctx = stack.top
if ctx is not None: if ctx is not None:
if not hasattr(ctx, 'senpy_plugins') or self._outdated: if not hasattr(ctx, 'senpy_plugins') or self._outdated:
ctx.senpy_plugins = self._load_plugins() ctx.senpy_plugins = {p.name:p.plugin_object for p in self.manager.getAllPlugins()}
return ctx.senpy_plugins return ctx.senpy_plugins
def filter_plugins(self, **kwargs): def filter_plugins(self, **kwargs):

View File

@ -1,4 +1,5 @@
import logging import logging
from yapsy.IPlugin import IPlugin
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -33,7 +34,7 @@ PARAMS = {"input": {"aliases": ["i", "input"],
} }
class SenpyPlugin(object): class SenpyPlugin(IPlugin):
def __init__(self, name=None, version=None, extraparams=None, params=None): def __init__(self, name=None, version=None, extraparams=None, params=None):
logger.debug("Initialising {}".format(name)) logger.debug("Initialising {}".format(name))
self.name = name self.name = name
@ -45,21 +46,16 @@ class SenpyPlugin(object):
if extraparams: if extraparams:
self.params.update(extraparams) self.params.update(extraparams)
self.extraparams = extraparams or {} self.extraparams = extraparams or {}
self.enabled = True self.is_activated = True
def analyse(self, *args, **kwargs): def analyse(self, *args, **kwargs):
logger.debug("Analysing with: {} {}".format(self.name, self.version))
pass pass
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def jsonable(self, parameters=False): def jsonable(self, parameters=False):
resp = { resp = {
"@id": "{}_{}".format(self.name, self.version), "@id": "{}_{}".format(self.name, self.version),
"enabled": self.enabled, "is_activated": self.is_activated,
} }
if hasattr(self, "repo") and self.repo: if hasattr(self, "repo") and self.repo:
resp["repo"] = self.repo.remotes[0].url resp["repo"] = self.repo.remotes[0].url

View File

@ -1,3 +0,0 @@
from senpy.plugins import SenpyPlugin
plugin = SenpyPlugin("dummy")

View File

@ -0,0 +1,6 @@
from senpy.plugins import SenpyPlugin
class DummyPlugin(SenpyPlugin):
def __init__(self):
super(DummyPlugin, self).__init__("dummy")

View File

@ -0,0 +1,8 @@
[Core]
Name = dummy
Module = dummy
[Documentation]
Description = What my plugin broadly does
Author = My very own name
Version = 0.1
Website = My very own website

View File

@ -13,10 +13,9 @@ from flask.ext.testing import TestCase
class ExtensionsTest(TestCase): class ExtensionsTest(TestCase):
def create_app(self): def create_app(self):
self.app = Flask("test_extensions") self.app = Flask("test_extensions")
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..") self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir) self.senpy = Senpy(plugin_folder=self.dir)
self.senpy.init_app(self.app)
return self.app return self.app
def test_init(self): def test_init(self):
@ -35,14 +34,14 @@ class ExtensionsTest(TestCase):
def test_enabling(self): def test_enabling(self):
""" Enabling a plugin """ """ Enabling a plugin """
self.senpy.enable_plugin("dummy") self.senpy.activate_plugin("dummy")
assert self.senpy.plugins["dummy"].enabled assert self.senpy.plugins["dummy"].is_activated
def test_disabling(self): def test_disabling(self):
""" Disabling a plugin """ """ Disabling a plugin """
self.senpy.enable_plugin("dummy") self.senpy.activate_plugin("dummy")
self.senpy.disable_plugin("dummy") self.senpy.deactivate_plugin("dummy")
assert not self.senpy.plugins["dummy"].enabled assert not self.senpy.plugins["dummy"].is_activated
def test_default(self): def test_default(self):
""" Default plugin should be set """ """ Default plugin should be set """
@ -57,7 +56,7 @@ class ExtensionsTest(TestCase):
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy") mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
mocked.assert_any_call(input="tupni", output="tuptuo") mocked.assert_any_call(input="tupni", output="tuptuo")
for plug in self.senpy.plugins: for plug in self.senpy.plugins:
self.senpy.disable_plugin(plug) self.senpy.deactivate_plugin(plug)
resp = self.senpy.analyse(input="tupni") resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp)) logging.debug("Response: {}".format(resp))
assert resp["status"] == 400 assert resp["status"] == 400
@ -66,6 +65,6 @@ class ExtensionsTest(TestCase):
""" Filtering plugins """ """ Filtering plugins """
assert len(self.senpy.filter_plugins(name="dummy")) > 0 assert len(self.senpy.filter_plugins(name="dummy")) > 0
assert not len(self.senpy.filter_plugins(name="notdummy")) assert not len(self.senpy.filter_plugins(name="notdummy"))
assert self.senpy.filter_plugins(name="dummy", enabled=True) assert self.senpy.filter_plugins(name="dummy", is_activated=True)
self.senpy.disable_plugin("dummy") self.senpy.deactivate_plugin("dummy")
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True)) assert not len(self.senpy.filter_plugins(name="dummy", is_activated=True))