Refactored the way dbus is imported.

This commit is contained in:
Alex Dedul 2007-08-13 11:10:15 +00:00
parent 3edd3280ae
commit 72fa2a44a5
3 changed files with 38 additions and 70 deletions

View File

@ -39,22 +39,10 @@ from optparse import OptionParser
import re import re
import sys import sys
try:
import dbus
dbus_version = getattr(dbus, 'version', (0,0,0))
if dbus_version >= (0,41,0) and dbus_version < (0,80,0):
import dbus.glib
elif dbus_version >= (0,80,0):
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
else:
pass
except: dbus_imported = False
else: dbus_imported = True
import deluge import deluge
import deluge.common import deluge.common
import deluge.core import deluge.core
import deluge._dbus as dbus
import deluge.interface import deluge.interface
parser = OptionParser(usage="%prog [options] [torrents to add]", parser = OptionParser(usage="%prog [options] [torrents to add]",
@ -123,13 +111,11 @@ def start_deluge():
interface.start(get_cmd_line_torrents()) interface.start(get_cmd_line_torrents())
if dbus_imported:
bus = dbus.SessionBus() bus = dbus.SessionBus()
dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames() dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames()
interface = None
if not "org.deluge_torrent.Deluge" in dbus_objects: if not "org.deluge_torrent.Deluge" in dbus_objects:
print "no existing Deluge session" print "no existing Deluge session"
start_deluge() start_deluge()
@ -143,6 +129,3 @@ if dbus_imported:
for filename in get_cmd_line_torrents(): for filename in get_cmd_line_torrents():
deluge_iface.interactive_add_torrent(filename) deluge_iface.interactive_add_torrent(filename)
else:
print "no existing Deluge session"
start_deluge()

12
src/_dbus.py Normal file
View File

@ -0,0 +1,12 @@
# Import all we will use in deluge
from dbus import Interface, SessionBus, version
# Code for dbus_importing borrowed from Listen (http://listen-project.org)
# I couldn't figure out how to use dbus without breaking on versions past
# 0.80.0. I finally found a solution by reading the source code from the
# Listen project.
if version >= (0,41,0) and version < (0,80,0):
import dbus.glib
elif version >= (0,80,0):
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

View File

@ -30,28 +30,9 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
import deluge._dbus as dbus
# Code for dbus_importing borrowed from Listen (http://listen-project.org)
# I couldn't figure out how to use dbus without breaking on versions past
# 0.80.0. I finally found a solution by reading the source code from the
# Listen project.
try:
import dbus
import dbus.service import dbus.service
dbus_version = getattr(dbus, 'version', (0,0,0))
if dbus_version >= (0,41,0) and dbus_version < (0,80,0):
dbus.SessionBus()
import dbus.glib
elif dbus_version >= (0,80,0):
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
dbus.SessionBus()
else:
pass
except: dbus_imported = False
else: dbus_imported = True
if dbus_imported:
class Manager(dbus.service.Object): class Manager(dbus.service.Object):
def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'): def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'):
self.interface = interface self.interface = interface
@ -63,11 +44,3 @@ if dbus_imported:
@dbus.service.method('org.deluge_torrent.Deluge') @dbus.service.method('org.deluge_torrent.Deluge')
def interactive_add_torrent(self, torrent_file): def interactive_add_torrent(self, torrent_file):
self.interface.interactive_add_torrent(torrent_file) self.interface.interactive_add_torrent(torrent_file)
else:
# This is a fallback class in case dbus is not available
class Manager:
def __init__(self, interface, object_path=None):
self.interface = interface
def interactive_add_torrent(self, torrent_file):
print "I can't do anything with this."