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

Compare commits

...

8 Commits
0.1 ... 0.2.6

Author SHA1 Message Date
J. Fernando Sánchez
eaf65f0c6b First tests 2014-11-07 19:12:21 +01:00
J. Fernando Sánchez
a5e79bead3 Version 0.2.5 - Pypi 2014-11-05 19:21:17 +01:00
J. Fernando Sánchez
54492472ba Files for deployment 2014-11-05 19:17:27 +01:00
J. Fernando Sánchez
ff8d12074b Improved plugins (reload, imp) 2014-11-04 21:31:41 +01:00
J. Fernando Sánchez
bdf1992775 Fixed plugins 2014-10-17 19:14:49 +02:00
J. Fernando Sánchez
e06fc2e671 V 0.2.2 - Better plugins 2014-10-17 19:06:19 +02:00
J. Fernando Sánchez
8405e5deef Added plugin architecture 2014-10-17 12:47:17 +02:00
J. Fernando Sánchez
d0aa889124 Fixed download_url 2014-09-23 12:37:54 +02:00
20 changed files with 674 additions and 129 deletions

View File

@@ -1,4 +1,4 @@
![GSI Logo](http://gsi.dit.upm.es/templates/jgsi/images/logo.png)
![GSI Logo](logo.png)
[Senpy](http://senpy.herokuapp.com)
=========================================
Example endpoint that yields results compatible with the EUROSENTIMENT format and exposes the NIF API.

42
app.py
View File

@@ -20,48 +20,14 @@ Simple Sentiment Analysis server for EUROSENTIMENT
This class shows how to use the nif_server module to create custom services.
'''
import config
import re
from flask import Flask
import random
from senpy.nif_server import *
from senpy.extensions import Senpy
app = Flask(__name__)
rgx = re.compile("(\w[\w']*\w|\w)", re.U)
def hard_analysis(params):
response = basic_analysis(params)
response["analysis"][0].update({ "marl:algorithm": "SimpleAlgorithm",
"marl:minPolarityValue": -1,
"marl:maxPolarityValue": 1})
for i in response["entries"]:
contextid = i["@id"]
random.seed(str(params))
polValue = 2*random.random()-1
if polValue > 0:
pol = "marl:Positive"
elif polValue == 0:
pol = "marl:Neutral"
else:
pol = "marl:Negative"
i["opinions"] = [{"marl:polarityValue": polValue,
"marl:hasPolarity": pol
}]
i["strings"] = []
for m in rgx.finditer(i["nif:isString"]):
i["strings"].append({
"@id": "{}#char={},{}".format(contextid, m.start(), m.end()),
"nif:beginIndex": m.start(),
"nif:endIndex": m.end(),
"nif:anchorOf": m.group(0)
})
return response
app.analyse = hard_analysis
app.register_blueprint(nif_server)
sp = Senpy()
sp.init_app(app)
if __name__ == '__main__':
app.debug = config.DEBUG
app.run()
app.run(host="0.0.0.0", use_reloader=False)

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@@ -0,0 +1,9 @@
from senpy.plugins import SenpyPlugin
class Prueba(SenpyPlugin):
def __init__(self, **kwargs):
super(Prueba, self).__init__(name="prueba",
version="4.0",
**kwargs)
plugin = Prueba()

View File

@@ -0,0 +1,47 @@
import requests
import json
import sys
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()
polarityValue = int(res.json()["data"][0]["polarity"]) * 25
polarity = "marl:Neutral"
if polarityValue > 50:
polarity = "marl:Positive"
elif polarityValue < 50:
polarity = "marl:Negative"
entry = Entry(text=params["input"])
opinion = Opinion(polarity=polarity, polarityValue=polarityValue)
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)
return response
plugin = Sentiment140Plugin()

View File

@@ -1,3 +1,5 @@
Flask==0.10.1
gunicorn==19.0.0
requests==2.4.1
Flask-Plugins==1.4
GitPython==0.3.2.RC1

View File

@@ -0,0 +1,34 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
# DIT, UPM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Sentiment analysis server in Python
'''
VERSION = "0.2.6"
import extensions
import blueprints
import plugins
if __name__ == '__main__':
from flask import Flask
app = Flask(__name__)
sp = extensions.Senpy()
sp.init_app(app)
app.debug = config.DEBUG
app.run()

7
senpy/__main__.py Normal file
View File

@@ -0,0 +1,7 @@
from flask import Flask
from extensions import Senpy
app = Flask(__name__)
sp = Senpy()
sp.init_app(app)
app.debug = True
app.run()

View File

@@ -17,47 +17,21 @@
'''
Simple Sentiment Analysis server
'''
from flask import Blueprint, render_template, request, jsonify, current_app
import config
import json
import logging
logger = logging.getLogger(__name__)
nif_server = Blueprint("NIF Sentiment Analysis Server", __name__)
from flask import Blueprint, render_template, request, jsonify, current_app
PARAMS = {"input": {"aliases": ["i", "input"],
"required": True,
"help": "Input text"
},
"informat": {"aliases": ["f", "informat"],
"required": False,
"default": "text",
"options": ["turtle", "text"],
},
"intype": {"aliases": ["intype", "t"],
"required": False,
"default": "direct",
"options": ["direct", "url", "file"],
},
"outformat": {"aliases": ["outformat", "o"],
"default": "json-ld",
"required": False,
"options": ["json-ld"],
},
"algorithm": {"aliases": ["algorithm", "a"],
"required": False,
},
"language": {"aliases": ["language", "l"],
"required": False,
"options": ["es", "en"],
},
"urischeme": {"aliases": ["urischeme", "u"],
"required": False,
"default": "RFC5147String",
"options": "RFC5147String"
},
}
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
BASIC_PARAMS = {
"algorithm": {"aliases": ["algorithm", "a", "algo"],
"required": False,
},
}
def get_params(req):
def get_params(req, params=BASIC_PARAMS):
indict = None
if req.method == 'POST':
indict = req.form
@@ -68,20 +42,20 @@ def get_params(req):
outdict = {}
wrongParams = {}
for param, options in PARAMS.iteritems():
for param, options in params.iteritems():
for alias in options["aliases"]:
if alias in indict:
outdict[param] = indict[alias]
if param not in outdict:
if options.get("required", False):
wrongParams[param] = PARAMS[param]
wrongParams[param] = params[param]
else:
if "default" in options:
outdict[param] = options["default"]
else:
if "options" in PARAMS[param] and \
outdict[param] not in PARAMS[param]["options"]:
wrongParams[param] = PARAMS[param]
if "options" in params[param] and \
outdict[param] not in params[param]["options"]:
wrongParams[param] = params[param]
if wrongParams:
message = {"status": "failed", "message": "Missing or invalid parameters"}
message["parameters"] = outdict
@@ -109,18 +83,54 @@ def basic_analysis(params):
})
return response
@nif_server.route('/', methods=['POST', 'GET'])
@nif_blueprint.route('/', methods=['POST', 'GET'])
def home(entries=None):
try:
params = get_params(request)
algo = get_params(request).get("algorithm", None)
specific_params = current_app.senpy.parameters(algo)
params = get_params(request, specific_params)
response = current_app.senpy.analyse(**params)
return jsonify(response)
except ValueError as ex:
return ex.message
response = current_app.analyse(params)
return jsonify(response)
except Exception as ex:
return jsonify(status="400", message=ex.message)
@nif_blueprint.route("/default")
def default():
return current_app.senpy.default_plugin
#return plugins(action="list", plugin=current_app.senpy.default_algorithm)
@nif_blueprint.route('/plugins/', methods=['POST', 'GET'])
@nif_blueprint.route('/plugins/<plugin>', methods=['POST', 'GET'])
@nif_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
def plugins(plugin=None, action="list"):
filt = {}
if plugin:
filt["name"] = plugin
plugs = current_app.senpy.filter_plugins(**filt)
if plugin and not plugs:
return "Plugin not found", 400
if action == "list":
with_params = request.args.get("params", "") == "1"
dic = {plug:plugs[plug].jsonable(with_params) for plug in plugs}
return jsonify(dic)
if action == "disable":
current_app.senpy.disable_plugin(plugin)
return "Ok"
elif action == "enable":
current_app.senpy.enable_plugin(plugin)
return "Ok"
elif action == "reload":
current_app.senpy.reload_plugin(plugin)
return "Ok"
else:
return "action '{}' not allowed".format(action), 400
if __name__ == '__main__':
import config
from flask import Flask
app = Flask(__name__)
app.register_blueprint(nif_server)
app.register_blueprint(nif_blueprint)
app.debug = config.DEBUG
app.run()

