Add '-c, --config' options to set config directory.

This commit is contained in:
Andrew Resch 2008-04-05 22:40:35 +00:00
parent 72e0df3e5f
commit 4c867264f9
13 changed files with 86 additions and 39 deletions

1
TODO
View File

@ -11,7 +11,6 @@ For 0.6 release:
* Implement add by hash * Implement add by hash
* Implement 'Classic' mode * Implement 'Classic' mode
* Add tabs to view menu * Add tabs to view menu
* Add command line option to change config dir.. --config
After 0.6 release: After 0.6 release:
* Figure out easy way for user-made plugins to add i18n support. * Figure out easy way for user-made plugins to add i18n support.

View File

@ -38,7 +38,6 @@ import os
import pkg_resources import pkg_resources
import xdg, xdg.BaseDirectory import xdg, xdg.BaseDirectory
LT_TORRENT_STATE = { LT_TORRENT_STATE = {
"Queued": 0, "Queued": 0,
"Checking": 1, "Checking": 1,
@ -76,13 +75,12 @@ def get_revision():
return 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 path if no filename is specified
Returns the config directory + filename as a path if filename is specified Returns the config directory + filename as a path if filename is specified
""" """
if filename != None: if filename != None:
return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename)
filename)
else: else:
return xdg.BaseDirectory.save_config_path("deluge") return xdg.BaseDirectory.save_config_path("deluge")
@ -93,14 +91,6 @@ def get_default_download_dir():
else: else:
return os.environ.get("HOME") 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(): def windows_check():
"""Checks if the current platform is Windows. Returns True if it is Windows """Checks if the current platform is Windows. Returns True if it is Windows
and False if not.""" and False if not."""

View File

