__main__.py 3.47 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
28
29
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop


30
31
32
import logging
import os
import argparse
33
import senpy
34

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
36
SERVER_PORT = os.environ.get("PORT", 5000)

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38
def main():
39
    parser = argparse.ArgumentParser(description='Run a Senpy server')
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    parser.add_argument(
        '--level',
        '-l',
        metavar='logging_level',
        type=str,
        default="INFO",
        help='Logging level')
    parser.add_argument(
        '--debug',
        '-d',
        action='store_true',
        default=False,
        help='Run the application in debug mode')
    parser.add_argument(
        '--default-plugins',
        action='store_true',
        default=False,
        help='Load the default plugins')
    parser.add_argument(
        '--host',
        type=str,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
        default="0.0.0.0",
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        help='Use 0.0.0.0 to accept requests from any host.')
    parser.add_argument(
        '--port',
        '-p',
        type=int,
        default=SERVER_PORT,
        help='Port to listen on.')
    parser.add_argument(
        '--plugins-folder',
        '-f',
        type=str,
        default='plugins',
        help='Where to look for plugins.')
    parser.add_argument(
        '--only-install',
        '-i',
        action='store_true',
        default=False,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
        help='Do not run a server, only install plugin dependencies')
81
82
83
84
85
86
    parser.add_argument(
        '--version',
        '-v',
        action='store_true',
        default=False,
        help='Output the senpy version and exit')
87
    args = parser.parse_args()
88
89
90
    if args.version:
        print('Senpy version {}'.format(senpy.__version__))
        exit(1)
91
92
93
    logging.basicConfig()
    rl = logging.getLogger()
    rl.setLevel(getattr(logging, args.level))
94
95
    app = Flask(__name__)
    app.debug = args.debug
96
    sp = Senpy(app, args.plugins_folder, default_plugins=args.default_plugins)
97
98
99
    if args.only_install:
        sp.install_deps()
        return
100
    sp.activate_all()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
101
102
103
104
    print('Senpy version {}'.format(senpy.__version__))
    print('Server running on port %s:%d. Ctrl+C to quit' % (args.host,
                                                            args.port))
    if not app.debug:
105
106
        http_server = HTTPServer(WSGIContainer(app))
        http_server.listen(args.port, address=args.host)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
107
        try:
108
            IOLoop.instance().start()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
109
110
111
112
113
114
115
        except KeyboardInterrupt:
            print('Bye!')
        http_server.stop()
    else:
        app.run(args.host,
                args.port,
                debug=True)
116
    sp.deactivate_all()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
117

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
119
120
if __name__ == '__main__':
    main()