mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-18 12:32:21 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
dabf444607 |
@@ -1,4 +1,3 @@
|
|||||||
include requirements.txt
|
include requirements.txt
|
||||||
include README.md
|
include README.md
|
||||||
include senpy/context.jsonld
|
include senpy/context.jsonld
|
||||||
recursive-include *.senpy
|
|
||||||
|
4
app.py
4
app.py
@@ -22,8 +22,6 @@ This class shows how to use the nif_server module to create custom services.
|
|||||||
import config
|
import config
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from senpy.extensions import Senpy
|
from senpy.extensions import Senpy
|
||||||
import logging
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -31,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)
|
||||||
|
@@ -36,7 +36,7 @@ class Sentiment140Plugin(SentimentPlugin):
|
|||||||
elif polarity_value < 50:
|
elif polarity_value < 50:
|
||||||
polarity = "marl:Negative"
|
polarity = "marl:Negative"
|
||||||
entry = Entry(text=params["input"])
|
entry = Entry(text=params["input"])
|
||||||
opinion = Opinion(hasPolarity=polarity, polarityValue=polarity_value)
|
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
|
||||||
entry.opinions.append(opinion)
|
entry.opinions.append(opinion)
|
||||||
entry.language = lang
|
entry.language = lang
|
||||||
response.entries.append(entry)
|
response.entries.append(entry)
|
||||||
|
45
plugins/yapsy_plugin/prueba.py
Normal file
45
plugins/yapsy_plugin/prueba.py
Normal 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()
|
8
plugins/yapsy_plugin/prueba.senpy
Normal file
8
plugins/yapsy_plugin/prueba.senpy
Normal 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
|
@@ -1,4 +1,5 @@
|
|||||||
Flask==0.10.1
|
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
|
@@ -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)
|
||||||
|
@@ -8,26 +8,25 @@
|
|||||||
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||||
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
||||||
"emotions": {
|
"emotions": {
|
||||||
"@container": "@set",
|
"@id": "onyx:hasEmotionSet",
|
||||||
"@id": "onyx:hasEmotionSet"
|
"@type": "onyx:EmotionSet"
|
||||||
},
|
},
|
||||||
"opinions": {
|
"opinions": {
|
||||||
"@container": "@set",
|
"@container": "@list",
|
||||||
"@id": "marl:hasOpinion"
|
"@id": "marl:hasOpinion",
|
||||||
|
"@type": "marl:Opinion"
|
||||||
},
|
},
|
||||||
"prov": "http://www.w3.org/ns/prov#",
|
"prov": "http://www.w3.org/ns/prov#",
|
||||||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
||||||
"analysis": {
|
"analysis": {
|
||||||
"@container": "@set",
|
|
||||||
"@id": "prov:wasInformedBy"
|
"@id": "prov:wasInformedBy"
|
||||||
},
|
},
|
||||||
"entries": {
|
"entries": {
|
||||||
"@container": "@set",
|
|
||||||
"@id": "prov:generated"
|
"@id": "prov:generated"
|
||||||
},
|
},
|
||||||
"strings": {
|
"strings": {
|
||||||
"@container": "@set",
|
"@reverse": "nif:hasContext",
|
||||||
"@reverse": "nif:hasContext"
|
"@type": "nif:String"
|
||||||
},
|
},
|
||||||
"date":
|
"date":
|
||||||
{
|
{
|
||||||
|
@@ -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,17 +68,24 @@ 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:
|
||||||
plug = self.plugins[algo]
|
if self.plugins[algo].is_activated:
|
||||||
resp = plug.analyse(**params)
|
plug = self.plugins[algo]
|
||||||
resp.analysis.append(plug.jsonable())
|
resp = plug.analyse(**params)
|
||||||
return resp
|
resp.analysis.append(plug.jsonable())
|
||||||
|
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) > 0:
|
if len(candidates) > 0:
|
||||||
candidate = candidates.keys()[0]
|
candidate = candidates.keys()[0]
|
||||||
logger.debug("Default: {}".format(candidate))
|
logger.debug("Default: {}".format(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):
|
||||||
@@ -169,4 +193,4 @@ class Senpy(object):
|
|||||||
def sentiment_plugins(self):
|
def sentiment_plugins(self):
|
||||||
""" Return only the sentiment plugins """
|
""" Return only the sentiment plugins """
|
||||||
return {p: plugin for p, plugin in self.plugins.items() if
|
return {p: plugin for p, plugin in self.plugins.items() if
|
||||||
isinstance(plugin, SentimentPlugin)}
|
isinstance(plugin, SentimentPlugin)}
|
@@ -4,48 +4,35 @@ from collections import defaultdict
|
|||||||
|
|
||||||
|
|
||||||
class Leaf(defaultdict):
|
class Leaf(defaultdict):
|
||||||
def __init__(self, context=None, ofclass=list):
|
def __init__(self, ofclass=list):
|
||||||
super(Leaf, self).__init__(ofclass)
|
super(Leaf, self).__init__(ofclass)
|
||||||
if context:
|
|
||||||
self.context = context
|
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name is not "context":
|
return super(Leaf, self).__getitem__(name)
|
||||||
return super(Leaf, self).__getitem__(name)
|
|
||||||
return self["@context"]
|
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
name = "@context" if name is "context" else name
|
self[name] = value
|
||||||
self[name] = self.get_context(value)
|
|
||||||
|
|
||||||
def __delattr__(self, name):
|
def __delattr__(self, name):
|
||||||
return super(Leaf, self).__delitem__(name)
|
return super(Leaf, self).__delitem__(name)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_context(context):
|
|
||||||
if isinstance(context, list):
|
|
||||||
contexts = []
|
|
||||||
for c in context:
|
|
||||||
contexts.append(Response.get_context(c))
|
|
||||||
return contexts
|
|
||||||
elif isinstance(context, dict):
|
|
||||||
return context
|
|
||||||
elif isinstance(context, basestring):
|
|
||||||
try:
|
|
||||||
with open(context) as f:
|
|
||||||
return json.loads(f.read())
|
|
||||||
except IOError:
|
|
||||||
return context
|
|
||||||
|
|
||||||
|
|
||||||
class Response(Leaf):
|
class Response(Leaf):
|
||||||
def __init__(self, context=None, *args, **kwargs):
|
def __init__(self, context=None):
|
||||||
|
super(Response, self).__init__()
|
||||||
|
self["analysis"] = []
|
||||||
|
self["entries"] = []
|
||||||
if context is None:
|
if context is None:
|
||||||
context = "{}/context.jsonld".format(os.path.dirname(
|
context = "{}/context.jsonld".format(os.path.dirname(
|
||||||
os.path.realpath(__file__)))
|
os.path.realpath(__file__)))
|
||||||
super(Response, self).__init__(*args, context=context, **kwargs)
|
if isinstance(context, dict):
|
||||||
self["analysis"] = []
|
self["@context"] = context
|
||||||
self["entries"] = []
|
if isinstance(context, str) or isinstance(context, unicode):
|
||||||
|
try:
|
||||||
|
with open(context) as f:
|
||||||
|
self["@context"] = json.loads(f.read())
|
||||||
|
except IOError:
|
||||||
|
self["@context"] = context
|
||||||
|
|
||||||
|
|
||||||
class Entry(Leaf):
|
class Entry(Leaf):
|
||||||
@@ -60,27 +47,17 @@ class Entry(Leaf):
|
|||||||
|
|
||||||
|
|
||||||
class Opinion(Leaf):
|
class Opinion(Leaf):
|
||||||
opinionContext = {
|
def __init__(self, polarity_value=None, polarity=None, **kwargs):
|
||||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
|
super(Opinion, self).__init__(**kwargs)
|
||||||
}
|
if polarity_value is not None:
|
||||||
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
|
self.polarity_value = polarity_value
|
||||||
super(Opinion, self).__init__(context=self.opinionContext,
|
if polarity is not None:
|
||||||
*args,
|
self.polarity = polarity
|
||||||
**kwargs)
|
|
||||||
if polarityValue is not None:
|
|
||||||
self.polarityValue = polarityValue
|
|
||||||
if hasPolarity is not None:
|
|
||||||
self.hasPolarity = hasPolarity
|
|
||||||
|
|
||||||
|
|
||||||
class EmotionSet(Leaf):
|
class EmotionSet(Leaf):
|
||||||
emotionContext = {
|
def __init__(self, emotions=None, **kwargs):
|
||||||
"@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"
|
|
||||||
}
|
|
||||||
def __init__(self, emotions=None, *args, **kwargs):
|
|
||||||
if not emotions:
|
if not emotions:
|
||||||
emotions = []
|
emotions = []
|
||||||
super(EmotionSet, self).__init__(context=self.emotionContext,
|
super(EmotionSet, self).__init__(**kwargs)
|
||||||
*args,
|
|
||||||
**kwargs)
|
|
||||||
self.emotions = emotions or []
|
self.emotions = emotions or []
|
||||||
|
@@ -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
|
||||||
|
2
setup.py
2
setup.py
@@ -8,7 +8,7 @@ install_reqs = parse_requirements("requirements.txt")
|
|||||||
# 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]
|
reqs = [str(ir.req) for ir in install_reqs]
|
||||||
|
|
||||||
VERSION = "0.2.9"
|
VERSION = "0.2.8"
|
||||||
|
|
||||||
print(reqs)
|
print(reqs)
|
||||||
|
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
from senpy.plugins import SenpyPlugin
|
|
||||||
|
|
||||||
plugin = SenpyPlugin("dummy")
|
|
6
tests/dummy_plugin/dummy.py
Normal file
6
tests/dummy_plugin/dummy.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
|
||||||
|
class DummyPlugin(SenpyPlugin):
|
||||||
|
def __init__(self):
|
||||||
|
super(DummyPlugin, self).__init__("dummy")
|
||||||
|
|
8
tests/dummy_plugin/dummy.senpy
Normal file
8
tests/dummy_plugin/dummy.senpy
Normal 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
|
@@ -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))
|
||||||
|
Reference in New Issue
Block a user