Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/datastore-service
blob: c18d141eed2916b935a6e3ec9066e1a807d90414 (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
#!/usr/bin/env python
#
# Author: Sascha Silbe <sascha-pgp@silbe.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# 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/>.
"""
Gdatastore DBus service main loop
"""

import logging
import os
import signal
import time

import dbus.service
import dbus.mainloop.glib
import dbus.glib
import gobject

from gdatastore import datastore


BASE_DIR = os.path.join(os.path.expanduser('~/.gdatastore'))
LOG_DIR = os.path.join(BASE_DIR, 'logs')


def handle_disconnect(mainloop):
    mainloop.quit()
    logging.debug('DBus disconnect')


def handle_signal(signal_number, mainloop):
    logging.warning('Signal %d received, shutting down.', signal_number)
    mainloop.quit()


def main():
    if not os.path.exists(LOG_DIR):
        os.makedirs(LOG_DIR)

    logging.basicConfig(level=logging.WARN,
        format='%(asctime)-15s %(name)s %(levelname)s: %(message)s',
        filename='%s/%d.log' % (LOG_DIR, time.time()))

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    internal_api = datastore.InternalApi(BASE_DIR)
    dbus_api_sugar_v1 = datastore.DBusApiSugarV1(internal_api)
    mainloop = gobject.MainLoop()

    bus.set_exit_on_disconnect(False)
    bus.add_signal_receiver(
        lambda mainloop=mainloop: handle_disconnect(mainloop),
        signal_name='Disconnected',
        dbus_interface='org.freedesktop.DBus.Local')

    signal.signal(signal.SIGHUP, lambda signal_number, frame_,
        mainloop=mainloop: handle_signal(signal_number, mainloop))
    signal.signal(signal.SIGTERM, lambda signal_number, frame_,
        mainloop=mainloop: handle_signal(signal_number, mainloop))

    # pylint: disable-msg=W0702
    try:
        mainloop.run()
    except:
        logging.exception('Data store shut down with error')

    internal_api.stop()


main()