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,26 +111,21 @@ def start_deluge():
interface.start(get_cmd_line_torrents()) interface.start(get_cmd_line_torrents())
if dbus_imported:
bus = dbus.SessionBus()
dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames() bus = dbus.SessionBus()
interface = None dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames()
if not "org.deluge_torrent.Deluge" in dbus_objects: if not "org.deluge_torrent.Deluge" in dbus_objects:
print "no existing Deluge session"
start_deluge()
else:
## This connects to the deluge interface
print "create proxy object"
proxy = bus.get_object('org.deluge_torrent.Deluge', '/org/deluge_torrent/DelugeObject')
print "create iface"
deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge')
print "send to iface"
for filename in get_cmd_line_torrents():
deluge_iface.interactive_add_torrent(filename)
else:
print "no existing Deluge session" print "no existing Deluge session"
start_deluge() start_deluge()
else:
## This connects to the deluge interface
print "create proxy object"
proxy = bus.get_object('org.deluge_torrent.Deluge', '/org/deluge_torrent/DelugeObject')
print "create iface"
deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge')
print "send to iface"
for filename in get_cmd_line_torrents():
deluge_iface.interactive_add_torrent(filename)

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,44 +30,17 @@
# 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
import dbus.service
# Code for dbus_importing borrowed from Listen (http://listen-project.org) class Manager(dbus.service.Object):
# I couldn't figure out how to use dbus without breaking on versions past def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'):
# 0.80.0. I finally found a solution by reading the source code from the self.interface = interface
# Listen project. self.bus = dbus.SessionBus()
try: bus_name = dbus.service.BusName("org.deluge_torrent.Deluge", bus=self.bus)
import dbus dbus.service.Object.__init__(self, bus_name, object_path)
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: ## interactive_add_torrent should only be called from outside the class
class Manager(dbus.service.Object): @dbus.service.method('org.deluge_torrent.Deluge')
def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'): def interactive_add_torrent(self, torrent_file):
self.interface = interface self.interface.interactive_add_torrent(torrent_file)
self.bus = dbus.SessionBus()
bus_name = dbus.service.BusName("org.deluge_torrent.Deluge", bus=self.bus)
dbus.service.Object.__init__(self, bus_name, object_path)
## interactive_add_torrent should only be called from outside the class
@dbus.service.method('org.deluge_torrent.Deluge')
def interactive_add_torrent(self, 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."