1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 08:12:27 +00:00

Added plugins by default and monkey patching

Fixes #2
This commit is contained in:
J. Fernando Sánchez 2015-06-18 17:52:02 +02:00
parent 384aba4654
commit 7c2e0ddec7
12 changed files with 2765 additions and 19 deletions

View File

@ -1,4 +1,5 @@
include requirements.txt include requirements.txt
include test-requirements.txt
include README.md include README.md
include senpy/context.jsonld include senpy/context.jsonld
recursive-include *.senpy graft senpy/plugins

2728
logo.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 180 KiB

View File

@ -18,7 +18,7 @@
Blueprints for Senpy Blueprints for Senpy
""" """
from flask import Blueprint, request, current_app from flask import Blueprint, request, current_app
from .models import Error, Response from .models import Error, Response, Leaf
import json import json
import logging import logging
@ -158,9 +158,9 @@ def plugins(plugin=None, action="list"):
method = "{}_plugin".format(action) method = "{}_plugin".format(action)
if(hasattr(sp, method)): if(hasattr(sp, method)):
getattr(sp, method)(plugin) getattr(sp, method)(plugin)
return "Ok" return Leaf(message="Ok").flask()
else: else:
return "action '{}' not allowed".format(action), 400 return Error("action '{}' not allowed".format(action)).flask()
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,5 +1,9 @@
""" """
""" """
import gevent
from gevent import monkey
monkey.patch_all()
from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin
from .models import Error from .models import Error
from .blueprints import nif_blueprint from .blueprints import nif_blueprint
@ -23,15 +27,16 @@ class Senpy(object):
""" Default Senpy extension for Flask """ """ Default Senpy extension for Flask """
def __init__(self, app=None, plugin_folder="plugins"): def __init__(self, app=None, plugin_folder="plugins", base_plugins=True):
self.app = app self.app = app
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
self._search_folders = set() self._search_folders = set()
self._outdated = True self._outdated = True
for folder in (base_folder, plugin_folder): self.add_folder(plugin_folder)
self.add_folder(folder) if base_plugins:
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
self.add_folder(base_folder)
if app is not None: if app is not None:
self.init_app(app) self.init_app(app)
@ -124,7 +129,13 @@ class Senpy(object):
def activate_plugin(self, plugin_name, sync=False): def activate_plugin(self, plugin_name, sync=False):
plugin = self.plugins[plugin_name] plugin = self.plugins[plugin_name]
th = gevent.spawn(plugin.activate) def act():
try:
plugin.activate()
except Exception as ex:
logger.error("Error activating plugin {}: {}".format(plugin.name,
ex))
th = gevent.spawn(act)
th.link_value(partial(self._set_active_plugin, plugin_name, True)) th.link_value(partial(self._set_active_plugin, plugin_name, True))
if sync: if sync:
th.join() th.join()

View File

@ -239,7 +239,7 @@ class Emotion(Leaf):
_context = {} _context = {}
class Error(Response): class Error(Leaf):
# A better pattern would be this: # A better pattern would be this:
# http://flask.pocoo.org/docs/0.10/patterns/apierrors/ # http://flask.pocoo.org/docs/0.10/patterns/apierrors/
_frame = {} _frame = {}

View File

@ -1,16 +1,21 @@
import pip
from setuptools import setup from setuptools import setup
from pip.req import parse_requirements from pip.req import parse_requirements
# parse_requirements() returns generator of pip.req.InstallRequirement objects # parse_requirements() returns generator of pip.req.InstallRequirement objects
try:
install_reqs = parse_requirements("requirements.txt", session=pip.download.PipSession())
test_reqs = parse_requirements("test-requirements.txt", session=pip.download.PipSession())
except AttributeError:
install_reqs = parse_requirements("requirements.txt") install_reqs = parse_requirements("requirements.txt")
test_reqs = parse_requirements("test-requirements.txt")
# reqs is a list of requirement # reqs is a list of requirement
# e.g. ['django==1.5.1', 'mezzanine==1.4.6'] # e.g. ['django==1.5.1', 'mezzanine==1.4.6']
reqs = [str(ir.req) for ir in install_reqs] install_reqs = [str(ir.req) for ir in install_reqs]
test_reqs = [str(ir.req) for ir in test_reqs]
VERSION = "0.4.1" VERSION = "0.4.6"
print(reqs)
setup( setup(
name='senpy', name='senpy',
@ -27,6 +32,7 @@ extendable, so new algorithms and sources can be used.
.format(VERSION), .format(VERSION),
keywords=['eurosentiment', 'sentiment', 'emotions', 'nif'], keywords=['eurosentiment', 'sentiment', 'emotions', 'nif'],
classifiers=[], classifiers=[],
install_requires=reqs, install_requires=install_reqs,
tests_require=test_reqs,
include_package_data=True, include_package_data=True,
) )

0
test-requirements.txt Normal file
View File

View File

@ -15,7 +15,7 @@ class ExtensionsTest(TestCase):
def create_app(self): def create_app(self):
self.app = Flask("test_extensions") self.app = Flask("test_extensions")
self.dir = os.path.join(os.path.dirname(__file__), "..") self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy = Senpy(plugin_folder=self.dir) self.senpy = Senpy(plugin_folder=self.dir, base_plugins=False)
self.senpy.init_app(self.app) self.senpy.init_app(self.app)
self.senpy.activate_plugin("Dummy", sync=True) self.senpy.activate_plugin("Dummy", sync=True)
return self.app return self.app