Skip to content

Commit 5e1b33d

Browse files
more unify
1 parent b25480f commit 5e1b33d

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

mne_bids/_fileio.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,61 @@
1313
from mne.utils import _soft_import, warn
1414

1515

16-
@contextlib.contextmanager
17-
def _mne_open_lock(path, *args, **kwargs):
18-
"""
19-
Context manager that opens a file with an optional file lock.
20-
21-
If the `filelock` package is available, a lock is acquired on a lock file
22-
based on the given path (by appending '.lock').
23-
24-
Otherwise, a null context is used. The path is then opened in the
25-
specified mode.
26-
27-
Parameters
28-
----------
29-
path : str
30-
The path to the file to be opened.
31-
*args, **kwargs : optional
32-
Additional arguments and keyword arguments to be passed to the
33-
`open` function.
34-
35-
"""
16+
def _get_lock_context(path):
17+
"""Return a context manager that locks ``path`` if possible."""
3618
filelock = _soft_import(
3719
"filelock", purpose="parallel config set and get", strict=False
3820
)
3921

40-
lock_context = contextlib.nullcontext() # default to no lock
22+
lock_context = contextlib.nullcontext()
23+
lock_path = Path(f"{os.fspath(path)}.lock")
24+
have_lock = False
4125

4226
if filelock:
43-
lock_path = f"{path}.lock"
4427
try:
4528
lock_context = filelock.FileLock(lock_path, timeout=5)
4629
lock_context.acquire()
30+
have_lock = True
4731
except TimeoutError:
4832
warn(
4933
"Could not acquire lock file after 5 seconds, consider deleting it "
5034
f"if you know the corresponding file is usable:\n{lock_path}"
5135
)
5236
lock_context = contextlib.nullcontext()
37+
except OSError:
38+
warn(
39+
"Could not create lock file due to insufficient permissions. "
40+
"Proceeding without a lock."
41+
)
42+
lock_context = contextlib.nullcontext()
5343

54-
with lock_context, open(path, *args, **kwargs) as fid:
55-
yield fid
44+
return lock_context, lock_path, have_lock
5645

5746

5847
@contextmanager
5948
def _open_lock(path, *args, **kwargs):
6049
"""Wrap :func:`mne.utils.config._open_lock` and remove stale ``.lock`` files."""
61-
lock_path = Path(f"{os.fspath(path)}.lock")
50+
lock_context, lock_path, have_lock = _get_lock_context(path)
6251
try:
63-
with _mne_open_lock(path, *args, **kwargs) as fid:
52+
with lock_context, open(path, *args, **kwargs) as fid:
6453
yield fid
6554
finally:
66-
if lock_path.exists():
55+
if have_lock and lock_path.exists():
56+
try:
57+
lock_path.unlink()
58+
except OSError:
59+
pass
60+
61+
62+
@contextmanager
63+
def _file_lock(path):
64+
"""Acquire a lock on ``path`` without opening the file."""
65+
lock_context, lock_path, have_lock = _get_lock_context(path)
66+
try:
67+
with lock_context:
68+
yield
69+
finally:
70+
if have_lock and lock_path.exists():
6771
try:
6872
lock_path.unlink()
6973
except OSError:

mne_bids/tsv_handler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ def _to_tsv(data, fname):
184184
Ordered dictionary containing data to be written to a tsv file.
185185
fname : str
186186
Path to the file being written.
187-
188187
"""
189188
n_rows = len(data[list(data.keys())[0]])
190189
output = _tsv_to_str(data, n_rows)

0 commit comments

Comments
 (0)