2007-07-05 19:35:59 +00:00
|
|
|
#
|
|
|
|
# config.py
|
|
|
|
#
|
2007-07-21 00:50:13 +00:00
|
|
|
# Copyright (C) 2007 Andrew Resch ('andar') <andrewresch@gmail.com>
|
2007-07-05 19:35:59 +00:00
|
|
|
#
|
|
|
|
# Deluge is free software.
|
|
|
|
#
|
|
|
|
# You may redistribute it and/or modify it under the terms of the
|
|
|
|
# GNU General Public License, as published by the Free Software
|
|
|
|
# Foundation; either version 2 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# deluge is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
# See the GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-07-13 01:34:18 +00:00
|
|
|
# along with deluge. If not, write to:
|
2007-07-05 19:35:59 +00:00
|
|
|
# The Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor
|
2007-07-13 01:34:18 +00:00
|
|
|
# Boston, MA 02110-1301, USA.
|
2007-07-05 19:35:59 +00:00
|
|
|
#
|
2007-07-13 01:34:18 +00:00
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link the code of portions of this program with the OpenSSL
|
|
|
|
# library.
|
|
|
|
# You must obey the GNU General Public License in all respects for all of
|
|
|
|
# the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
# exception, you may extend this exception to your version of the file(s),
|
|
|
|
# but you are not obligated to do so. If you do not wish to do so, delete
|
|
|
|
# this exception statement from your version. If you delete this exception
|
|
|
|
# statement from all source files in the program, then also delete it here.
|
2007-07-05 19:35:59 +00:00
|
|
|
|
2007-08-26 14:34:50 +00:00
|
|
|
"""Configuration class used to access/create/modify configuration files."""
|
|
|
|
|
2007-07-05 19:35:59 +00:00
|
|
|
import pickle
|
|
|
|
|
|
|
|
import deluge.common
|
2007-08-26 14:34:50 +00:00
|
|
|
from deluge.log import LOG as log
|
2007-07-05 19:35:59 +00:00
|
|
|
|
|
|
|
class Config:
|
2007-08-26 14:34:50 +00:00
|
|
|
"""This class is used to access configuration files."""
|
|
|
|
|
2007-07-13 01:34:18 +00:00
|
|
|
def __init__(self, filename, defaults=None):
|
|
|
|
log.debug("Config created with filename: %s", filename)
|
|
|
|
log.debug("Config defaults: %s", defaults)
|
|
|
|
self.config = {}
|
|
|
|
# If defaults is not None then we need to use "defaults".
|
|
|
|
if defaults != None:
|
|
|
|
self.config = defaults
|
2007-07-05 19:35:59 +00:00
|
|
|
|
2007-07-13 01:34:18 +00:00
|
|
|
# Load the config from file in the config_dir
|
|
|
|
self.config_file = deluge.common.get_config_dir(filename)
|
|
|
|
self.load(self.config_file)
|
2007-07-25 00:43:37 +00:00
|
|
|
# Save
|
|
|
|
self.save()
|
2007-07-21 00:50:13 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
log.debug("Config object deconstructing..")
|
|
|
|
self.save()
|
2007-07-13 01:34:18 +00:00
|
|
|
|
|
|
|
def load(self, filename=None):
|
2007-08-26 14:34:50 +00:00
|
|
|
"""Load a config file either by 'filename' or the filename set during
|
|
|
|
construction of this object."""
|
2007-07-13 01:34:18 +00:00
|
|
|
# Use self.config_file if filename is None
|
|
|
|
if filename is None:
|
|
|
|
filename = self.config_file
|
|
|
|
try:
|
|
|
|
# Un-pickle the file and update the config dictionary
|
|
|
|
log.debug("Opening pickled file for load..")
|
|
|
|
pkl_file = open(filename, "rb")
|
|
|
|
filedump = pickle.load(pkl_file)
|
|
|
|
self.config.update(filedump)
|
|
|
|
pkl_file.close()
|
|
|
|
except IOError:
|
|
|
|
log.warning("IOError: Unable to load file '%s'", filename)
|
|
|
|
except EOFError:
|
|
|
|
log.debug("Closing pickled file..")
|
|
|
|
pkl_file.close()
|
|
|
|
|
|
|
|
def save(self, filename=None):
|
2007-08-26 14:34:50 +00:00
|
|
|
"""Save configuration to either 'filename' or the filename set during
|
|
|
|
construction of this object."""
|
2007-07-13 01:34:18 +00:00
|
|
|
# Saves the config dictionary
|
|
|
|
if filename is None:
|
|
|
|
filename = self.config_file
|
|
|
|
try:
|
|
|
|
log.debug("Opening pickled file for save..")
|
|
|
|
pkl_file = open(filename, "wb")
|
|
|
|
pickle.dump(self.config, pkl_file)
|
|
|
|
log.debug("Closing pickled file..")
|
|
|
|
pkl_file.close()
|
|
|
|
except IOError:
|
|
|
|
log.warning("IOError: Unable to save file '%s'", filename)
|
|
|
|
|
|
|
|
def set(self, key, value):
|
2007-08-26 14:34:50 +00:00
|
|
|
"""Set the 'key' with 'value'."""
|
2007-07-13 01:34:18 +00:00
|
|
|
# Sets the "key" with "value" in the config dict
|
|
|
|
log.debug("Setting '%s' to %s", key, value)
|
|
|
|
self.config[key] = value
|
2007-07-25 00:43:37 +00:00
|
|
|
# Whenever something is set, we should save
|
|
|
|
self.save()
|
2007-07-05 19:35:59 +00:00
|
|
|
|
2007-07-13 01:34:18 +00:00
|
|
|
def get(self, key):
|
2007-08-26 14:34:50 +00:00
|
|
|
"""Get the value of 'key'. If it is an invalid key then get() will
|
|
|
|
return None."""
|
2007-07-13 01:34:18 +00:00
|
|
|
# Attempts to get the "key" value and returns None if the key is
|
|
|
|
# invalid
|
|
|
|
try:
|
|
|
|
value = self.config[key]
|
|
|
|
log.debug("Getting '%s' as %s", key, value)
|
|
|
|
return value
|
|
|
|
except KeyError:
|
|
|
|
log.warning("Key does not exist, returning None")
|
2007-08-26 14:34:50 +00:00
|
|
|
return None
|
2007-07-09 02:50:20 +00:00
|
|
|
|
2007-09-14 11:45:42 +00:00
|
|
|
def get_config(self):
|
|
|
|
"""Returns the entire configuration as a dictionary."""
|
|
|
|
return self.config
|
|
|
|
|
2007-07-13 01:34:18 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.config[key]
|
2007-07-09 02:50:20 +00:00
|
|
|
|
2007-07-13 01:34:18 +00:00
|
|
|
def __setitem__(self, key, value):
|
2007-07-25 00:43:37 +00:00
|
|
|
self.set(key, value)
|