Usage examples

Creating new problem

import problem

prob = problem.Runtime(
        reason='egg_error_message: assertion "error" failed',
    )

prob.add_current_process_data()
prob.add_current_environment()
prob.save()

Creating problem for different executable

import problem

prob = problem.Selinux(reason='Front fell off')

prob.executable = '/usr/bin/time'

prob.save()

Adding custom data

import problem

prob = problem.Runtime(
        reason='Error getting devices:'
        'GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: '
        'No such interface `org.gnome.SettingsDaemon.Power` on object at path '
        '/org/gnome/SettingsDaemon/Power'
    )

prob.add_current_process_data()
prob.custom_data = 'any'
prob['dict_access_example'] = 'works'

print(prob)
print('')

for key, value in prob.items():
    print('{0}={1}'.format(key, value))

print('Identifier:', prob.save())

Querying problems

import problem

for prob in problem.list():
    print(prob)
    print(repr(prob.time))
    if hasattr(prob, 'pid'):
        print(prob.pid)

Querying all problems

The list method used with auth=True parameter will try to authenticate via polkit to gain access to all problems on the system.

If there is no authentication agent running or authentication is unsuccessful, the list of problems which belong to current user is returned (same as returned by the list method).

import problem

for prob in problem.list(auth=True):
    print(prob)
    if hasattr(prob, 'username'):
        print('Problem belongs to {0}'.format(prob.username))

Editing existing problems

import problem

for prob in problem.list():
    if prob.type == problem.JAVA:
        prob.delete()

    if prob.type == problem.CCPP:
        if 'password' in prob.backtrace:
            del prob.backtrace
            prob.save()

    if prob.type == problem.KERNELOOPS:
        prob.backtrace = prob.backtrace.replace(' ?', '')
        prob.save()

Watching for new problems

import problem
import logging

logging.basicConfig(level=logging.DEBUG)

def monitor(prob):
    print(prob)
    prob.delete()

pwatch = problem.get_problem_watcher()
pwatch.add_callback(monitor)

try:
    pwatch.run()
except KeyboardInterrupt:
    pwatch.quit()

Watching for new problems in a thread

from __future__ import print_function

import sys
import time
import problem
import threading

class ProblemWatchThread(threading.Thread):
    def __init__(self):
        super(ProblemWatchThread, self).__init__()
        self.pwatch = problem.get_problem_watcher()
        self.pwatch.add_callback(self.handle)
        self.probcount = 0

    def handle(self, prob):
        self.probcount += 1
        print('{0}: {1}'.format(self.probcount, prob))
        # prob.delete()

    def run(self):
        self.pwatch.run()

    def stop(self):
        self.pwatch.quit()

pwt = ProblemWatchThread()
pwt.start()

i = 0
print('Waiting for new problem to appear')
spinner = ['\\', '|', '/', '-']

try:
    while True:
        time.sleep(0.1)
        print('{0}\r'.format(spinner[i]), end='')
        i += 1
        i = i % len(spinner)
        sys.stdout.flush()
except KeyboardInterrupt:
    pwt.stop()

pwt.stop()

Getting bug numbers of problems reported to bugzilla

import problem

bugs = set()

for prob in problem.list():
    if not hasattr(prob, 'reported_to'):
        continue

    for line in prob.reported_to.splitlines():
        if line.startswith('Bugzilla:'):
            bug_num = int(line.split('=')[-1])
            bugs.add(bug_num)

print(bugs)