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.
Bases: object
This class is used to access/create/modify config files
Parameters: |
|
---|
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
Calls set functions for :param:key.
Parameter: | key – str, the config 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 a config file
Parameter: | filename – if None, uses filename set in object initialization |
---|
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 a function to be called when a config value changes
Parameters: |
|
---|
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
Runs a function that will convert file versions in the :param:input_range to the :param:output_version.
Parameters: |
|
---|---|
Raises ValueError: | |
if the output_version is less than the input_range |
Save configuration to disk
Parameter: | filename – if None, uses filename set in object initiliazation |
---|---|
Rtype bool: | |
Returns: | whether or not the save succeeded. |
Sets item ‘key’ to ‘value’ in the config dictionary, but does not allow changing the item’s type unless it is None
Parameters: |
|
---|---|
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