global preferences patch by andar

This commit is contained in:
Marcos Pinto 2007-06-08 00:15:51 +00:00
parent 1b6f40c12a
commit 90a473e013
3 changed files with 101 additions and 88 deletions

View File

@ -41,6 +41,7 @@ import os, os.path, shutil, statvfs
import pickle import pickle
import time import time
import gettext import gettext
import pref
# Constants # Constants
@ -52,28 +53,17 @@ DHT_FILENAME = "dht.state"
CACHED_DATA_EXPIRATION = 1 # seconds, like the output of time.time() CACHED_DATA_EXPIRATION = 1 # seconds, like the output of time.time()
# "max_half_open" : -1,
DEFAULT_PREFS = {
"max_uploads" : 2, # a.k.a. upload slots
"listen_on" : [6881,9999],
"max_connections" : 80,
"use_DHT" : True,
"max_active_torrents" : -1,
"auto_seed_ratio" : -1,
"max_download_rate" : -1,
"max_upload_rate" : -1
}
PREF_FUNCTIONS = { PREF_FUNCTIONS = {
"max_uploads" : deluge_core.set_max_uploads, "max_uploads" : deluge_core.set_max_uploads,
"listen_on" : deluge_core.set_listen_on, "listen_on" : deluge_core.set_listen_on,
"max_connections" : deluge_core.set_max_connections, "max_connections" : deluge_core.set_max_connections,
"use_DHT" : None, # not a normal pref in that is is applied only on start "enable_dht" : None, # not a normal pref in that is is applied only on start
"max_active_torrents" : None, # no need for a function, applied constantly "max_active_torrents" : None, # no need for a function, applied constantly
"auto_seed_ratio" : None, # no need for a function, applied constantly "auto_seed_ratio" : None, # no need for a function, applied constantly
"max_download_rate" : deluge_core.set_download_rate_limit, "max_download_rate_bps" : deluge_core.set_download_rate_limit,
"max_upload_rate" : deluge_core.set_upload_rate_limit "max_upload_rate_bps" : deluge_core.set_upload_rate_limit
} }
STATE_MESSAGES = ( "Queued", STATE_MESSAGES = ( "Queued",
"Checking", "Checking",
"Connecting", "Connecting",
@ -84,7 +74,6 @@ STATE_MESSAGES = ( "Queued",
"Allocating" "Allocating"
) )
# Exceptions # Exceptions
class DelugeError(Exception): class DelugeError(Exception):
@ -206,21 +195,14 @@ class Manager:
self.saved_core_torrent_file_infos = {} # unique_ID -> torrent_state self.saved_core_torrent_file_infos = {} # unique_ID -> torrent_state
# Unpickle the preferences, or create a new one # Load the preferences
self.prefs = DEFAULT_PREFS self.config = pref.Preferences(os.path.join(self.base_dir, PREFS_FILENAME))
if not blank_slate:
try:
pkl_file = open(os.path.join(self.base_dir, PREFS_FILENAME), 'rb')
self.prefs = pickle.load(pkl_file)
pkl_file.close()
except IOError:
pass
# Apply preferences. Note that this is before any torrents are added # Apply preferences. Note that this is before any torrents are added
self.apply_prefs() self.apply_prefs()
# Apply DHT, if needed. Note that this is before any torrents are added # Apply DHT, if needed. Note that this is before any torrents are added
if self.get_pref('use_DHT'): if self.get_pref('enable_dht'):
if not blank_slate: if not blank_slate:
deluge_core.start_DHT(os.path.join(self.base_dir, DHT_FILENAME)) deluge_core.start_DHT(os.path.join(self.base_dir, DHT_FILENAME))
else: else:
@ -251,10 +233,8 @@ class Manager:
self.pre_quitting() self.pre_quitting()
# Pickle the prefs # Pickle the prefs
print "Pickling prefs..." print "Saving prefs..."
output = open(os.path.join(self.base_dir, PREFS_FILENAME), 'wb') self.config.save()
pickle.dump(self.prefs, output)
output.close()
# Pickle the state # Pickle the state
print "Pickling state..." print "Pickling state..."
@ -267,7 +247,7 @@ class Manager:
self.save_fastresume_data() self.save_fastresume_data()
# Stop DHT, if needed # Stop DHT, if needed
if self.get_pref('use_DHT'): if self.get_pref('enable_dht'):
print "Stopping DHT..." print "Stopping DHT..."
deluge_core.stop_DHT(os.path.join(self.base_dir, DHT_FILENAME)) deluge_core.stop_DHT(os.path.join(self.base_dir, DHT_FILENAME))
@ -284,24 +264,20 @@ class Manager:
# Preference management functions # Preference management functions
def get_config(self):
# This returns the preference object
return self.config
def get_pref(self, key): def get_pref(self, key):
# If we have a value, return, else fallback on default_prefs, else raise an error # Get the value from the preferences object
# the fallback is useful if the source has newer prefs than the existing pref state, return self.config.get(key)
# which was created by an old version of the source
if key in self.prefs.keys():
return self.prefs[key]
elif key in DEFAULT_PREFS:
self.prefs[key] = DEFAULT_PREFS[key]
return self.prefs[key]
else:
raise DelugeError("Asked for a pref that doesn't exist: " + key)
def set_pref(self, key, value): def set_pref(self, key, value):
# Make sure this is a valid key # Make sure this is a valid key
if key not in DEFAULT_PREFS.keys(): if key not in pref.DEFAULT_PREFS.keys():
raise DelugeError("Asked to change a pref that isn't valid: " + key) raise DelugeError("Asked to change a pref that isn't valid: " + key)
self.prefs[key] = value self.config.set(key, value)
# Apply the pref, if applicable # Apply the pref, if applicable
if PREF_FUNCTIONS[key] is not None: if PREF_FUNCTIONS[key] is not None:
@ -373,7 +349,7 @@ class Manager:
# Get additional data from our level # Get additional data from our level
ret['is_listening'] = deluge_core.is_listening() ret['is_listening'] = deluge_core.is_listening()
ret['port'] = deluge_core.listening_port() ret['port'] = deluge_core.listening_port()
if self.get_pref('use_DHT'): if self.get_pref('enable_dht'):
ret['DHT_nodes'] = deluge_core.get_DHT_info() ret['DHT_nodes'] = deluge_core.get_DHT_info()
return ret return ret
@ -728,7 +704,6 @@ class Manager:
def apply_prefs(self): def apply_prefs(self):
print "Applying preferences" print "Applying preferences"
assert(len(PREF_FUNCTIONS) == len(DEFAULT_PREFS))
for pref in PREF_FUNCTIONS.keys(): for pref in PREF_FUNCTIONS.keys():
if PREF_FUNCTIONS[pref] is not None: if PREF_FUNCTIONS[pref] is not None:

View File

@ -85,15 +85,6 @@ class DelugeGTK:
self.is_running = False self.is_running = False
self.ipc_manager = ipc_manager.Manager(self) self.ipc_manager = ipc_manager.Manager(self)
self.torrent_file_queue = [] self.torrent_file_queue = []
#Load up a config file:
self.conf_file = os.path.join(common.CONFIG_DIR, 'deluge.conf')
if os.path.isdir(self.conf_file):
print 'Weird, the file I was trying to write to, %s, is an existing directory'%(self.conf_file)
sys.exit(0)
if not os.path.isfile(self.conf_file):
f = open(self.conf_file, mode='w')
f.flush()
f.close()
#Start the Deluge Manager: #Start the Deluge Manager:
self.manager = core.Manager(common.CLIENT_CODE, common.CLIENT_VERSION, self.manager = core.Manager(common.CLIENT_CODE, common.CLIENT_VERSION,
'%s %s'%(common.PROGRAM_NAME, common.PROGRAM_VERSION), common.CONFIG_DIR) '%s %s'%(common.PROGRAM_NAME, common.PROGRAM_VERSION), common.CONFIG_DIR)
@ -103,7 +94,7 @@ class DelugeGTK:
if os.path.isdir(os.path.join(common.CONFIG_DIR , 'plugins')): if os.path.isdir(os.path.join(common.CONFIG_DIR , 'plugins')):
self.plugins.add_plugin_dir(os.path.join(common.CONFIG_DIR, 'plugins')) self.plugins.add_plugin_dir(os.path.join(common.CONFIG_DIR, 'plugins'))
self.plugins.scan_for_plugins() self.plugins.scan_for_plugins()
self.config = pref.Preferences(self.conf_file, DEFAULT_PREFS) self.config = self.manager.get_config()
#Set up the interface: #Set up the interface:
self.wtree = gtk.glade.XML(common.get_glade_file("delugegtk.glade"), domain=APP) self.wtree = gtk.glade.XML(common.get_glade_file("delugegtk.glade"), domain=APP)
self.window = self.wtree.get_widget("main_window") self.window = self.wtree.get_widget("main_window")
@ -515,7 +506,7 @@ class DelugeGTK:
if self.window.get_property("visible"): if self.window.get_property("visible"):
self.preferences_dialog.show() self.preferences_dialog.show()
self.apply_prefs() self.apply_prefs()
self.config.save_to_file() self.config.save()
else: else:
if self.config.get("lock_tray", bool, default=False) == True: if self.config.get("lock_tray", bool, default=False) == True:
@ -544,8 +535,8 @@ class DelugeGTK:
auto_seed_ratio = -1 auto_seed_ratio = -1
self.tray_icon.set_visible(self.config.get("enable_system_tray", bool, default=True)) self.tray_icon.set_visible(self.config.get("enable_system_tray", bool, default=True))
self.manager.set_pref("listen_on", ports) self.manager.set_pref("listen_on", ports)
self.manager.set_pref("max_upload_rate", ulrate) self.manager.set_pref("max_upload_rate_bps", ulrate)
self.manager.set_pref("max_download_rate", dlrate) self.manager.set_pref("max_download_rate_bps", dlrate)
self.manager.set_pref("max_uploads", self.config.get("max_number_uploads", int, default=-1)) self.manager.set_pref("max_uploads", self.config.get("max_number_uploads", int, default=-1))
self.manager.set_pref("max_connections", self.config.get("max_number_downloads", int, default=-1)) self.manager.set_pref("max_connections", self.config.get("max_number_downloads", int, default=-1))
self.manager.set_pref("auto_seed_ratio", auto_seed_ratio) self.manager.set_pref("auto_seed_ratio", auto_seed_ratio)
@ -1076,7 +1067,7 @@ class DelugeGTK:
enabled_plugins = ':'.join(self.plugins.get_enabled_plugins()) enabled_plugins = ':'.join(self.plugins.get_enabled_plugins())
self.config.set('enabled_plugins', enabled_plugins) self.config.set('enabled_plugins', enabled_plugins)
self.save_window_settings() self.save_window_settings()
self.config.save_to_file(self.conf_file) self.config.save()
self.plugins.shutdown_all_plugins() self.plugins.shutdown_all_plugins()
self.manager.quit() self.manager.quit()
gtk.main_quit() gtk.main_quit()

View File

@ -22,12 +22,65 @@
# Preferences is basically a wrapper around a simple Python dictionary # Preferences is basically a wrapper around a simple Python dictionary
# object. However, this class provides a few extra features on top of # object. However, this class provides a few extra features on top of
# the built in class that Deluge can take advantage of. # the built in class that Deluge can take advantage of.
import pickle
import common
DEFAULT_PREFS = {
"encin_state" : common.EncState.enabled,
"encout_state" : common.EncState.enabled,
"enclevel_type" : common.EncLevel.both,
"pref_rc4" : True,
"auto_end_seeding" : False,
"auto_seed_ratio" : -1,
"close_to_tray" : False,
"lock_tray" : False,
"tray_passwd" : "",
"default_download_path" : "",
"dht_connections" : 80,
"enable_dht" : True,
"enable_system_tray" : True,
"enabled_plugins" : "",
"end_seed_ratio" : 0.0,
"gui_update_interval" : 1.0,
"listen_on" : [6881,9999],
"max_active_torrents" : -1,
"max_connections" : 80,
"max_download_rate" : -1.0,
"max_download_rate_bps": -1.0,
"max_number_downloads" : -1.0,
"max_number_torrents" : -1.0,
"max_number_uploads" : -1.0,
"max_upload_rate" : -1.0,
"max_upload_rate_bps" : -1.0,
"max_uploads" : 2,
"queue_seeds_to_bottom" : False,
"show_dl" : True,
"show_eta" : True,
"show_infopane" : True,
"show_peers" : True,
"show_seeders" : True,
"show_share" : True,
"show_size" : True,
"show_status" : True,
"show_toolbar" : True,
"show_ul" : True,
"tcp_port_range_lower" : 6881,
"tcp_port_range_upper" : 6889,
"use_compact_storage" : False,
"use_default_dir" : False,
"window_height" : 480,
"window_width" : 640,
"window_x_pos" : 0,
"window_y_pos" : 0,
}
class Preferences: class Preferences:
def __init__(self, filename=None, defaults=None): def __init__(self, filename=None, defaults=None):
self.mapping = {} self.mapping = DEFAULT_PREFS
print self.mapping.keys()
self.config_file = filename self.config_file = filename
if self.config_file is not None: if self.config_file is not None:
self.load_from_file(self.config_file) self.load(self.config_file)
if defaults is not None: if defaults is not None:
for key in defaults.keys(): for key in defaults.keys():
self.mapping.setdefault(key, defaults[key]) self.mapping.setdefault(key, defaults[key])
@ -53,38 +106,32 @@ class Preferences:
def keys(self): return self.mapping.keys() def keys(self): return self.mapping.keys()
def values(self): return self.mapping.values() def values(self): return self.mapping.values()
def save_to_file(self, filename=None): def save(self, filename=None):
if filename is None: if filename is None:
filename = self.config_file filename = self.config_file
f = open(filename, mode='w') try:
keys = self.mapping.keys() pkl_file = open(filename, 'wb')
keys.sort() pickle.dump(self.mapping, pkl_file)
for key in keys: pkl_file.close()
f.write(key) except IOError:
f.write(' = ') pass
f.write(str(self.mapping[key]))
f.write('\n') def load(self, filename=None):
f.flush()
f.close()
self.config_file = filename
def load_from_file(self, filename=None):
if filename is None: if filename is None:
filename = self.config_file filename = self.config_file
f = open(filename, mode='r') try:
for line in f: pkl_file = open(filename, 'rb')
try: self.dump = pickle.load(pkl_file)
(key, value) = line.split('=', 1) self.mapping.update(self.dump)
key = key.strip(' \n') pkl_file.close()
value = value.strip(' \n') except IOError:
self.mapping[key] = value pass
except ValueError: except EOFError:
pass pkl_file.close()
f.close() pass
self.config_file = filename
def set(self, key, value): def set(self, key, value):
self.mapping[key] = value self.mapping[key] = value
def get(self, key, kind=None, default=None): def get(self, key, kind=None, default=None):
if not key in self.mapping.keys(): if not key in self.mapping.keys():