__main__.py 2.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python
# -*- coding: utf-8 -*-
#    Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
#                                                       DIT, UPM
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
"""
Senpy is a modular sentiment analysis server. This script runs an instance of
the server.

"""
22

23
from flask import Flask
24
from senpy.extensions import Senpy
25
26
27
from gevent.wsgi import WSGIServer
from gevent.monkey import patch_all
import gevent
28
29
30
31
import logging
import os
import argparse

32
33
patch_all(thread=False)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
def main():
35
36
37
38
39
40
41
42
43
    parser = argparse.ArgumentParser(description='Run a Senpy server')
    parser.add_argument('--level',
                        "-l",
                        metavar="logging_level",
                        type=str,
                        default="INFO",
                        help='Logging level')
    parser.add_argument('--debug',
                        "-d",
44
                        action='store_true',
45
46
                        default=False,
                        help='Run the application in debug mode')
47
48
49
    parser.add_argument('--default-plugins',
                        action='store_true',
                        default=False,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
50
                        help='Load the default plugins')
51
52
    parser.add_argument('--host',
                        type=str,
53
                        default="127.0.0.1",
54
55
56
57
                        help='Use 0.0.0.0 to accept requests from any host.')
    parser.add_argument('--port',
                        '-p',
                        type=int,
58
                        default=5000,
59
60
61
62
                        help='Port to listen on.')
    parser.add_argument('--plugins-folder',
                        '-f',
                        type=str,
63
                        default="plugins",
64
65
                        help='Where to look for plugins.')
    args = parser.parse_args()
66
    logging.basicConfig(level=getattr(logging, args.level))
67
68
    app = Flask(__name__)
    app.debug = args.debug
69
    sp = Senpy(app, args.plugins_folder, default_plugins=args.default_plugins)
70
71
72
    sp.activate_all()
    http_server = WSGIServer((args.host, args.port), app)
    try:
73
74
        print("Server running on port %s:%d. Ctrl+C to quit" % (args.host,
                                                                args.port))
75
76
77
        http_server.serve_forever()
    except KeyboardInterrupt:
        http_server.stop()
78
        print("Bye!")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
80
81

if __name__ == '__main__':
    main()