@ -34,6 +34,7 @@
"""Configuration class used to access/create/modify configuration files.""" """Configuration class used to access/create/modify configuration files."""
import cPickle import cPickle
import os.path
import gobject import gobject
import deluge.common import deluge.common
@ -42,7 +43,7 @@ from deluge.log import LOG as log
class Config: class Config:
"""This class is used to access configuration files.""" """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 created with filename: %s", filename)
log.debug("Config defaults: %s", defaults) log.debug("Config defaults: %s", defaults)
self.config = {} self.config = {}
@ -55,7 +56,11 @@ class Config:
self.config = defaults self.config = defaults
# Load the config from file in the config_dir # 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) self.load(self.config_file)
# Save # Save
self.save() self.save()

View File

@ -32,7 +32,10 @@
# 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 gobject import gobject
import os
import os.path
import deluge.common
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.config import Config from deluge.config import Config
@ -40,6 +43,7 @@ class _ConfigManager:
def __init__(self): def __init__(self):
log.debug("ConfigManager started..") log.debug("ConfigManager started..")
self.config_files = {} self.config_files = {}
self.config_directory = deluge.common.get_default_config_dir()
# Set a 5 minute timer to call save() # Set a 5 minute timer to call save()
gobject.timeout_add(300000, self.save) gobject.timeout_add(300000, self.save)
@ -47,6 +51,24 @@ class _ConfigManager:
log.debug("ConfigManager stopping..") log.debug("ConfigManager stopping..")
del self.config_files 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): def close(self, config):
"""Closes a config file.""" """Closes a config file."""
try: try:
@ -66,7 +88,7 @@ class _ConfigManager:
log.debug("Getting config '%s'", config_file) log.debug("Getting config '%s'", config_file)
# Create the config object if not already created # Create the config object if not already created
if config_file not in self.config_files.keys(): 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] return self.config_files[config_file]
@ -76,5 +98,15 @@ _configmanager = _ConfigManager()
def ConfigManager(config, defaults=None): def ConfigManager(config, defaults=None):
return _configmanager.get_config(config, defaults) 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): def close(config):
return _configmanager.close(config) return _configmanager.close(config)

View File

@ -58,6 +58,11 @@ class AutoAdd(component.Component):
self._on_autoadd_location) self._on_autoadd_location)
def update(self): 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 # Check the auto add folder for new torrents to add
if not os.path.exists(self.config["autoadd_location"]): if not os.path.exists(self.config["autoadd_location"]):
log.warning("Invalid AutoAdd folder: %s", self.config["autoadd_location"]) log.warning("Invalid AutoAdd folder: %s", self.config["autoadd_location"])

View File

@ -37,6 +37,7 @@ import pkg_resources
import sys import sys
import shutil import shutil
import os import os
import os.path
import signal import signal
import deluge.SimpleXMLRPCServer as SimpleXMLRPCServer import deluge.SimpleXMLRPCServer as SimpleXMLRPCServer
from SocketServer import ThreadingMixIn from SocketServer import ThreadingMixIn
@ -45,7 +46,7 @@ import gobject
import threading import threading
import deluge.libtorrent as lt import deluge.libtorrent as lt
from deluge.configmanager import ConfigManager import deluge.configmanager
import deluge.common import deluge.common
import deluge.component as component import deluge.component as component
from deluge.core.torrentmanager import TorrentManager from deluge.core.torrentmanager import TorrentManager
@ -56,14 +57,14 @@ from deluge.core.autoadd import AutoAdd
from deluge.log import LOG as log from deluge.log import LOG as log
DEFAULT_PREFS = { DEFAULT_PREFS = {
"config_location": deluge.common.get_config_dir(), "config_location": deluge.configmanager.get_config_dir(),
"daemon_port": 58846, "daemon_port": 58846,
"allow_remote": False, "allow_remote": False,
"compact_allocation": True, "compact_allocation": True,
"download_location": deluge.common.get_default_download_dir(), "download_location": deluge.common.get_default_download_dir(),
"listen_ports": [6881, 6891], "listen_ports": [6881, 6891],
"torrentfiles_location": deluge.common.get_default_torrent_dir(), "torrentfiles_location": os.path.join(deluge.configmanager.get_config_dir(), "torrentfiles"),
"plugins_location": deluge.common.get_default_plugin_dir(), "plugins_location": os.path.join(deluge.configmanager.get_config_dir(), "plugins"),
"prioritize_first_last_pieces": False, "prioritize_first_last_pieces": False,
"random_port": True, "random_port": True,
"dht": False, "dht": False,
@ -107,7 +108,7 @@ class Core(
self.client_address = None self.client_address = None
# Get config # Get config
self.config = ConfigManager("core.conf", DEFAULT_PREFS) self.config = deluge.configmanager.ConfigManager("core.conf", DEFAULT_PREFS)
if port == None: if port == None:
port = self.config["daemon_port"] port = self.config["daemon_port"]

View File

@ -31,11 +31,15 @@
# 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.
from deluge.core.core import Core import deluge.configmanager
from deluge.log import LOG as log from deluge.log import LOG as log
class Daemon: class Daemon:
def __init__(self, port): def __init__(self, options, args):
# Start the core as a thread and join it until it's done # Set the config directory
self.core = Core(port).run() 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()

View File

@ -457,8 +457,8 @@ class TorrentManager(component.Component):
try: try:
log.debug("Opening torrent state file for load.") log.debug("Opening torrent state file for load.")
state_file = open(deluge.common.get_config_dir("torrents.state"), state_file = open(
"rb") os.path.join(self.config["config_location"], "torrents.state"), "rb")
state = cPickle.load(state_file) state = cPickle.load(state_file)
state_file.close() state_file.close()
except IOError: except IOError:
@ -556,7 +556,8 @@ class TorrentManager(component.Component):
# Pickle the TorrentManagerState object # Pickle the TorrentManagerState object
try: try:
log.debug("Saving torrent state file.") 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") "wb")
cPickle.dump(state, state_file) cPickle.dump(state, state_file)
state_file.close() state_file.close()

View File

@ -49,6 +49,8 @@ def start_ui():
parser.add_option("-u", "--ui", dest="ui", parser.add_option("-u", "--ui", dest="ui",
help="The UI that you wish to launch", action="store", type="str") 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 # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
@ -76,6 +78,9 @@ def start_daemon():
help="Port daemon will listen on", action="store", type="int") help="Port daemon will listen on", action="store", type="int")
parser.add_option("-d", "--do-not-daemonize", dest="donot", parser.add_option("-d", "--do-not-daemonize", dest="donot",
help="Do not daemonize", action="store_true", default=False) 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 # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
@ -93,10 +98,11 @@ def start_daemon():
if options.donot: if options.donot:
log.info("Starting daemon..") log.info("Starting daemon..")
Daemon(options.port) Daemon(options, args)
else: else:
cmd = "deluged -d " + "".join(a for a in args) cmd = "deluged -d " + "".join(a for a in args)
if options.port != None: if options.port != None:
cmd = cmd + " -p %s" % options.port cmd = cmd + " -p %s" % options.port
if options.config != None:
cmd = cmd + " -c %s" % options.config
os.popen2(cmd) os.popen2(cmd)

View File

@ -82,7 +82,7 @@ class PluginManagerBase:
def scan_for_plugins(self): def scan_for_plugins(self):
"""Scans for available plugins""" """Scans for available plugins"""
plugin_dir = os.path.join(os.path.dirname(__file__), "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(plugin_dir)
pkg_resources.working_set.add_entry(user_plugin_dir) pkg_resources.working_set.add_entry(user_plugin_dir)

View File

@ -124,7 +124,7 @@ class TorrentBlockList:
# Make sure we have a current block list file locally # Make sure we have a current block list file locally
self.fetch = False 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 # Check list for modifications from online version
self.check_update() self.check_update()
@ -280,7 +280,7 @@ class TorrentBlockList:
request = urllib2.Request(self.url) request = urllib2.Request(self.url)
new_list = opener.open(request) 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') log.info('Blocklist: Writing blocklist to disk')
# Write new blocklist to disk # Write new blocklist to disk

View File

@ -60,11 +60,11 @@ from pluginmanager import PluginManager
from dbusinterface import DbusInterface from dbusinterface import DbusInterface
from queuedtorrents import QueuedTorrents from queuedtorrents import QueuedTorrents
from coreconfig import CoreConfig from coreconfig import CoreConfig
from deluge.configmanager import ConfigManager import deluge.configmanager
import deluge.common import deluge.common
DEFAULT_PREFS = { DEFAULT_PREFS = {
"config_location": deluge.common.get_config_dir(), "config_location": deluge.configmanager.get_config_dir(),
"interactive_add": True, "interactive_add": True,
"focus_add_dialog": True, "focus_add_dialog": True,
"enable_system_tray": True, "enable_system_tray": True,
@ -128,7 +128,7 @@ class GtkUI:
signal.signal(signal.SIGTERM, self.shutdown) signal.signal(signal.SIGTERM, self.shutdown)
# Make sure gtkui.conf has at least the defaults set # 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 # Start the Dbus Interface before anything else.. Just in case we are
# already running. # already running.

View File

@ -31,7 +31,7 @@
# 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.
from deluge.configmanager import ConfigManager import deluge.configmanager
from deluge.log import LOG as log from deluge.log import LOG as log
@ -42,7 +42,11 @@ DEFAULT_PREFS = {
class UI: class UI:
def __init__(self, options, args): def __init__(self, options, args):
log.debug("UI init..") 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: if options.ui != None:
config["selected_ui"] = options.ui config["selected_ui"] = options.ui