#!/usr/bin/env python # # Author: Sascha Silbe # # 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 . """ 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.DEBUG, 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_v2 = datastore.DBusApiSugarV2(internal_api) dbus_api_native_v1 = datastore.DBusApiNativeV1(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()