Add command-line option for the daemon to restrict some config keys to being read-only.

This only affects the core.set_config() RPC method which will drop items if the key
is listed as read-only.
This commit is contained in:
Andrew Resch 2016-02-02 19:02:28 -08:00
parent 77f8449c0c
commit 90a22af5e5
4 changed files with 31 additions and 2 deletions

View File

@ -72,10 +72,15 @@ from deluge.core.eventmanager import EventManager
from deluge.core.rpcserver import export
class Core(component.Component):
def __init__(self, listen_interface=None):
def __init__(self, listen_interface=None, read_only_config_keys=None):
log.debug("Core init..")
component.Component.__init__(self, "Core")
# These keys will be dropped from the set_config() RPC and are
# configurable from the command-line.
self.read_only_config_keys = read_only_config_keys
log.debug("read_only_config_keys: %s", read_only_config_keys)
# Start the libtorrent session
log.info("Starting libtorrent %s session..", lt.version)
@ -502,6 +507,8 @@ class Core(component.Component):
"""Set the config with values from dictionary"""
# Load all the values into the configuration
for key in config.keys():
if self.read_only_config_keys and key in self.read_only_config_keys:
continue
if isinstance(config[key], unicode) or isinstance(config[key], str):
config[key] = config[key].encode("utf8")
self.config[key] = config[key]

View File

@ -133,9 +133,15 @@ class Daemon(object):
else:
listen_interface = ""
if options and options.read_only_config_keys:
read_only_config_keys = options.read_only_config_keys.split(",")
else:
read_only_config_keys = []
from deluge.core.core import Core
# Start the core as a thread and join it until it's done
self.core = Core(listen_interface=listen_interface)
self.core = Core(listen_interface=listen_interface,
read_only_config_keys=read_only_config_keys)
port = self.core.config["daemon_port"]
if options and options.port:

View File

@ -169,6 +169,9 @@ this should be an IP address", metavar="IFACE",
help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False)
parser.add_option("--profile", dest="profile", action="store_true", default=False,
help="Profiles the daemon")
parser.add_option("--read-only-config-keys", dest="read_only_config_keys",
help="List of comma-separated config keys that will not be modified by set_config RPC.",
action="store", type="str")
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()

View File

@ -13,6 +13,7 @@ from deluge.core.core import Core
import deluge.component as component
import deluge.error
class CoreTestCase(unittest.TestCase):
def setUp(self):
common.set_tmp_config_dir()
@ -148,3 +149,15 @@ class CoreTestCase(unittest.TestCase):
for key in pathlist:
self.assertEquals(deluge.core.torrent.sanitize_filepath(key, folder=False), pathlist[key])
self.assertEquals(deluge.core.torrent.sanitize_filepath(key, folder=True), pathlist[key] + '/')
def test_read_only_config_keys(self):
key = 'max_upload_speed'
self.core.read_only_config_keys = [key]
old_value = self.core.get_config_value(key)
self.core.set_config({key: old_value + 10})
new_value = self.core.get_config_value(key)
self.assertEquals(old_value, new_value)
self.core.read_only_config_keys = None