cli.py 1.5 KB
Newer Older
1
2
3
import sys
from .models import Error
from .extensions import Senpy
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
from . import api
5

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
6

7
8
9
10
11
12
13
14
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('-')
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
15
            value = argv[i + 1] if len(argv) > i + 1 else None
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
            if not value or value[0] == '-':
                cli_dict[key] = True
18
19
20
21
            else:
                cli_dict[key] = value
    return cli_dict

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
22

23
24
25
def main_function(argv):
    '''This is the method for unit testing
    '''
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26
27
28
29
30
    params = api.parse_params(argv_to_dict(argv),
                              api.CLI_PARAMS,
                              api.API_PARAMS,
                              api.NIF_PARAMS)
    plugin_folder = params['plugin_folder']
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
31
32
    default_plugins = params.get('default-plugins', False)
    sp = Senpy(default_plugins=default_plugins, plugin_folder=plugin_folder)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
    request = api.parse_call(params)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
35
36
37
38
39
    algos = request.parameters.get('algorithm', None)
    if algos:
        for algo in algos:
            sp.activate_plugin(algo)
    else:
        sp.activate_all()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
    res = sp.analyse(request)
41
42
    return res

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
43

44
45
46
47
48
def main():
    '''This method is the entrypoint for the CLI (as configured un setup.py)
    '''
    try:
        res = main_function(sys.argv[1:])
49
        print(res.serialize())
50
    except Error as err:
51
        print(err.serialize())
52
        sys.exit(2)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
53

54
55
56

if __name__ == '__main__':
    main()