deluge.config

Deluge Config Module

This module is used for loading and saving of configuration files.. or anything really.

The format of the config file is as follows:

<format version as int> <config file version as int> <content>

The format version is controlled by the Config class. It should only be changed when anything below it is changed directly by the Config class. An example of this would be if we changed the serializer for the content to something different.

The config file version is changed by the ‘owner’ of the config file. This is to signify that there is a change in the naming of some config keys or something similar along those lines.

The content is simply the dict to be saved and will be serialized before being written.

Converting

Since the format of the config could change, there needs to be a way to have the Config object convert to newer formats. To do this, you will need to register conversion functions for various versions of the config file. Note that this can only be done for the ‘config file version’ and not for the ‘format’ version as this will be done internally.

class deluge.config.Config(filename, defaults=None, config_dir=None)

Bases: object

This class is used to access/create/modify config files

Parameters:
  • filename – the name of the config file
  • defaults – dictionary of default values
  • config_dir – the path to the config directory
__setitem__(key, value)
See set_item()
__getitem__(key)
See get_item()
apply_all()

Calls all set functions

Usage

>>> config = Config("test.conf", defaults={"test": 5})
>>> def cb(key, value):
...     print key, value
...
>>> config.register_set_function("test", cb, apply_now=False)
>>> config.apply_all()
test 5
apply_set_functions(key)

Calls set functions for :param:key.

Parameter:key – str, the config key
config
The config dictionary
config_file
get_item(key)

Gets the value of item ‘key’

Parameter:key – the item for which you want it’s value
Returns:the value of item ‘key’
Raises KeyError:
 if ‘key’ is not in the config dictionary

Usage

>>> config = Config("test.conf", defaults={"test": 5})
>>> config["test"]
5
load(filename=None)

Load a config file

Parameter:filename – if None, uses filename set in object initialization
register_change_callback(callback)

Registers a callback function that will be called when a value is changed in the config dictionary

Parameter:callback – the function, callback(key, value)

Usage

>>> config = Config("test.conf", defaults={"test": 5})
>>> def cb(key, value):
...     print key, value
...
>>> config.register_change_callback(cb)
register_set_function(key, function, apply_now=True)

Register a function to be called when a config value changes

Parameters:
  • key – the item to monitor for change
  • function – the function to call when the value changes, f(key, value)
  • apply_now – if True, the function will be called after it’s registered

Usage

>>> config = Config("test.conf", defaults={"test": 5})
>>> def cb(key, value):
...     print key, value
...
>>> config.register_set_function("test", cb, apply_now=True)
test 5
run_converter(input_range, output_version, func)

Runs a function that will convert file versions in the :param:input_range to the :param:output_version.

Parameters:
  • input_range – tuple, (int, int) the range of input versions this function will accept
  • output_version – int, the version this function will return
  • func – func, the function that will do the conversion, it will take the config dict as an argument and return the augmented dict
Raises ValueError:
 

if the output_version is less than the input_range

save(filename=None)

Save configuration to disk

Parameter:filename – if None, uses filename set in object initiliazation
Rtype bool:
Returns:whether or not the save succeeded.
set_item(key, value)

Sets item ‘key’ to ‘value’ in the config dictionary, but does not allow changing the item’s type unless it is None

Parameters:
  • key – string, item to change to change
  • value – the value to change item to, must be same type as what is currently in the config
Raises ValueError:
 

raised when the type of value is not the same as what is currently in the config

Usage

>>> config = Config("test.conf")
>>> config["test"] = 5
>>> config["test"]
5

Previous topic

deluge.common

This Page