mirror of https://github.com/gsi-upm/senpy
Added CLI and refactored argument parsing
parent
a50f026701
commit
03eb38c12d
@ -0,0 +1,105 @@
|
|||||||
|
from future.utils import iteritems
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
from .models import Error
|
||||||
|
|
||||||
|
API_PARAMS = {
|
||||||
|
"algorithm": {
|
||||||
|
"aliases": ["algorithm", "a", "algo"],
|
||||||
|
"required": False,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WEB_PARAMS = {
|
||||||
|
"inHeaders": {
|
||||||
|
"aliases": ["inHeaders", "headers"],
|
||||||
|
"required": True,
|
||||||
|
"default": "0"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
CLI_PARAMS = {
|
||||||
|
"plugin_folder": {
|
||||||
|
"aliases": ["plugin_folder", "folder"],
|
||||||
|
"required": True,
|
||||||
|
"default": "."
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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"],
|
||||||
|
},
|
||||||
|
"outformat": {
|
||||||
|
"@id": "outformat",
|
||||||
|
"aliases": ["outformat", "o"],
|
||||||
|
"default": "json-ld",
|
||||||
|
"required": False,
|
||||||
|
"options": ["json-ld"],
|
||||||
|
},
|
||||||
|
"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):
|
||||||
|
outdict = {}
|
||||||
|
wrong_params = {}
|
||||||
|
for param, options in iteritems(spec):
|
||||||
|
if param[0] != "@": # Exclude json-ld properties
|
||||||
|
logger.debug("Param: %s - Options: %s", param, options)
|
||||||
|
for alias in options["aliases"]:
|
||||||
|
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:
|
||||||
|
message = Error(status=404,
|
||||||
|
message="Missing or invalid parameters",
|
||||||
|
parameters=outdict,
|
||||||
|
errors={param: error for param, error in
|
||||||
|
iteritems(wrong_params)})
|
||||||
|
raise message
|
||||||
|
return outdict
|
@ -0,0 +1,50 @@
|
|||||||
|
import sys
|
||||||
|
from .models import Error
|
||||||
|
from .api import parse_params, CLI_PARAMS
|
||||||
|
from .extensions import Senpy
|
||||||
|
|
||||||
|
def argv_to_dict(argv):
|
||||||
|
'''Turns parameters in the form of '--key value' into a dict {'key': 'value'}
|
||||||
|
'''
|
||||||
|
cli_dict = {}
|
||||||
|
|
||||||
|
for i in range(len(argv)):
|
||||||
|
if argv[i][0] == '-':
|
||||||
|
key = argv[i].strip('-')
|
||||||
|
value = argv[i+1] if len(argv)>i+1 else None
|
||||||
|
if value and value[0] == '-':
|
||||||
|
cli_dict[key] = ""
|
||||||
|
else:
|
||||||
|
cli_dict[key] = value
|
||||||
|
return cli_dict
|
||||||
|
|
||||||
|
def parse_cli(argv):
|
||||||
|
cli_dict = argv_to_dict(argv)
|
||||||
|
cli_params = parse_params(cli_dict, spec=CLI_PARAMS)
|
||||||
|
return cli_params, cli_dict
|
||||||
|
|
||||||
|
|
||||||
|
def main_function(argv):
|
||||||
|
'''This is the method for unit testing
|
||||||
|
'''
|
||||||
|
cli_params, cli_dict = parse_cli(argv)
|
||||||
|
plugin_folder = cli_params['plugin_folder']
|
||||||
|
sp = Senpy(default_plugins=False, plugin_folder=plugin_folder)
|
||||||
|
sp.activate_all(sync=True)
|
||||||
|
res = sp.analyse(**cli_dict)
|
||||||
|
return res
|
||||||
|
|
||||||
|
def main():
|
||||||
|
'''This method is the entrypoint for the CLI (as configured un setup.py)
|
||||||
|
'''
|
||||||
|
try:
|
||||||
|
res = main_function(sys.argv[1:])
|
||||||
|
print(res.to_JSON())
|
||||||
|
except Error as err:
|
||||||
|
print(err.to_JSON())
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -0,0 +1,21 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
from unittest import TestCase
|
||||||
|
from senpy.cli import main_function
|
||||||
|
from senpy.models import Error
|
||||||
|
|
||||||
|
|
||||||
|
class CLITest(TestCase):
|
||||||
|
|
||||||
|
def test_basic(self):
|
||||||
|
self.assertRaises(Error, partial(main_function, []))
|
||||||
|
res = main_function(['--input', 'test'])
|
||||||
|
assert 'entries' in res
|
||||||
|
res = main_function(['--input', 'test', '--algo', 'rand'])
|
||||||
|
assert 'entries' in res
|
||||||
|
assert 'analysis' in res
|
||||||
|
assert res['analysis'][0]['name'] == 'rand'
|
Loading…
Reference in New Issue