From 4c867264f9a6def2f7d35bd2610e8b688f0b0f18 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sat, 5 Apr 2008 22:40:35 +0000 Subject: [PATCH] Add '-c, --config' options to set config directory. --- TODO | 1 - deluge/common.py | 14 ++------ deluge/config.py | 9 +++-- deluge/configmanager.py | 34 ++++++++++++++++++- deluge/core/autoadd.py | 5 +++ deluge/core/core.py | 11 +++--- deluge/core/daemon.py | 12 ++++--- deluge/core/torrentmanager.py | 7 ++-- deluge/main.py | 12 +++++-- deluge/pluginmanagerbase.py | 2 +- .../blocklist/blocklist/torrentblocklist.py | 4 +-- deluge/ui/gtkui/gtkui.py | 6 ++-- deluge/ui/ui.py | 8 +++-- 13 files changed, 86 insertions(+), 39 deletions(-) diff --git a/TODO b/TODO index 33a4e186d..ec6fa203f 100644 --- a/TODO +++ b/TODO @@ -11,7 +11,6 @@ For 0.6 release: * Implement add by hash * Implement 'Classic' mode * Add tabs to view menu -* Add command line option to change config dir.. --config After 0.6 release: * Figure out easy way for user-made plugins to add i18n support. diff --git a/deluge/common.py b/deluge/common.py index 2bd7149a2..23ca8fb41 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -38,7 +38,6 @@ import os import pkg_resources import xdg, xdg.BaseDirectory - LT_TORRENT_STATE = { "Queued": 0, "Checking": 1, @@ -76,13 +75,12 @@ def get_revision(): return revision -def get_config_dir(filename=None): +def get_default_config_dir(filename=None): """ Returns the config path if no filename is specified Returns the config directory + filename as a path if filename is specified """ if filename != None: - return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), - filename) + return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename) else: return xdg.BaseDirectory.save_config_path("deluge") @@ -93,14 +91,6 @@ def get_default_download_dir(): else: return os.environ.get("HOME") -def get_default_torrent_dir(): - """Returns the default torrent directory""" - return os.path.join(get_config_dir(), "torrentfiles") - -def get_default_plugin_dir(): - """Returns the default plugin directory""" - return os.path.join(get_config_dir(), "plugins") - def windows_check(): """Checks if the current platform is Windows. Returns True if it is Windows and False if not.""" diff --git a/deluge/config.py b/deluge/config.py index e5bc1bd20..11d54ca48 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -34,6 +34,7 @@ """Configuration class used to access/create/modify configuration files.""" import cPickle +import os.path import gobject import deluge.common @@ -42,7 +43,7 @@ from deluge.log import LOG as log class Config: """This class is used to access configuration files.""" - def __init__(self, filename, defaults=None): + def __init__(self, filename, defaults=None, config_dir=None): log.debug("Config created with filename: %s", filename) log.debug("Config defaults: %s", defaults) self.config = {} @@ -55,7 +56,11 @@ class Config: self.config = defaults # Load the config from file in the config_dir - self.config_file = deluge.common.get_config_dir(filename) + if config_dir == None: + self.config_file = deluge.common.get_default_config_dir(filename) + else: + self.config_file = os.path.join(config_dir, filename) + self.load(self.config_file) # Save self.save() diff --git a/deluge/configmanager.py b/deluge/configmanager.py index 163fcdc1f..2870484b9 100644 --- a/deluge/configmanager.py +++ b/deluge/configmanager.py @@ -32,7 +32,10 @@ # statement from all source files in the program, then also delete it here. import gobject +import os +import os.path +import deluge.common from deluge.log import LOG as log from deluge.config import Config @@ -40,6 +43,7 @@ class _ConfigManager: def __init__(self): log.debug("ConfigManager started..") self.config_files = {} + self.config_directory = deluge.common.get_default_config_dir() # Set a 5 minute timer to call save() gobject.timeout_add(300000, self.save) @@ -47,6 +51,24 @@ class _ConfigManager: log.debug("ConfigManager stopping..") del self.config_files + def set_config_dir(self, directory): + """Sets the config directory""" + if directory == None: + return + log.info("Setting config directory to: %s", directory) + if not os.path.exists(directory): + # Try to create the config folder if it doesn't exist + try: + os.makedirs(directory) + except Exception, e: + log.warning("Unable to make config directory: %s", e) + + self.config_directory = directory + + def get_config_dir(self): + log.debug("get_config_dir: %s", self.config_directory) + return self.config_directory + def close(self, config): """Closes a config file.""" try: @@ -66,7 +88,7 @@ class _ConfigManager: log.debug("Getting config '%s'", config_file) # Create the config object if not already created if config_file not in self.config_files.keys(): - self.config_files[config_file] = Config(config_file, defaults) + self.config_files[config_file] = Config(config_file, defaults, self.config_directory) return self.config_files[config_file] @@ -76,5 +98,15 @@ _configmanager = _ConfigManager() def ConfigManager(config, defaults=None): return _configmanager.get_config(config, defaults) +def set_config_dir(directory): + """Sets the config directory, else just uses default""" + return _configmanager.set_config_dir(directory) + +def get_config_dir(filename=None): + if filename != None: + return os.path.join(_configmanager.get_config_dir(), filename) + else: + return _configmanager.get_config_dir() + def close(config): return _configmanager.close(config) diff --git a/deluge/core/autoadd.py b/deluge/core/autoadd.py index 03c3016fb..e053e4131 100644 --- a/deluge/core/autoadd.py +++ b/deluge/core/autoadd.py @@ -58,6 +58,11 @@ class AutoAdd(component.Component): self._on_autoadd_location) def update(self): + if not self.config["autoadd_enable"]: + # We shouldn't be updating because autoadd is not enabled + component.pause("AutoAdd") + return + # Check the auto add folder for new torrents to add if not os.path.exists(self.config["autoadd_location"]): log.warning("Invalid AutoAdd folder: %s", self.config["autoadd_location"]) diff --git a/deluge/core/core.py b/deluge/core/core.py index 098618f68..6ec1c31c8 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -37,6 +37,7 @@ import pkg_resources import sys import shutil import os +import os.path import signal import deluge.SimpleXMLRPCServer as SimpleXMLRPCServer from SocketServer import ThreadingMixIn @@ -45,7 +46,7 @@ import gobject import threading import deluge.libtorrent as lt -from deluge.configmanager import ConfigManager +import deluge.configmanager import deluge.common import deluge.component as component from deluge.core.torrentmanager import TorrentManager @@ -56,14 +57,14 @@ from deluge.core.autoadd import AutoAdd from deluge.log import LOG as log DEFAULT_PREFS = { - "config_location": deluge.common.get_config_dir(), + "config_location": deluge.configmanager.get_config_dir(), "daemon_port": 58846, "allow_remote": False, "compact_allocation": True, "download_location": deluge.common.get_default_download_dir(), "listen_ports": [6881, 6891], - "torrentfiles_location": deluge.common.get_default_torrent_dir(), - "plugins_location": deluge.common.get_default_plugin_dir(), + "torrentfiles_location": os.path.join(deluge.configmanager.get_config_dir(), "torrentfiles"), + "plugins_location": os.path.join(deluge.configmanager.get_config_dir(), "plugins"), "prioritize_first_last_pieces": False, "random_port": True, "dht": False, @@ -107,7 +108,7 @@ class Core( self.client_address = None # Get config - self.config = ConfigManager("core.conf", DEFAULT_PREFS) + self.config = deluge.configmanager.ConfigManager("core.conf", DEFAULT_PREFS) if port == None: port = self.config["daemon_port"] diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index b24e16ab0..77be90adb 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -31,11 +31,15 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. -from deluge.core.core import Core +import deluge.configmanager from deluge.log import LOG as log class Daemon: - def __init__(self, port): - # Start the core as a thread and join it until it's done - self.core = Core(port).run() + def __init__(self, options, args): + # Set the config directory + deluge.configmanager.set_config_dir(options.config) + + from deluge.core.core import Core + # Start the core as a thread and join it until it's done + self.core = Core(options.port).run() diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index fae8f4e81..4bad7aae4 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -457,8 +457,8 @@ class TorrentManager(component.Component): try: log.debug("Opening torrent state file for load.") - state_file = open(deluge.common.get_config_dir("torrents.state"), - "rb") + state_file = open( + os.path.join(self.config["config_location"], "torrents.state"), "rb") state = cPickle.load(state_file) state_file.close() except IOError: @@ -556,7 +556,8 @@ class TorrentManager(component.Component): # Pickle the TorrentManagerState object try: log.debug("Saving torrent state file.") - state_file = open(deluge.common.get_config_dir("torrents.state"), + state_file = open( + os.path.join(self.config["config_location"], "torrents.state"), "wb") cPickle.dump(state, state_file) state_file.close() diff --git a/deluge/main.py b/deluge/main.py index 954556d02..7bd32329a 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -49,7 +49,9 @@ def start_ui(): parser.add_option("-u", "--ui", dest="ui", help="The UI that you wish to launch", action="store", type="str") - + parser.add_option("-c", "--config", dest="config", + help="Set the config location", action="store", type="str") + # Get the options and args from the OptionParser (options, args) = parser.parse_args() @@ -76,6 +78,9 @@ def start_daemon(): help="Port daemon will listen on", action="store", type="int") parser.add_option("-d", "--do-not-daemonize", dest="donot", help="Do not daemonize", action="store_true", default=False) + parser.add_option("-c", "--config", dest="config", + help="Set the config location", action="store", type="str") + # Get the options and args from the OptionParser (options, args) = parser.parse_args() @@ -93,10 +98,11 @@ def start_daemon(): if options.donot: log.info("Starting daemon..") - Daemon(options.port) + Daemon(options, args) else: cmd = "deluged -d " + "".join(a for a in args) if options.port != None: cmd = cmd + " -p %s" % options.port - + if options.config != None: + cmd = cmd + " -c %s" % options.config os.popen2(cmd) diff --git a/deluge/pluginmanagerbase.py b/deluge/pluginmanagerbase.py index 4f5108838..e47c2a682 100644 --- a/deluge/pluginmanagerbase.py +++ b/deluge/pluginmanagerbase.py @@ -82,7 +82,7 @@ class PluginManagerBase: def scan_for_plugins(self): """Scans for available plugins""" plugin_dir = os.path.join(os.path.dirname(__file__), "plugins") - user_plugin_dir = os.path.join(deluge.common.get_config_dir("plugins")) + user_plugin_dir = os.path.join(self.config["config_location"], "plugins") pkg_resources.working_set.add_entry(plugin_dir) pkg_resources.working_set.add_entry(user_plugin_dir) diff --git a/deluge/plugins/blocklist/blocklist/torrentblocklist.py b/deluge/plugins/blocklist/blocklist/torrentblocklist.py index 08f900b2f..e6ca5a9c6 100644 --- a/deluge/plugins/blocklist/blocklist/torrentblocklist.py +++ b/deluge/plugins/blocklist/blocklist/torrentblocklist.py @@ -124,7 +124,7 @@ class TorrentBlockList: # Make sure we have a current block list file locally self.fetch = False - self.local_blocklist = deluge.common.get_config_dir("blocklist.cache") + self.local_blocklist = deluge.configmanager.get_config_dir("blocklist.cache") # Check list for modifications from online version self.check_update() @@ -280,7 +280,7 @@ class TorrentBlockList: request = urllib2.Request(self.url) new_list = opener.open(request) - file = open(deluge.common.get_config_dir("blocklist.cache"), 'w') + file = open(deluge.configmanager.get_config_dir("blocklist.cache"), 'w') log.info('Blocklist: Writing blocklist to disk') # Write new blocklist to disk diff --git a/deluge/ui/gtkui/gtkui.py b/deluge/ui/gtkui/gtkui.py index 98ec795c6..cb1e699c4 100644 --- a/deluge/ui/gtkui/gtkui.py +++ b/deluge/ui/gtkui/gtkui.py @@ -60,11 +60,11 @@ from pluginmanager import PluginManager from dbusinterface import DbusInterface from queuedtorrents import QueuedTorrents from coreconfig import CoreConfig -from deluge.configmanager import ConfigManager +import deluge.configmanager import deluge.common DEFAULT_PREFS = { - "config_location": deluge.common.get_config_dir(), + "config_location": deluge.configmanager.get_config_dir(), "interactive_add": True, "focus_add_dialog": True, "enable_system_tray": True, @@ -128,7 +128,7 @@ class GtkUI: signal.signal(signal.SIGTERM, self.shutdown) # Make sure gtkui.conf has at least the defaults set - self.config = ConfigManager("gtkui.conf", DEFAULT_PREFS) + self.config = deluge.configmanager.ConfigManager("gtkui.conf", DEFAULT_PREFS) # Start the Dbus Interface before anything else.. Just in case we are # already running. diff --git a/deluge/ui/ui.py b/deluge/ui/ui.py index 1fc6138a5..4ab30461c 100644 --- a/deluge/ui/ui.py +++ b/deluge/ui/ui.py @@ -31,7 +31,7 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. -from deluge.configmanager import ConfigManager +import deluge.configmanager from deluge.log import LOG as log @@ -42,7 +42,11 @@ DEFAULT_PREFS = { class UI: def __init__(self, options, args): log.debug("UI init..") - config = ConfigManager("ui.conf", DEFAULT_PREFS) + + # Set the config directory + deluge.configmanager.set_config_dir(options.config) + + config = deluge.configmanager.ConfigManager("ui.conf", DEFAULT_PREFS) if options.ui != None: config["selected_ui"] = options.ui