Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
782baec5
Commit
782baec5
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: New functions in settings module, primarily to make testing easier
parent
a37e0499
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/settings.py
+39
-10
39 additions, 10 deletions
fsl/utils/settings.py
with
39 additions
and
10 deletions
fsl/utils/settings.py
+
39
−
10
View file @
782baec5
...
...
@@ -25,6 +25,16 @@ the following functions can be called at the module-level:
Settings.clear
Some functions are also available to replace the module-level :class:`Settings`
instance:
.. autosummary::
:nosignatures:
set
use
These functions will have no effect before :func:`initialise` is called.
Two types of configuration data are available:
...
...
@@ -67,15 +77,9 @@ storing configuration files.
"""
def
initialise
(
*
args
,
**
kwargs
):
"""
Initialise the ``settings`` module. This function creates a
:class:`Settings` instance, and enables the module-level
functions. All settings are passed through to :meth:`Settings.__init__`.
"""
mod
=
sys
.
modules
[
__name__
]
settings
=
Settings
(
*
args
,
**
kwargs
)
def
set
(
settings
):
"""
Set the module-level :class:`Settings` instance.
"""
mod
=
sys
.
modules
[
__name__
]
mod
.
settings
=
settings
mod
.
read
=
settings
.
read
mod
.
write
=
settings
.
write
...
...
@@ -89,6 +93,31 @@ def initialise(*args, **kwargs):
mod
.
clear
=
settings
.
clear
def
initialise
(
*
args
,
**
kwargs
):
"""
Initialise the ``settings`` module. This function creates a
:class:`Settings` instance, and enables the module-level
functions. All settings are passed through to :meth:`Settings.__init__`.
"""
set
(
Settings
(
*
args
,
**
kwargs
))
@contextlib.contextmanager
def
use
(
settings
):
"""
Temporarily replace the module-level :class:`Settings` object
with the given one.
"""
mod
=
sys
.
modules
[
__name__
]
old
=
getattr
(
mod
,
'
settings
'
,
None
)
try
:
set
(
settings
)
yield
finally
:
if
old
is
not
None
:
set
(
old
)
# These are all overwritten by
# the initialise function.
def
read
(
name
,
default
=
None
):
...
...
@@ -391,7 +420,7 @@ class Settings(object):
try
:
with
open
(
configFile
,
'
wb
'
)
as
f
:
pickle
.
dump
(
config
,
f
,
protocol
=
2
)
except
(
IOError
,
pickle
.
PicklingError
,
EOFError
):
except
(
FileNotFoundError
,
IOError
,
pickle
.
PicklingError
,
EOFError
):
log
.
warning
(
'
Unable to save {} configuration file
'
'
{}
'
.
format
(
self
.
__configID
,
configFile
),
exc_info
=
True
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment