Blame src/python-problem/problem/watch.py

Packit 8ea169
import os
Packit 8ea169
import logging
Packit 8ea169
Packit 8ea169
import problem
Packit 8ea169
Packit 8ea169
Packit 8ea169
class ProblemWatcher(object):
Packit 8ea169
    ''' New problem signal handler attached to DBus signal
Packit 8ea169
Packit 8ea169
    Use ``auth=True`` if authentication should be attempted for
Packit 8ea169
    new problem that doesn't belong to current user. If not
Packit 8ea169
    set such a problem is ignored.
Packit 8ea169
Packit 8ea169
    '''
Packit 8ea169
Packit 8ea169
    def __init__(self, auth):
Packit 8ea169
        import dbus
Packit 8ea169
        from gi.repository import GObject as gobject
Packit 8ea169
        from dbus.mainloop.glib import DBusGMainLoop
Packit 8ea169
Packit 8ea169
        gobject.threads_init()
Packit 8ea169
Packit 8ea169
        bus = dbus.SystemBus(
Packit 8ea169
            mainloop=DBusGMainLoop(),
Packit 8ea169
            private=True)
Packit 8ea169
Packit 8ea169
        self.bus = bus
Packit 8ea169
        self.auth = auth
Packit 8ea169
        self.callbacks = []
Packit 8ea169
Packit 8ea169
        # local context required!?
Packit 8ea169
        # http://rmarko.fedorapeople.org/random/high_five.jpg
Packit 8ea169
        evt_match = self.bus.add_signal_receiver(
Packit 8ea169
            self._new_problem_handler,
Packit 8ea169
            signal_name='Crash', path='/org/freedesktop/problems')
Packit 8ea169
Packit 8ea169
        # add second listener for the old path
Packit 8ea169
        evt_match_old_path = self.bus.add_signal_receiver(
Packit 8ea169
            self._new_problem_handler,
Packit 8ea169
            signal_name='Crash', path='/com/redhat/abrt')
Packit 8ea169
Packit 8ea169
        self.loop = gobject.MainLoop()
Packit 8ea169
Packit 8ea169
    def _new_problem_handler(self, comp, ddir, uid, uuid, duphash):
Packit 8ea169
        logging.debug('New problem notification received')
Packit 8ea169
        if int(uid) != os.getuid() and not self.auth:
Packit 8ea169
            logging.debug('Auth disabled, ignoring crash with'
Packit 8ea169
                          ' uid: {0}'.format(uid))
Packit 8ea169
            return
Packit 8ea169
Packit 8ea169
        prob = problem.tools.problemify(ddir, problem.proxies.get_proxy())
Packit 8ea169
        for cb in self.callbacks:
Packit 8ea169
            cb(prob)
Packit 8ea169
Packit 8ea169
    def add_callback(self, fun):
Packit 8ea169
        ''' Add callback to be called when new problem occurs.
Packit 8ea169
Packit 8ea169
        Each callback function receives ``Problem`` instance
Packit 8ea169
Packit 8ea169
        '''
Packit 8ea169
Packit 8ea169
        self.callbacks.append(fun)
Packit 8ea169
Packit 8ea169
    def run(self):
Packit 8ea169
        ''' Start event listener loop '''
Packit 8ea169
Packit 8ea169
        self.loop.run()
Packit 8ea169
Packit 8ea169
    def quit(self):
Packit 8ea169
        ''' Stop event listener loop '''
Packit 8ea169
Packit 8ea169
        self.loop.quit()