api.py 3.84 KB
Newer Older
1
from future.utils import iteritems
2
from .models import Error
3 4 5 6 7 8 9
import logging
logger = logging.getLogger(__name__)

API_PARAMS = {
    "algorithm": {
        "aliases": ["algorithm", "a", "algo"],
        "required": False,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    },
    "outformat": {
        "@id": "outformat",
        "aliases": ["outformat", "o"],
        "default": "json-ld",
        "required": True,
        "options": ["json-ld", "turtle"],
    },
    "expanded-jsonld": {
        "@id": "expanded-jsonld",
        "aliases": ["expanded", "expanded-jsonld"],
        "required": True,
        "default": 0
    },
    "emotionModel": {
        "@id": "emotionModel",
        "aliases": ["emotionModel", "emoModel"],
        "required": False
    },
29 30 31 32 33 34 35
    "plugin_type": {
        "@id": "pluginType",
        "description": 'What kind of plugins to list',
        "aliases": ["pluginType", "plugin_type"],
        "required": True,
        "default": "analysisPlugin"
    },
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36 37 38 39 40 41
    "conversion": {
        "@id": "conversion",
        "description": "How to show the elements that have (not) been converted",
        "required": True,
        "options": ["filtered", "nested", "full"],
        "default": "full"
42 43 44 45 46 47 48 49
    },
    "help": {
        "@id": "help",
        "description": "Show additional help to know more about the possible parameters",
        "aliases": ["help", "h"],
        "required": True,
        "options": ["True", "False"],
        "default": "False"
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    }
}

WEB_PARAMS = {
    "inHeaders": {
        "aliases": ["inHeaders", "headers"],
        "required": True,
        "default": "0"
    },
}

CLI_PARAMS = {
    "plugin_folder": {
        "aliases": ["plugin_folder", "folder"],
        "required": True,
        "default": "."
    },
J. Fernando Sánchez's avatar
YAPFed  
J. Fernando Sánchez committed
67
}
68 69 70 71 72 73 74 75 76 77 78 79 80

NIF_PARAMS = {
    "input": {
        "@id": "input",
        "aliases": ["i", "input"],
        "required": True,
        "help": "Input text"
    },
    "informat": {
        "@id": "informat",
        "aliases": ["f", "informat"],
        "required": False,
        "default": "text",
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
81
        "options": ["turtle", "text", "json-ld"],
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    },
    "intype": {
        "@id": "intype",
        "aliases": ["intype", "t"],
        "required": False,
        "default": "direct",
        "options": ["direct", "url", "file"],
    },
    "language": {
        "@id": "language",
        "aliases": ["language", "l"],
        "required": False,
    },
    "prefix": {
        "@id": "prefix",
        "aliases": ["prefix", "p"],
        "required": True,
        "default": "",
    },
    "urischeme": {
        "@id": "urischeme",
        "aliases": ["urischeme", "u"],
        "required": False,
        "default": "RFC5147String",
        "options": "RFC5147String"
    },
}


def parse_params(indict, spec=NIF_PARAMS):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
112 113
    logger.debug("Parsing: {}\n{}".format(indict, spec))
    outdict = indict.copy()
114 115 116
    wrong_params = {}
    for param, options in iteritems(spec):
        if param[0] != "@":  # Exclude json-ld properties
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
117
            for alias in options.get("aliases", []):
118 119 120 121 122 123 124 125 126 127 128 129 130
                if alias in indict:
                    outdict[param] = indict[alias]
            if param not in outdict:
                if options.get("required", False) and "default" not in options:
                    wrong_params[param] = spec[param]
                else:
                    if "default" in options:
                        outdict[param] = options["default"]
            else:
                if "options" in spec[param] and \
                   outdict[param] not in spec[param]["options"]:
                    wrong_params[param] = spec[param]
    if wrong_params:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
131
        logger.debug("Error parsing: %s", wrong_params)
J. Fernando Sánchez's avatar
YAPFed  
J. Fernando Sánchez committed
132
        message = Error(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
            status=400,
J. Fernando Sánchez's avatar
YAPFed  
J. Fernando Sánchez committed
134 135 136 137
            message="Missing or invalid parameters",
            parameters=outdict,
            errors={param: error
                    for param, error in iteritems(wrong_params)})
138 139
        raise message
    return outdict