Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/runserver.py
blob: 7fc926a76452699f8a8f40383e1b4e2bec4f5c84 (plain)
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
#!/usr/bin/env python

# Copyright (C) 2012, Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import optparse
from os.path import dirname, join, realpath
from gettext import gettext as _

from gevent.wsgi import WSGIServer
from sugar_network import client 
from sugar_network.toolkit.http import Connection
from sugar_network.toolkit import application,Option
from sugar_network_webui import get_app

SRC_ROOT = dirname(realpath(__file__))


host = optparse.Option("-h", "--host", type="string",
        help=_('address to listen for Web clients'),
        default='127.0.0.1')

port = optparse.Option("-p", "--port", type="int",
        help=_('address to listen for Web clients'),
        default=5000)

api_url = optparse.Option("-a", "--api_url", type="string",
        help=_('address of node server to connect to'),
        default='http://node-devel.sugarlabs.org/')

anonymous = optparse.Option("--anonymous", action="store_true",
        help=_('connect as anonymous user'),
        default=False)

class Application(application.Daemon):

    httpd = None

    def start(self):
        application.logdir.value = client.profile_path('logs')
        application.rundir.value = client.profile_path('run')
        application.Daemon.start(self)

    def run(self):
        app = self._get_app()
        self.httpd = WSGIServer((host.default, port.default), app)
        self.httpd.serve_forever()

    def shutdown(self):
        if self.httpd is not None:
            self.httpd.stop()

    @application.command(_('Run application in debug mode'))
    def debug(self):
        app = self._get_app()
        app.debug = True
        app.run(host=host.default, port=port.default)

    def _get_app(self):
        self.connection = Connection(api_url)
        return get_app(self.connection.call,
                        api_url, anonymous)


Option.seek('mejorar-sistema')
Option.seek('mejorar-sistema',
       [application.debug, application.foreground])

app = Application(
        name='mejorar-sistema',
        description=_('Flask server to serve Sugar Network users'),
        epilog=_('See http://wiki.sugarlabs.org/go/Platform_Team/' \
                 'Sugar_Network/Browser.'),
        config_files=[
            '/etc/sweets.conf',
            '~/.config/sweets/config',
            client.profile_path('sweets.conf'),
            ])
app.start()