1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 08:12:27 +00:00

Fix schema issues and parameter validation

This commit is contained in:
J. Fernando Sánchez 2018-05-14 11:38:02 +02:00
parent 73f7cbbe8a
commit d7acf3d67a
9 changed files with 49 additions and 21 deletions

View File

@ -6,8 +6,6 @@ RUN apt-get update && apt-get install -y \
libblas-dev liblapack-dev liblapacke-dev gfortran \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir --upgrade numpy scipy scikit-learn
RUN mkdir /cache/ /senpy-plugins /data/
VOLUME /data/

View File

@ -9,3 +9,6 @@ jsonref
PyYAML
rdflib
rdflib-jsonld
numpy
scipy
scikit-learn

View File

@ -147,7 +147,7 @@ def parse_params(indict, *specs):
for param, options in iteritems(spec):
for alias in options.get("aliases", []):
# Replace each alias with the correct name of the parameter
if alias in indict and alias is not param:
if alias in indict and alias != param:
outdict[param] = indict[alias]
del outdict[alias]
continue

View File

@ -19,7 +19,7 @@ Blueprints for Senpy
"""
from flask import (Blueprint, request, current_app, render_template, url_for,
jsonify)
from .models import Error, Response, Help, Plugins, read_schema, Datasets
from .models import Error, Response, Help, Plugins, read_schema, dump_schema, Datasets
from . import api
from .version import __version__
from functools import wraps
@ -67,9 +67,9 @@ def index():
@api_blueprint.route('/schemas/<schema>')
def schema(schema="definitions"):
try:
return jsonify(read_schema(schema))
except Exception: # Should be FileNotFoundError, but it's missing from py2
return Error(message="Schema not found", status=404).flask()
return dump_schema(read_schema(schema))
except Exception as ex: # Should be FileNotFoundError, but it's missing from py2
return Error(message="Schema not found: {}".format(ex), status=404).flask()
def basic_api(f):
@ -133,6 +133,7 @@ def api_root():
req = api.parse_call(request.parameters)
return current_app.senpy.analyse(req)
@api_blueprint.route('/evaluate/', methods=['POST', 'GET'])
@basic_api
def evaluate():
@ -145,6 +146,7 @@ def evaluate():
response = current_app.senpy.evaluate(params)
return response
@api_blueprint.route('/plugins/', methods=['POST', 'GET'])
@basic_api
def plugins():

View File

@ -51,6 +51,10 @@ def read_schema(schema_file, absolute=False):
return jsonref.load(f, base_uri=schema_uri)
def dump_schema(schema):
return jsonref.dumps(schema)
def load_context(context):
logging.debug('Loading context: {}'.format(context))
if not context:

View File

@ -19,8 +19,6 @@ import importlib
import yaml
import threading
import numpy as np
from .. import models, utils
from .. import api
@ -49,11 +47,11 @@ class PluginMeta(models.BaseMeta):
attrs['name'] = alias
if 'description' not in attrs:
doc = attrs.get('__doc__', None)
if not doc:
raise Exception(('Please, add a description or '
'documentation to class {}').format(name))
if doc:
attrs['description'] = doc
attrs['name'] = alias
else:
logger.warn(('Plugin {} does not have a description. '
'Please, add a short summary to help other developers').format(name))
cls = super(PluginMeta, mcs).__new__(mcs, name, bases, attrs)
if alias in mcs._classes:
@ -291,7 +289,7 @@ class Box(AnalysisPlugin):
return self
def transform(self, X):
return np.array([self.predict_one(x) for x in X])
return [self.predict_one(x) for x in X]
def predict(self, X):
return self.transform(X)

View File

@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"name": "Evalation",
"name": "Evaluation",
"properties": {
"@id": {
"type": "string"

View File

@ -32,7 +32,7 @@ class APITest(TestCase):
query = {}
plug_params = {
'hello': {
'aliases': ['hello', 'hiya'],
'aliases': ['hiya', 'hello'],
'required': True
}
}
@ -48,6 +48,26 @@ class APITest(TestCase):
assert 'hello' in p
assert p['hello'] == 'dlrow'
def test_parameters2(self):
in1 = {
'meaningcloud-key': 5
}
in2 = {
'apikey': 25
}
extra_params = {
"apikey": {
"aliases": [
"apikey",
"meaningcloud-key"
],
"required": True
}
}
p1 = parse_params(in1, extra_params)
p2 = parse_params(in2, extra_params)
assert (p2['apikey'] / p1['apikey']) == 5
def test_default(self):
spec = {
'hello': {

View File

@ -8,6 +8,8 @@ from fnmatch import fnmatch
from jsonschema import RefResolver, Draft4Validator, ValidationError
from senpy.models import read_schema
root_path = path.join(path.dirname(path.realpath(__file__)), '..')
schema_folder = path.join(root_path, 'senpy', 'schemas')
examples_path = path.join(root_path, 'docs', 'examples')
@ -15,7 +17,8 @@ bad_examples_path = path.join(root_path, 'docs', 'bad-examples')
class JSONSchemaTests(unittest.TestCase):
pass
def test_definitions(self):
read_schema('definitions.json')
def do_create_(jsfile, success):