1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-09-13 10:02:20 +00:00

Prefix handling and bug fixes

This commit is contained in:
J. Fernando Sánchez
2016-02-21 02:53:39 +01:00
parent 48d7d1d02e
commit 14a3e4103b
9 changed files with 88 additions and 59 deletions

View File

@@ -38,7 +38,13 @@ API_PARAMS = {
"aliases": ["inHeaders", "headers"],
"required": True,
"default": "0"
}
},
"prefix": {
"@id": "prefix",
"aliases": ["prefix", "p"],
"required": True,
"default": "",
},
}
BASIC_PARAMS = {
@@ -171,7 +177,8 @@ def api():
params.update(get_params(request, specific_params))
response = current_app.senpy.analyse(**params)
in_headers = params["inHeaders"] != "0"
return response.flask(in_headers=in_headers)
prefix = params["prefix"]
return response.flask(in_headers=in_headers, prefix=prefix)
except Error as ex:
return ex.message.flask()
@@ -186,6 +193,7 @@ def plugins():
@nif_blueprint.route('/plugins/<plugin>/', methods=['POST', 'GET'])
@nif_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
def plugin(plugin=None, action="list"):
params = get_params(request, API_PARAMS)
filt = {}
sp = current_app.senpy
plugs = sp.filter_plugins(name=plugin)
@@ -197,8 +205,9 @@ def plugin(plugin=None, action="list"):
else:
return Error(message="Plugin not found", status=404).flask()
if action == "list":
in_headers = get_params(request, API_PARAMS)["inHeaders"] != "0"
return response.flask(in_headers=in_headers)
in_headers = params["inHeaders"] != "0"
prefix = params['prefix']
return response.flask(in_headers=in_headers, prefix=prefix)
method = "{}_plugin".format(action)
if(hasattr(sp, method)):
getattr(sp, method)(plugin)

View File

@@ -69,9 +69,12 @@ class SenpyMixin(object):
def flask(self,
in_headers=False,
url="http://demos.gsi.dit.upm.es/senpy/senpy.jsonld"):
url="http://demos.gsi.dit.upm.es/senpy/senpy.jsonld",
prefix=None):
"""
Return the values and error to be used in flask
Return the values and error to be used in flask.
So far, it returns a fixed context. We should store/generate different
contexts if the plugin adds more aliases.
"""
headers = None
if in_headers:
@@ -80,7 +83,8 @@ class SenpyMixin(object):
'rel="http://www.w3.org/ns/json-ld#context";'
' type="application/ld+json"' % url)
}
return FlaskResponse(self.to_JSON(with_context=not in_headers),
return FlaskResponse(self.to_JSON(with_context=not in_headers,
prefix=prefix),
status=getattr(self, "status", 200),
headers=headers,
mimetype="application/json")
@@ -103,11 +107,14 @@ class SenpyMixin(object):
return ser_or_down(self._plain_dict())
def jsonld(self, context=None, with_context=False):
def jsonld(self, context=None, prefix=None, with_context=False):
ser = self.serializable()
if with_context:
ser["@context"] = self.context
ser["@context"] = self.context.copy()
if prefix:
ser["@context"]["@base"] = prefix
return ser
@@ -129,8 +136,17 @@ class SenpyModel(SenpyMixin, dict):
schema = base_schema
def __init__(self, *args, **kwargs):
self.id = kwargs.pop('id', '{}_{}'.format(type(self).__name__,
time.time()))
temp = dict(*args, **kwargs)
for i in temp:
nk = self._get_key(i)
if nk != i:
temp[nk] = temp[i]
del temp[i]
reqs = self.schema.get('required', [])
for i in reqs:
if i not in temp:
@@ -175,30 +191,15 @@ class SenpyModel(SenpyMixin, dict):
def _plain_dict(self):
d = { k: v for (k,v) in self.items() if k[0] != "_"}
if hasattr(self, "id"):
d["@id"] = self.id
d["@id"] = d.pop('id')
return d
@property
def id(self):
if not hasattr(self, '_id'):
self.__dict__["_id"] = '_:{}_{}'.format(type(self).__name__, time.time())
return self._id
@id.setter
def id(self, value):
self._id = value
class Response(SenpyModel):
schema = read_schema('response.json')
class Results(SenpyModel):
schema = read_schema('results.json')
def jsonld(self, context=None, with_context=True):
return super(Results, self).jsonld(context, with_context)
class Entry(SenpyModel):
schema = read_schema('entry.json')
@@ -211,6 +212,9 @@ class Analysis(SenpyModel):
class EmotionSet(SenpyModel):
schema = read_schema('emotionSet.json')
class Emotion(SenpyModel):
schema = read_schema('emotion.json')
class Suggestion(SenpyModel):
schema = read_schema('suggestion.json')

View File

@@ -17,6 +17,7 @@ class SenpyPlugin(PluginModel):
"information for the plugin."))
logger.debug("Initialising {}".format(info))
super(SenpyPlugin, self).__init__(info)
self.id = '{}_{}'.format(self.name, self.version)
self.params = info.get("extra_params", {})
self._info = info
if "@id" not in self.params:
@@ -36,10 +37,6 @@ class SenpyPlugin(PluginModel):
def deactivate(self):
pass
@property
def id(self):
return "{}_{}".format(self.name, self.version)
def __del__(self):
''' Destructor, to make sure all the resources are freed '''
self.deactivate()