38
senpy/context.jsonld Normal file
View File

@@ -0,0 +1,38 @@
{
"dc": "http://purl.org/dc/terms/",
"dc:subject": {
"@type": "@id"
},
"xsd": "http://www.w3.org/2001/XMLSchema#",
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
"emotions": {
"@id": "onyx:hasEmotionSet",
"@type": "onyx:EmotionSet"
},
"opinions": {
"@container": "@list",
"@id": "marl:hasOpinion",
"@type": "marl:Opinion"
},
"prov": "http://www.w3.org/ns/prov#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"analysis": {
"@id": "prov:wasInformedBy"
},
"entries": {
"@id": "prov:generated"
},
"strings": {
"@reverse": "nif:hasContext",
"@type": "nif:String"
},
"date":
{
"@id": "dc:date",
"@type": "xsd:dateTime"
},
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
}

183
senpy/extensions.py Normal file
View File

@@ -0,0 +1,183 @@
import os
import sys
import imp
import logging
logger = logging.getLogger(__name__)
from .plugins import SentimentPlugin, EmotionPlugin
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
from .blueprints import nif_blueprint
from git import Repo, InvalidGitRepositoryError
class Senpy(object):
""" Default Senpy extension for Flask """
def __init__(self, app=None, plugin_folder="plugins"):
self.app = app
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
self._search_folders = set()
self._outdated = True
for folder in (base_folder, plugin_folder):
self.add_folder(folder)
if app is not None:
self.init_app(app)
def init_app(self, app):
""" Initialise a flask app to add plugins to its context """
"""
Note: I'm not particularly fond of adding self.app and app.senpy, but
I can't think of a better way to do it.
"""
app.senpy = self
# Use the newstyle teardown_appcontext if it's available,
# otherwise fall back to the request context
if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
app.register_blueprint(nif_blueprint)
def add_folder(self, folder):
if os.path.isdir(folder):
self._search_folders.add(folder)
self._outdated = True
return True
else:
return False
def analyse(self, **params):
algo = None
logger.debug("analysing with params: {}".format(params))
if "algorithm" in params:
algo = params["algorithm"]
elif self.plugins:
algo = self.default_plugin
if algo in self.plugins and self.plugins[algo].enabled:
plug = self.plugins[algo]
resp = plug.analyse(**params)
resp.analysis.append(plug.jsonable())
return resp
else:
return {"status": 400, "message": "The algorithm '{}' is not valid".format(algo) }
@property
def default_plugin(self):
candidates = self.filter_plugins(enabled=True)
if len(candidates)>1:
candidate = candidates.keys()[0]
logger.debug("Default: {}".format(candidate))
return candidate
else:
return None
def parameters(self, algo):
return getattr(self.plugins.get(algo or self.default_plugin), "params", {})
def enable_plugin(self, plugin):
self.plugins[plugin].enable()
def disable_plugin(self, plugin):
self.plugins[plugin].disable()
def reload_plugin(self, plugin):
logger.debug("Reloading {}".format(plugin))
plug = self.plugins[plugin]
nplug = self._load_plugin(plug.module, plug.path)
del self.plugins[plugin]
self.plugins[nplug.name] = nplug
def _load_plugin(self, plugin, search_folder, enabled=True):
logger.debug("Loading plugins")
sys.path.append(search_folder)
(fp, pathname, desc) = imp.find_module(plugin)
try:
tmp = imp.load_module(plugin, fp, pathname, desc).plugin
sys.path.remove(search_folder)
tmp.path = search_folder
try:
repo_path = os.path.join(search_folder, plugin)
tmp.repo = Repo(repo_path)
except InvalidGitRepositoryError:
tmp.repo = None
if not hasattr(tmp, "enabled"):
tmp.enabled = enabled
tmp.module = plugin
except Exception as ex:
tmp = None
logger.debug("Exception importing {}: {}".format(plugin, ex))
return tmp
def _load_plugins(self):
plugins = {}
for search_folder in self._search_folders:
for item in os.listdir(search_folder):
if os.path.isdir(os.path.join(search_folder, item)) \
and os.path.exists(
os.path.join(search_folder, item, "__init__.py")):
plugin = self._load_plugin(item, search_folder)
if plugin:
plugins[plugin.name] = plugin
self._outdated = False
return plugins
def teardown(self, exception):
pass
def enable_all(self):
for plugin in self.plugins:
self.enable_plugin(plugin)
def enable_plugin(self, item):
self.plugins[item].enabled = True
def disable_plugin(self, item):
self.plugins[item].enabled = False
@property
def plugins(self):
""" Return the plugins registered for a given application. """
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'senpy_plugins') or self._outdated:
ctx.senpy_plugins = self._load_plugins()
return ctx.senpy_plugins
def filter_plugins(self, **kwargs):
""" Filter plugins by different criteria """
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 not kwargs:
return self.plugins
else:
return {n:p for n, p in self.plugins.items() if matches(p)}
def sentiment_plugins(self):
""" Return only the sentiment plugins """
return {p:plugin for p, plugin in self.plugins.items() if
isinstance(plugin, SentimentPlugin)}
if __name__ == '__main__':
from flask import Flask
app = Flask(__name__)
sp = Senpy()
sp.init_app(APP)
with APP.app_context():
sp._load_plugins()

