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
2e3e0aa4
Commit
2e3e0aa4
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Changed settings.writeFile behaviour so it is used as a context manager.
parent
96def00d
No related branches found
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
+30
-4
30 additions, 4 deletions
fsl/utils/settings.py
with
30 additions
and
4 deletions
fsl/utils/settings.py
+
30
−
4
View file @
2e3e0aa4
...
...
@@ -55,6 +55,7 @@ import logging
import
fnmatch
import
tempfile
import
platform
import
contextlib
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -181,6 +182,8 @@ class Settings(object):
def
readFile
(
self
,
path
,
mode
=
'
t
'
):
"""
Reads and returns the contents of the given file ``path``.
Returns ``None`` if the path does not exist.
:arg mode: ``
'
t
'
`` for text mode, or ``
'
b
'
`` for binary.
"""
mode
=
'
r
'
+
mode
...
...
@@ -193,8 +196,28 @@ class Settings(object):
return
None
def
writeFile
(
self
,
path
,
value
,
mode
=
'
t
'
):
"""
Writes the given ``value`` to the given file ``path``.
"""
@contextlib.contextmanager
def
writeFile
(
self
,
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 :meth:`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.
"""
mode
=
'
w
'
+
mode
path
=
self
.
filePath
(
path
)
...
...
@@ -204,7 +227,7 @@ class Settings(object):
os
.
makedirs
(
pathdir
)
with
open
(
path
,
mode
)
as
f
:
f
.
write
(
value
)
yield
f
def
deleteFile
(
self
,
path
):
...
...
@@ -216,7 +239,10 @@ class Settings(object):
def
filePath
(
self
,
path
):
"""
Converts the given ``path`` to an absolute path.
"""
"""
Converts the given ``path`` to an absolute path. Note that there
is no guarantee that the returned file path (or its containing
directory) exists.
"""
path
=
self
.
__fixPath
(
path
)
path
=
op
.
join
(
self
.
__configDir
,
path
)
...
...
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