From 603e086606ca2cdf3947007003fb46b9b41e3814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 14 Mar 2017 13:05:52 +0100 Subject: [PATCH 1/2] Fix list of plugins Closes #17 --- senpy/plugins/__init__.py | 11 +++++++---- senpy/schemas/context.jsonld | 3 +++ senpy/schemas/plugins.json | 1 + tests/plugins/sleep_plugin/sleep.py | 4 ++-- tests/test_models.py | 28 +++++++++++++++++++++------- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/senpy/plugins/__init__.py b/senpy/plugins/__init__.py index 9477448..a310aa6 100644 --- a/senpy/plugins/__init__.py +++ b/senpy/plugins/__init__.py @@ -13,7 +13,7 @@ from .. import models logger = logging.getLogger(__name__) -class SenpyPlugin(models.Plugin): +class Plugin(models.Plugin): def __init__(self, info=None): """ Provides a canonical name for plugins and serves as base for other @@ -24,7 +24,7 @@ class SenpyPlugin(models.Plugin): "information for the plugin.")) logger.debug("Initialising {}".format(info)) id = 'plugins/{}_{}'.format(info['name'], info['version']) - super(SenpyPlugin, self).__init__(id=id, **info) + super(Plugin, self).__init__(id=id, **info) self.is_activated = False def get_folder(self): @@ -37,7 +37,10 @@ class SenpyPlugin(models.Plugin): pass -class AnalysisPlugin(SenpyPlugin): +SenpyPlugin = Plugin + + +class AnalysisPlugin(Plugin): def analyse(self, *args, **kwargs): raise NotImplemented( @@ -58,7 +61,7 @@ class AnalysisPlugin(SenpyPlugin): yield i -class ConversionPlugin(SenpyPlugin): +class ConversionPlugin(Plugin): pass diff --git a/senpy/schemas/context.jsonld b/senpy/schemas/context.jsonld index 795b0e6..a171639 100644 --- a/senpy/schemas/context.jsonld +++ b/senpy/schemas/context.jsonld @@ -37,6 +37,9 @@ "@type": "@id", "@container": "@set" }, + "plugins": { + "@container": "@list" + }, "prov:wasGeneratedBy": { "@type": "@id" }, diff --git a/senpy/schemas/plugins.json b/senpy/schemas/plugins.json index 5866b49..cd7c937 100644 --- a/senpy/schemas/plugins.json +++ b/senpy/schemas/plugins.json @@ -6,6 +6,7 @@ "properties": { "plugins": { "type": "array", + "default": [], "items": { "$ref": "plugin.json" } diff --git a/tests/plugins/sleep_plugin/sleep.py b/tests/plugins/sleep_plugin/sleep.py index 510c2a9..30ff9db 100644 --- a/tests/plugins/sleep_plugin/sleep.py +++ b/tests/plugins/sleep_plugin/sleep.py @@ -1,8 +1,8 @@ -from senpy.plugins import SenpyPlugin +from senpy.plugins import AnalysisPlugin from time import sleep -class SleepPlugin(SenpyPlugin): +class SleepPlugin(AnalysisPlugin): def activate(self, *args, **kwargs): sleep(self.timeout) diff --git a/tests/test_models.py b/tests/test_models.py index 3aad3ca..25c53e5 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -11,8 +11,10 @@ from senpy.models import (Emotion, Entry, Error, Results, - Sentiment) -from senpy.plugins import SenpyPlugin + Sentiment, + Plugins, + Plugin) +from senpy import plugins from pprint import pprint @@ -53,8 +55,8 @@ class ModelsTest(TestCase): assert (received["entries"][0]["nif:isString"] != "Not testing") def test_id(self): - ''' Adding the id after creation should overwrite the automatic ID - ''' + """ Adding the id after creation should overwrite the automatic ID + """ r = Entry() j = r.jsonld() assert '@id' in j @@ -94,8 +96,8 @@ class ModelsTest(TestCase): r.validate() def test_plugins(self): - self.assertRaises(Error, SenpyPlugin) - p = SenpyPlugin({"name": "dummy", "version": 0}) + self.assertRaises(Error, plugins.Plugin) + p = plugins.Plugin({"name": "dummy", "version": 0}) c = p.jsonld() assert "info" not in c assert "repo" not in c @@ -107,7 +109,7 @@ class ModelsTest(TestCase): def test_str(self): """The string representation shouldn't include private variables""" r = Results() - p = SenpyPlugin({"name": "STR test", "version": 0}) + p = plugins.Plugin({"name": "STR test", "version": 0}) p._testing = 0 s = str(p) assert "_testing" not in s @@ -143,3 +145,15 @@ class ModelsTest(TestCase): print(t) g = rdflib.Graph().parse(data=t, format='turtle') assert len(g) == len(triples) + + def test_single_plugin(self): + """A response with a single plugin should still return a list""" + plugs = Plugins() + for i in range(10): + p = Plugin({'id': str(i), + 'version': 0, + 'description': 'dummy'}) + plugs.plugins.append(p) + assert isinstance(plugs.plugins, list) + js = plugs.jsonld() + assert isinstance(js['plugins'], list) From 250052fb99fdf747df5737294457919d9313c04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 14 Mar 2017 13:17:09 +0100 Subject: [PATCH 2/2] Options as a set in the JSON-LD context Closes #18 --- senpy/schemas/context.jsonld | 3 +++ tests/test_models.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/senpy/schemas/context.jsonld b/senpy/schemas/context.jsonld index a171639..4b77adb 100644 --- a/senpy/schemas/context.jsonld +++ b/senpy/schemas/context.jsonld @@ -40,6 +40,9 @@ "plugins": { "@container": "@list" }, + "options": { + "@container": "@set" + }, "prov:wasGeneratedBy": { "@type": "@id" }, diff --git a/tests/test_models.py b/tests/test_models.py index 25c53e5..3f0d736 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -97,7 +97,15 @@ class ModelsTest(TestCase): def test_plugins(self): self.assertRaises(Error, plugins.Plugin) - p = plugins.Plugin({"name": "dummy", "version": 0}) + p = plugins.Plugin({"name": "dummy", + "version": 0, + "extra_params": { + "none": { + "options": ["es", ], + "required": False, + "default": "0" + } + }}) c = p.jsonld() assert "info" not in c assert "repo" not in c @@ -105,6 +113,8 @@ class ModelsTest(TestCase): logging.debug("Framed:") logging.debug(c) p.validate() + assert "es" in c['extra_params']['none']['options'] + assert isinstance(c['extra_params']['none']['options'], list) def test_str(self): """The string representation shouldn't include private variables"""