59
senpy/models.py Normal file
View File

@@ -0,0 +1,59 @@
import json
import os
from collections import defaultdict
class Leaf(defaultdict):
def __init__(self, ofclass=list):
super(Leaf, self).__init__(ofclass)
def __getattr__(self, name):
return super(Leaf, self).__getitem__(name)
def __setattr__(self, name, value):
self[name] = value
def __delattr__(self, name):
return super(Leaf, self).__delitem__(name)
class Response(Leaf):
def __init__(self, context=None):
super(Response, self).__init__()
self["analysis"] = []
self["entries"] = []
if context is None:
context = "{}/context.jsonld".format(os.path.dirname(
os.path.realpath(__file__)))
if isinstance(context, dict):
self["@context"] = context
if isinstance(context, basestring):
try:
with open(context) as f:
self["@context"] = json.loads(f.read())
except IOError:
self["@context"] = context
class Entry(Leaf):
def __init__(self, text=None, emotionSets=None, opinions=None, **kwargs):
super(Entry, self).__init__(**kwargs)
if text:
self.text = text
if emotionSets:
self.emotionSets = emotionSets
if opinions:
self.opinions = opinions
class Opinion(Leaf):
def __init__(self, polarityValue=None, polarity=None, **kwargs):
super(Opinion, self).__init__(**kwargs)
if polarityValue is not None:
self.polarityValue = polarityValue
if polarity is not None:
self.polarity = polarity
class EmotionSet(Leaf):
def __init__(self, emotions=[], **kwargs):
super(EmotionSet, self).__init__(**kwargs)
self.emotions = emotions or []

