Use dbus variants instead of pickling objects.

This commit is contained in:
Andrew Resch 2007-10-04 23:54:58 +00:00
commit c2f7c362b3
3 changed files with 19 additions and 49 deletions

1
TODO
View file

@ -12,4 +12,3 @@
* Restart daemon function * Restart daemon function
* Sync the details pane to current trunk * Sync the details pane to current trunk
* Docstrings! * Docstrings!
* Change core to use dbus variants instead of byte-arrays

View file

@ -272,38 +272,30 @@ class Core(dbus.service.Object):
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge", @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="sas", in_signature="sas",
out_signature="ay") out_signature="a{sv}")
def get_torrent_status(self, torrent_id, keys): def get_torrent_status(self, torrent_id, keys):
# Convert the array of strings to a python list of strings # Convert the array of strings to a python list of strings
nkeys = [] keys = deluge.common.pythonize(keys)
for key in keys: # Build the status dictionary
nkeys.append(str(key))
# Pickle the status dictionary from the torrent
try: try:
status = self.torrents[torrent_id].get_status(nkeys) status = self.torrents[torrent_id].get_status(keys)
except KeyError: except KeyError:
# The torrent_id is not found in the torrentmanager, so return None # The torrent_id is not found in the torrentmanager, so return None
status = None return None
status = pickle.dumps(status)
return status
# Get the leftover fields and ask the plugin manager to fill them # Get the leftover fields and ask the plugin manager to fill them
leftover_fields = list(set(nkeys) - set(status.keys())) leftover_fields = list(set(keys) - set(status.keys()))
if len(leftover_fields) > 0: if len(leftover_fields) > 0:
status.update(self.plugins.get_status(torrent_id, leftover_fields)) status.update(self.plugins.get_status(torrent_id, leftover_fields))
status = pickle.dumps(status)
return status return status
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge", @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="", in_signature="",
out_signature="ay") out_signature="as")
def get_session_state(self): def get_session_state(self):
"""Returns a list of torrent_ids in the session.""" """Returns a list of torrent_ids in the session."""
# Get the torrent list from the TorrentManager # Get the torrent list from the TorrentManager
torrent_list = self.torrents.get_torrent_list() return self.torrents.get_torrent_list()
# Pickle the list and send it
session_state = pickle.dumps(torrent_list)
return session_state
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge") @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
def save_state(self): def save_state(self):
@ -313,33 +305,28 @@ class Core(dbus.service.Object):
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge", @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="", in_signature="",
out_signature="ay") out_signature="a{sv}")
def get_config(self): def get_config(self):
"""Get all the preferences as a dictionary""" """Get all the preferences as a dictionary"""
config = self.config.get_config() return self.config.get_config()
config = pickle.dumps(config)
return config
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge", @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="s", in_signature="s",
out_signature="ay") out_signature="v")
def get_config_value(self, key): def get_config_value(self, key):
"""Get the config value for key""" """Get the config value for key"""
try: try:
value = self.config[key] value = self.config[key]
except KeyError: except KeyError:
return None return None
value = pickle.dumps(value)
return value return value
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge", @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="ay") in_signature="a{sv}")
def set_config(self, config): def set_config(self, config):
"""Set the config with values from dictionary""" """Set the config with values from dictionary"""
# Convert the byte array into the dictionary config = deluge.common.pythonize(config)
config = "".join(chr(b) for b in config)
config = pickle.loads(config)
# Load all the values into the configuration # Load all the values into the configuration
for key in config.keys(): for key in config.keys():
self.config[key] = config[key] self.config[key] = config[key]

View file

@ -42,6 +42,7 @@ import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk, gtk.glade import gtk, gtk.glade
import deluge.common
from deluge.log import LOG as log from deluge.log import LOG as log
def get_core(): def get_core():
@ -130,46 +131,29 @@ def force_reannounce(torrent_ids):
def get_torrent_status(core, torrent_id, keys): def get_torrent_status(core, torrent_id, keys):
"""Builds the status dictionary and returns it""" """Builds the status dictionary and returns it"""
status = core.get_torrent_status(torrent_id, keys) return deluge.common.pythonize(core.get_torrent_status(torrent_id, keys))
# Join the array of bytes into a string for pickle to read
status = "".join(chr(b) for b in status)
# De-serialize the object
status = pickle.loads(status)
return status
def get_session_state(core=None): def get_session_state(core=None):
# Get the core if not supplied # Get the core if not supplied
if core is None: if core is None:
core = get_core() core = get_core()
state = core.get_session_state() return deluge.common.pythonize(core.get_session_state())
# Join the array of bytes into a string for pickle to read
state = "".join(chr(b) for b in state)
# De-serialize the object
state = pickle.loads(state)
return state
def get_config(core=None): def get_config(core=None):
if core is None: if core is None:
core = get_core() core = get_core()
config = core.get_config() return deluge.common.pythonize(core.get_config())
config = "".join(chr(b) for b in config)
config = pickle.loads(config)
return config
def get_config_value(key, core=None): def get_config_value(key, core=None):
if core is None: if core is None:
core = get_core() core = get_core()
config = core.get_config_value(key) return deluge.common.pythonize(core.get_config_value(key))
config = "".join(chr(b) for b in config)
config = pickle.loads(config)
return config
def set_config(config, core=None): def set_config(config, core=None):
if config == {}: if config == {}:
return return
if core is None: if core is None:
core = get_core() core = get_core()
config = pickle.dumps(config)
core.set_config(config) core.set_config(config)
def get_listen_port(core=None): def get_listen_port(core=None):