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:
Reads a setting with the given |
|
Writes the given |
|
Delete the setting with the given |
|
Reads and returns the contents of the given file |
|
Write to the given file |
|
Deletes the given file |
|
Converts the given |
|
Returns all settings with names that match the given glob-style pattern. |
|
Returns a list of all stored settings files which match the given glob-style pattern. |
|
Delete all configuration settings and files. |
Some functions are also available to replace the module-level Settings
instance:
Set the module-level |
|
Temporarily replace the module-level |
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
,write
anddelete
functions. 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
, anddeleteFile
functions.
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)[source]¶ Initialise the
settings
module. This function creates aSettings
instance, and enables the module-level functions. All settings are passed through toSettings.__init__()
.
-
fsl.utils.settings.
use
(settings)[source]¶ Temporarily replace the module-level
Settings
object with the given one.
-
class
fsl.utils.settings.
Settings
(cfgid='fslpy', cfgdir=None, writeOnExit=True)[source]¶ Bases:
object
The
Settings
class contains all of the logic provided by thesettings
module. It is not meant to be instantiated directly (although you may do so if you wish).-
property
configID
¶ Returns the configuration identifier.
-
property
configDir
¶ Returns the location of the configuration directory.
-
read
(name, default=None)[source]¶ Reads a setting with the given
name
, returndefault
if there is no setting calledname
.
-
readFile
(path, mode='t')[source]¶ Reads and returns the contents of the given file
path
. ReturnsNone
if the path does not exist.- Parameters
mode –
't'
for text mode, or'b'
for binary.
-
writeFile
(path, mode='t')[source]¶ 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
writeFile
has the advantage that any intermediate directories will be created if they don’t already exist.
-
filePath
(path)[source]¶ Converts the given
path
to an absolute path. Note that there is no guarantee that the returned file path (or its containing directory) exists.
-
readAll
(pattern=None)[source]¶ Returns all settings with names that match the given glob-style pattern.
-
property