101
senpy/plugins.py Normal file
View File

@@ -0,0 +1,101 @@
import logging
logger = logging.getLogger(__name__)
PARAMS = {"input": {"aliases": ["i", "input"],
"required": True,
"help": "Input text"
},
"informat": {"aliases": ["f", "informat"],
"required": False,
"default": "text",
"options": ["turtle", "text"],
},
"intype": {"aliases": ["intype", "t"],
"required": False,
"default": "direct",
"options": ["direct", "url", "file"],
},
"outformat": {"aliases": ["outformat", "o"],
"default": "json-ld",
"required": False,
"options": ["json-ld"],
},
"language": {"aliases": ["language", "l"],
"required": False,
"options": ["es", "en"],
},
"urischeme": {"aliases": ["urischeme", "u"],
"required": False,
"default": "RFC5147String",
"options": "RFC5147String"
},
}
class SenpyPlugin(object):
def __init__(self, name=None, version=None, extraparams=None, params=None):
logger.debug("Initialising {}".format(name))
self.name = name
self.version = version
if params:
self.params = params
else:
self.params = PARAMS.copy()
if extraparams:
self.params.update(extraparams)
self.extraparams = extraparams or {}
self.enabled = True
def analyse(self, *args, **kwargs):
pass
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def jsonable(self, parameters=False):
resp = {
"@id": "{}_{}".format(self.name, self.version),
"enabled": self.enabled,
}
if self.repo:
resp["repo"] = self.repo.remotes[0].url
if parameters:
resp["parameters"] = self.params
elif self.extraparams:
resp["extra_parameters"] = self.extraparams
return resp
class SentimentPlugin(SenpyPlugin):
def __init__(self,
minPolarityValue=0,
maxPolarityValue=1,
**kwargs):
super(SentimentPlugin, self).__init__(**kwargs)
self.minPolarityValue = minPolarityValue
self.maxPolarityValue = maxPolarityValue
def jsonable(self, *args, **kwargs):
resp = super(SentimentPlugin, self).jsonable(*args, **kwargs)
resp["marl:maxPolarityValue"] = self.maxPolarityValue
resp["marl:minPolarityValue"] = self.minPolarityValue
return resp
class EmotionPlugin(SenpyPlugin):
def __init__(self,
minEmotionValue=0,
maxEmotionValue=1,
emotionCategory=None,
**kwargs):
super(EmotionPlugin, self).__init__(**kwargs)
self.minEmotionValue = minEmotionValue
self.maxEmotionValue = maxEmotionValue
self.emotionCategory = emotionCategory
def jsonable(self, *args, **kwargs):
resp = super(EmotionPlugin, self).jsonable(*args, **kwargs)
resp["onyx:minEmotionValue"] = self.minEmotionValue
resp["onyx:maxEmotionValue"] = self.maxEmotionValue
return resp

