From aaad5e8f2b5d944ba3662d3295b2bd06db6f9ae3 Mon Sep 17 00:00:00 2001 From: militarpancho Date: Thu, 13 Jul 2017 13:35:56 +0200 Subject: [PATCH 1/6] Add 'help' as a parameter to the API, with two options: "true" and "false". It is false by default. Modify all the /api blueprint to export the list of available params --- senpy/api.py | 8 ++++++++ senpy/blueprints.py | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/senpy/api.py b/senpy/api.py index 886901b..14ad069 100644 --- a/senpy/api.py +++ b/senpy/api.py @@ -39,6 +39,14 @@ API_PARAMS = { "required": True, "options": ["filtered", "nested", "full"], "default": "full" + }, + "help": { + "@id": "help", + "description": "Show additional help to know more about the possible parameters", + "aliases": ["help", "h"], + "required": True, + "options": ["True", "False"], + "default": "False" } } diff --git a/senpy/blueprints.py b/senpy/blueprints.py index 4dcc9ce..26c91a9 100644 --- a/senpy/blueprints.py +++ b/senpy/blueprints.py @@ -20,7 +20,7 @@ Blueprints for Senpy from flask import (Blueprint, request, current_app, render_template, url_for, jsonify) from .models import Error, Response, Plugins, read_schema -from .api import WEB_PARAMS, API_PARAMS, parse_params +from .api import WEB_PARAMS, API_PARAMS, CLI_PARAMS, NIF_PARAMS, parse_params from .version import __version__ from functools import wraps @@ -144,3 +144,21 @@ def plugin(plugin=None): else: return Error(message="Plugin not found", status=404) return response + +@api_blueprint.route('/params/', methods=['POST', 'GET']) +@basic_api +def params(): + phelp = request.params.get('help') + if eval(phelp): + dic = {'WEB_PARAMS': WEB_PARAMS, + 'CLI_PARAMS': CLI_PARAMS, + 'NIF_PARAMS': NIF_PARAMS, + 'API_PARAMS': API_PARAMS + } + response = Response(dic) + return response + else: + response = Response(request.params) + return response + + From 7f44f9e85d55120a784f976cc7a4e64bcb289eab Mon Sep 17 00:00:00 2001 From: militarpancho Date: Fri, 14 Jul 2017 10:38:18 +0200 Subject: [PATCH 2/6] Fix changes in blueprints api for showing help --- senpy/blueprints.py | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/senpy/blueprints.py b/senpy/blueprints.py index 26c91a9..9144d4c 100644 --- a/senpy/blueprints.py +++ b/senpy/blueprints.py @@ -43,7 +43,6 @@ def get_params(req): raise Error(message="Invalid data") return indict - @demo_blueprint.route('/') def index(): return render_template("index.html", version=__version__) @@ -117,8 +116,18 @@ def basic_api(f): @api_blueprint.route('/', methods=['POST', 'GET']) @basic_api def api(): - response = current_app.senpy.analyse(**request.params) - return response + phelp = request.params.get('help') + if phelp == "True": + dic = {'WEB_PARAMS': WEB_PARAMS, + 'CLI_PARAMS': CLI_PARAMS, + 'NIF_PARAMS': NIF_PARAMS, + 'API_PARAMS': API_PARAMS + } + response = Response(dic) + return response + else: + response = current_app.senpy.analyse(**request.params) + return response @api_blueprint.route('/plugins/', methods=['POST', 'GET']) @@ -144,21 +153,3 @@ def plugin(plugin=None): else: return Error(message="Plugin not found", status=404) return response - -@api_blueprint.route('/params/', methods=['POST', 'GET']) -@basic_api -def params(): - phelp = request.params.get('help') - if eval(phelp): - dic = {'WEB_PARAMS': WEB_PARAMS, - 'CLI_PARAMS': CLI_PARAMS, - 'NIF_PARAMS': NIF_PARAMS, - 'API_PARAMS': API_PARAMS - } - response = Response(dic) - return response - else: - response = Response(request.params) - return response - - From 7547fc49af0e339b1f6468da077d477f253bcfee Mon Sep 17 00:00:00 2001 From: militarpancho Date: Fri, 14 Jul 2017 11:51:58 +0200 Subject: [PATCH 3/6] Change params dict structure. #42 --- senpy/blueprints.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/senpy/blueprints.py b/senpy/blueprints.py index 9144d4c..3462b84 100644 --- a/senpy/blueprints.py +++ b/senpy/blueprints.py @@ -118,11 +118,7 @@ def basic_api(f): def api(): phelp = request.params.get('help') if phelp == "True": - dic = {'WEB_PARAMS': WEB_PARAMS, - 'CLI_PARAMS': CLI_PARAMS, - 'NIF_PARAMS': NIF_PARAMS, - 'API_PARAMS': API_PARAMS - } + dic = dict(API_PARAMS, **NIF_PARAMS) response = Response(dic) return response else: From 7fd16a17fb1019684bd6caac9f2019c4ac5db853 Mon Sep 17 00:00:00 2001 From: militarpancho Date: Fri, 14 Jul 2017 14:56:06 +0200 Subject: [PATCH 4/6] Use the exported values to generate the form in the playground Issue #42 --- senpy/static/js/main.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/senpy/static/js/main.js b/senpy/static/js/main.js index cf9f2f1..0ced642 100644 --- a/senpy/static/js/main.js +++ b/senpy/static/js/main.js @@ -1,7 +1,7 @@ var ONYX = "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"; var RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; var plugins_params={}; - +var default_params = JSON.parse($.ajax({type: "GET", url: "/api?help=True" , async: false}).responseText); function replaceURLWithHTMLLinks(text) { console.log('Text: ' + text); var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; @@ -28,7 +28,7 @@ function hashchanged(){ $(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/plugins/default" , async: false}).responseText); - html=""; + html=""; var availablePlugins = document.getElementById('availablePlugins'); plugins = response.plugins; for (r in plugins){ @@ -88,8 +88,23 @@ $(document).ready(function() { function change_params(){ var plugin = document.getElementById("plugins").options[document.getElementById("plugins").selectedIndex].value; - html="" + for (param in default_params){ + if ((default_params[param]['options']) && (['help','conversion'].indexOf(param) < 0)){ + html+= "" + html+= "
" + } + } for (param in plugins_params[plugin]){ if (param || plugins_params[plugin][param].length > 1){ html+= "" @@ -120,6 +135,16 @@ function load_JSON(){ } } } + + for (param in default_params){ + if ((param != null) && (default_params[param]['options']) && (['help','conversion'].indexOf(param) < 0)){ + var param_value = encodeURIComponent(document.getElementById(param).options[document.getElementById(param).selectedIndex].text); + if (param_value){ + url+="&"+param+"="+param_value + } + } + } + var response = JSON.parse($.ajax({type: "GET", url: url , async: false}).responseText); var options = { mode: 'view' From ad2051307a784a313663d2f113217dd3b62e6060 Mon Sep 17 00:00:00 2001 From: militarpancho Date: Mon, 17 Jul 2017 14:28:31 +0200 Subject: [PATCH 5/6] Grouped plugins by plugin Type with and optgroup. This ought to close #42 --- senpy/static/js/main.js | 104 +++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/senpy/static/js/main.js b/senpy/static/js/main.js index 0ced642..e676504 100644 --- a/senpy/static/js/main.js +++ b/senpy/static/js/main.js @@ -31,51 +31,65 @@ $(document).ready(function() { html=""; var availablePlugins = document.getElementById('availablePlugins'); plugins = response.plugins; - for (r in plugins){ - plugin = plugins[r] - if (plugin["name"]){ - if (plugin["name"] == defaultPlugin["name"]){ - if (plugin["is_activated"]){ - html+= "" - }else{ - html+= "" - } - } - else{ - if (plugin["is_activated"]){ - html+= "" - } - else{ - html+= "" - } - } - } - if (plugin["extra_params"]){ - plugins_params[plugin["name"]]={}; - for (param in plugin["extra_params"]){ - if (typeof plugin["extra_params"][param] !="string"){ - var params = new Array(); - var alias = plugin["extra_params"][param]["aliases"][0]; - params[alias]=new Array(); - for (option in plugin["extra_params"][param]["options"]){ - params[alias].push(plugin["extra_params"][param]["options"][option]) - } - plugins_params[plugin["name"]][alias] = (params[alias]) - } - } - } - var pluginList = document.createElement('li'); - - newHtml = "" - if(plugin.url) { - newHtml= "" + plugin.name + ""; - }else { - newHtml= plugin["name"]; - } - newHtml += ": " + replaceURLWithHTMLLinks(plugin.description); - pluginList.innerHTML = newHtml; - availablePlugins.appendChild(pluginList) - } + gplugins = {}; + for (r in plugins){ + ptype = plugins[r]['@type']; + if(gplugins[ptype] == undefined){ + gplugins[ptype] = [r] + }else{ + gplugins[ptype].push(r) + } + } + for (g in gplugins){ + html += "" + for (r in gplugins[g]){ + plugin = plugins[r] + if (plugin["name"]){ + if (plugin["name"] == defaultPlugin["name"]){ + if (plugin["is_activated"]){ + html+= "" + }else{ + html+= "" + } + } + else{ + if (plugin["is_activated"]){ + html+= "" + } + else{ + html+= "" + } + } + } + + if (plugin["extra_params"]){ + plugins_params[plugin["name"]]={}; + for (param in plugin["extra_params"]){ + if (typeof plugin["extra_params"][param] !="string"){ + var params = new Array(); + var alias = plugin["extra_params"][param]["aliases"][0]; + params[alias]=new Array(); + for (option in plugin["extra_params"][param]["options"]){ + params[alias].push(plugin["extra_params"][param]["options"][option]) + } + plugins_params[plugin["name"]][alias] = (params[alias]) + } + } + } + var pluginList = document.createElement('li'); + + newHtml = "" + if(plugin.url) { + newHtml= "" + plugin.name + ""; + }else { + newHtml= plugin["name"]; + } + newHtml += ": " + replaceURLWithHTMLLinks(plugin.description); + pluginList.innerHTML = newHtml; + availablePlugins.appendChild(pluginList) + } + html += "" + } document.getElementById('plugins').innerHTML = html; change_params(); From 8c5f894843609918bad9cc3cd7495cd739f3eb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 18 Jul 2017 18:25:00 +0200 Subject: [PATCH 6/6] Fixed turtle outformat in playground --- senpy/static/js/main.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/senpy/static/js/main.js b/senpy/static/js/main.js index e676504..330dc95 100644 --- a/senpy/static/js/main.js +++ b/senpy/static/js/main.js @@ -159,15 +159,22 @@ function load_JSON(){ } } - var response = JSON.parse($.ajax({type: "GET", url: url , async: false}).responseText); + var response = $.ajax({type: "GET", url: url , async: false}).responseText; + rawcontainer.innerHTML = replaceURLWithHTMLLinks(response) + + document.getElementById("input_request").innerHTML = ""+url+"" + document.getElementById("results-div").style.display = 'block'; + try { + response = JSON.parse(response); var options = { mode: 'view' }; var editor = new JSONEditor(container, options, response); editor.expandAll(); - rawcontainer.innerHTML = replaceURLWithHTMLLinks(JSON.stringify(response, undefined, 2)) - document.getElementById("input_request").innerHTML = ""+url+"" - document.getElementById("results-div").style.display = 'block'; + } + catch(err){ + console.log("Error decoding JSON (got turtle?)"); + } }