api.py 3.36 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 29 30 31 32 33 34
    },
    "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
    },
    "conversion": {
        "@id": "conversion",
        "description": "How to show the elements that have (not) been converted",
        "required": True,
        "options": ["filtered", "nested", "full"],
        "default": "full"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    }
}

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
52
}
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

NIF_PARAMS = {
    "input": {
        "@id": "input",
        "aliases": ["i", "input"],
        "required": True,
        "help": "Input text"
    },
    "informat": {
        "@id": "informat",
        "aliases": ["f", "informat"],
        "required": False,
        "default": "text",
        "options": ["turtle", "text"],
    },
    "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
97 98
    logger.debug("Parsing: {}\n{}".format(indict, spec))
    outdict = indict.copy()
99 100 101
    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
102
            for alias in options.get("aliases", []):
103 104 105 106 107 108 109 110 111 112 113 114 115
                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
116
        logger.debug("Error parsing: %s", wrong_params)
J. Fernando Sánchez's avatar
YAPFed  
J. Fernando Sánchez committed
117
        message = Error(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
118
            status=400,
J. Fernando Sánchez's avatar
YAPFed  
J. Fernando Sánchez committed
119 120 121 122
            message="Missing or invalid parameters",
            parameters=outdict,
            errors={param: error
                    for param, error in iteritems(wrong_params)})
123 124
        raise message
    return outdict