fsl.utils.settings¶
This module provides functions for storing and retrieving persistent configuration settings and data files.
The initialise() function must be called to initialise the module. Then,
the following functions can be called at the module-level:
Settings.read |
Reads a setting with the given name, return default if there is no setting called name. |
Settings.write |
Writes the given value to the given file path. |
Settings.delete |
Delete the setting with the given name. |
Settings.readFile |
Reads and returns the contents of the given file path. |
Settings.writeFile |
Write to the given file path. |
Settings.deleteFile |
Deletes the given file path. |
Settings.filePath |
Converts the given path to an absolute path. |
Settings.readAll |
Returns all settings with names that match the given glob-style pattern. |
Settings.listFiles |
Returns a list of all stored settings files which match the given glob-style pattern. |
Settings.clear |
Delete all configuration settings and files. |
Some functions are also available to replace the module-level Settings
instance:
set |
Set the module-level Settings instance. |
use |
Temporarily replace the module-level Settings object with the given one. |
These functions will have no effect before initialise() is called.
Two types of configuration data are available:
- Key-value pairs - access these via the
read,writeanddeletefunctions. These are stored in a single file, viapickle. Anything that can be pickled can be stored.- Separate files, either text or binary. Access these via the
readFile,writeFile, anddeleteFilefunctions.
Both of the above data types will be stored in a configuration directory. The location of this directory differs from platform to platform, but is likely to be either ~/.fslpy/ or ~/.config/fslpy/.
-
fsl.utils.settings.initialise(*args, **kwargs)¶ Initialise the
settingsmodule. This function creates aSettingsinstance, and enables the module-level functions. All settings are passed through toSettings.__init__().
-
fsl.utils.settings.use(settings)¶ Temporarily replace the module-level
Settingsobject with the given one.
-
fsl.utils.settings.read(name, default=None)¶
-
fsl.utils.settings.write(*args, **kwargs)¶
-
fsl.utils.settings.delete(*args, **kwargs)¶
-
fsl.utils.settings.readFile(*args, **kwargs)¶
-
fsl.utils.settings.writeFile(*args, **kwargs)¶
-
fsl.utils.settings.deleteFile(*args, **kwargs)¶
-
fsl.utils.settings.filePath(*args, **kwargs)¶
-
fsl.utils.settings.readAll(*args, **kwarg)¶
-
fsl.utils.settings.listFiles(*args, **kwarg)¶
-
fsl.utils.settings.clear(*args, **kwarg)¶
-
class
fsl.utils.settings.Settings(cfgid='fslpy', cfgdir=None, writeOnExit=True)¶ Bases:
objectThe
Settingsclass contains all of the logic provided by thesettingsmodule. It is not meant to be instantiated directly (although you may do so if you wish).-
configID¶ Returns the configuration identifier.
-
configDir¶ Returns the location of the configuration directory.
-
read(name, default=None)¶ Reads a setting with the given
name, returndefaultif there is no setting calledname.
-
write(name, value)¶ Writes the given
valueto the given filepath.
-
delete(name)¶ Delete the setting with the given
name.
-
readFile(path, mode='t')¶ Reads and returns the contents of the given file
path. ReturnsNoneif the path does not exist.Parameters: mode – 't'for text mode, or'b'for binary.
-
writeFile(path, mode='t')¶ Write to the given file
path. This function is intended to be used as a context manager. For example:with settings.writeFile('mydata.txt') as f: f.write('data\n')
An alternate method of writing to a file is via
filePath(), e.g.:fname = settings.filePath('mydata.txt') with open(fname, 'wt') as f: f.write('data\n')
However using
writeFilehas the advantage that any intermediate directories will be created if they don’t already exist.
-
deleteFile(path)¶ Deletes the given file
path.
-
filePath(path)¶ Converts the given
pathto an absolute path. Note that there is no guarantee that the returned file path (or its containing directory) exists.
-
readAll(pattern=None)¶ Returns all settings with names that match the given glob-style pattern.
-
listFiles(pattern=None)¶ Returns a list of all stored settings files which match the given glob-style pattern. If a pattern is not given, all files are returned.
-
clear()¶ Delete all configuration settings and files.
-
writeConfigFile()¶ Writes all settings to a file.
-