summaryrefslogtreecommitdiff
path: root/service/pixelated/config/__init__.py
blob: af264c7714c90b2a2d36acdea012551dbe99c969 (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
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

from functools import partial
import sys

from pixelated.config.app import App
from pixelated.config import app_factory
from pixelated.config.args import parse as parse_args
from pixelated.config.config_ua import config_user_agent
from pixelated.config.dispatcher import config_dispatcher
from pixelated.config.events_server import init_events_server
from pixelated.config.loading_page import loading
from pixelated.config.register import register
from pixelated.config.logging_setup import init_logging
from pixelated.config.leap_cert import init_leap_cert
from pixelated.config.soledad import init_soledad_and_user_key
from twisted.internet import reactor
from twisted.internet.threads import deferToThread

# monkey patching some specifics
import pixelated.support.ext_protobuf
import pixelated.support.ext_sqlcipher
import pixelated.support.ext_esmtp_sender_factory
import pixelated.support.ext_fetch
import pixelated.support.ext_keymanager_fetch_key
import pixelated.support.ext_requests_urllib3


def initialize():
    args = parse_args()
    app = App()

    init_logging(args)
    init_leap_cert(args)

    if args.register:
        register(*args.register[::-1])
        sys.exit(0)

    if args.dispatcher or args.dispatcher_stdin:
        config_dispatcher(app, args)
    else:
        config_user_agent(app, args)

    init_events_server()

    def load_app():
        # welcome to deferred hell. Or maybe you'll be welcomed later, who knows.
        loading_app = loading(args)

        def init_soledad():
            return init_soledad_and_user_key(app, args.home)

        def stop_loading_app(leap_session):
            d = loading_app.stopListening()
            d.addCallback(partial(start_user_agent_app, leap_session))

        def start_user_agent_app(leap_session, _):
            app_factory.create_app(app, args, leap_session)

        d = deferToThread(init_soledad)
        d.addCallback(stop_loading_app)

    reactor.callWhenRunning(load_app)
    reactor.run()