View File

@@ -1,41 +0,0 @@
'''
SENTIMENT140
=============
* http://www.sentiment140.com/api/bulkClassifyJson
* Method: POST
* Parameters: JSON Object (that is copied to the result)
* text
* query
* language
* topic
* Example response:
```json
{"data": [{"text": "I love Titanic.", "id":1234, "polarity": 4},
{"text": "I hate Titanic.", "id":4567, "polarity": 0}]}
```
'''
import requests
import json
ENDPOINT_URI = "http://www.sentiment140.com/api/bulkClassifyJson"
def analyse(texts):
parameters = {"data": []}
if isinstance(texts, list):
for text in texts:
parameters["data"].append({"text": text})
else:
parameters["data"].append({"text": texts})
res = requests.post(ENDPOINT_URI, json.dumps(parameters))
res.json()
return res.json()
def test():
print analyse("I love Titanic")
print analyse(["I love Titanic", "I hate Titanic"])
if __name__ == "__main__":
test()

View File

@@ -1,8 +1,10 @@
from distutils.core import setup
from setuptools import setup
import senpy
setup(
name = 'senpy',
packages = ['senpy'], # this must be the same as the name above
version = '0.1',
version = senpy.VERSION,
description = '''
A sentiment analysis server implementation. Designed to be \
extendable, so new algorithms and sources can be used.
@@ -10,7 +12,7 @@ extendable, so new algorithms and sources can be used.
author = 'J. Fernando Sanchez',
author_email = 'balkian@gmail.com',
url = 'https://github.com/balkian/senpy', # use the URL to the github repo
download_url = 'https://github.com/balkian/senpy/tarball/0.1', # I'll explain this in a second
download_url = 'https://github.com/balkian/senpy/archive/{}.tar.gz'.format(senpy.VERSION),
keywords = ['eurosentiment', 'sentiment', 'emotions', 'nif'], # arbitrary keywords
classifiers = [],
)

20
supervisord.conf Normal file
View File

@@ -0,0 +1,20 @@
[unix_http_server]
file=/tmp/senpy.sock ; path to your socket file
[supervisord]
logfile = %(here)s/logs/supervisor.log
childlogdir = %(here)s/logs/
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
logfile = %(here)s/logs/supervisorctl.log
serverurl=unix:///tmp/senpy.sock ; use a unix:// URL for a unix socket
[program:senpy]
command = venv/bin/gunicorn -w 1 -b 0.0.0.0:6666 --log-file %(here)s/logs/gunicorn.log --access-logfile - app:app
directory = %(here)s
environment = PATH=%(here)s/venv/bin/
logfile = %(here)s/logs/senpy.log

View File

@@ -0,0 +1,35 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
def check_dict(indic, template):
return all(item in indic.items() for item in template.items())
class Blueprints_Test(TestCase):
def create_app(self):
self.app = Flask("test_extensions")
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir)
return self.app
def test_home(self):
""" Calling with no arguments should ask the user for more arguments """
resp = self.client.get("/")
self.assert200(resp)
logging.debug(resp.json)
assert resp.json["status"] == "failed"
atleast = {
"status": "failed",
"message": "Missing or invalid parameters",
}
assert check_dict(resp.json, atleast)

View File

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

View File

@@ -0,0 +1,70 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
class Extensions_Test(TestCase):
def create_app(self):
self.app = Flask("test_extensions")
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir)
return self.app
def test_init(self):
""" Initialising the app with the extension. """
assert hasattr(self.app, "senpy")
tapp = Flask("temp app")
tsen = Senpy(tapp)
assert hasattr(tapp, "senpy")
def test_discovery(self):
""" Discovery of plugins in given folders. """
assert self.dir in self.senpy._search_folders
print self.senpy.plugins
assert "dummy" in self.senpy.plugins
def test_enabling(self):
""" Enabling a plugin """
self.senpy.enable_plugin("dummy")
assert self.senpy.plugins["dummy"].enabled
def test_disabling(self):
""" Disabling a plugin """
self.senpy.enable_plugin("dummy")
self.senpy.disable_plugin("dummy")
assert self.senpy.plugins["dummy"].enabled == False
def test_default(self):
""" Default plugin should be set """
assert self.senpy.default_plugin
assert self.senpy.default_plugin == "dummy"
def test_analyse(self):
""" Using a plugin """
with mock.patch.object(self.senpy.plugins["dummy"], "analyse") as mocked:
self.senpy.analyse(algorithm="dummy", input="tupni", output="tuptuo")
self.senpy.analyse(input="tupni", output="tuptuo")
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
mocked.assert_any_call(input="tupni", output="tuptuo")
for plug in self.senpy.plugins:
self.senpy.disable_plugin(plug)
resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp))
assert resp["status"] == 400
def test_filtering(self):
""" Filtering plugins """
assert len(self.senpy.filter_plugins(name="dummy"))>0
assert not len(self.senpy.filter_plugins(name="notdummy"))
assert self.senpy.filter_plugins(name="dummy", enabled=True)
self.senpy.disable_plugin("dummy")
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))