View File

@@ -115,7 +115,11 @@
"type": "string"
},
"onyx:hasEmotion": {
"$ref": "#/Emotion"
"type": "array",
"items": {
"$ref": "#/Emotion"
},
"default": []
},
"prov:wasGeneratedBy": {
"type": "string",

View File

@@ -17,37 +17,38 @@ function encodeHTML(text) {
$(document).ready(function() {
var response = JSON.parse($.ajax({type: "GET", url: "/api/plugins/" , async: false}).responseText);
var defaultPlugin= JSON.parse($.ajax({type: "GET", url: "/api/default" , async: false}).responseText);
var defaultPlugin= JSON.parse($.ajax({type: "GET", url: "/api/plugins/default" , async: false}).responseText);
html="";
for (r in response){
if (response[r]["name"]){
if (response[r]["name"] == defaultPlugin["name"]){
if (response[r]["is_activated"]){
html+= "<option value=\""+response[r]["name"]+"\" selected=\"selected\">"+response[r]["name"]+"</option>"
plugins = response.plugins;
for (r in plugins){
if (plugins[r]["name"]){
if (plugins[r]["name"] == defaultPlugin["name"]){
if (plugins[r]["is_activated"]){
html+= "<option value=\""+plugins[r]["name"]+"\" selected=\"selected\">"+plugins[r]["name"]+"</option>"
}else{
html+= "<option value=\""+response[r]["name"]+"\" selected=\"selected\" disabled=\"disabled\">"+response[r]["name"]+"</option>"
html+= "<option value=\""+plugins[r]["name"]+"\" selected=\"selected\" disabled=\"disabled\">"+plugins[r]["name"]+"</option>"
}
}
else{
if (response[r]["is_activated"]){
html+= "<option value=\""+response[r]["name"]+"\">"+response[r]["name"]+"</option>"
if (plugins[r]["is_activated"]){
html+= "<option value=\""+plugins[r]["name"]+"\">"+plugins[r]["name"]+"</option>"
}
else{
html+= "<option value=\""+response[r]["name"]+"\" disabled=\"disabled\">"+response[r]["name"]+"</option>"
html+= "<option value=\""+plugins[r]["name"]+"\" disabled=\"disabled\">"+plugins[r]["name"]+"</option>"
}
}
}
if (response[r]["extra_params"]){
plugins_params[response[r]["name"]]={};
for (param in response[r]["extra_params"]){
if (typeof response[r]["extra_params"][param] !="string"){
if (plugins[r]["extra_params"]){
plugins_params[plugins[r]["name"]]={};
for (param in plugins[r]["extra_params"]){
if (typeof plugins[r]["extra_params"][param] !="string"){
var params = new Array();
var alias = response[r]["extra_params"][param]["aliases"][0];
var alias = plugins[r]["extra_params"][param]["aliases"][0];
params[alias]=new Array();
for (option in response[r]["extra_params"][param]["options"]){
params[alias].push(response[r]["extra_params"][param]["options"][option])
for (option in plugins[r]["extra_params"][param]["options"]){
params[alias].push(plugins[r]["extra_params"][param]["options"][option])
}
plugins_params[response[r]["name"]][alias] = (params[alias])
plugins_params[plugins[r]["name"]][alias] = (params[alias])
}
}
}
@@ -91,4 +92,4 @@ function load_JSON(){
document.getElementById("results").innerHTML = replaceURLWithHTMLLinks(JSON.stringify(response, undefined, 2))
document.getElementById("input_request").innerHTML = "<label>"+url+"</label>"
}
}