Skip to content

Commit c120ee5

Browse files
committed
fix: address @effigies' review comments
1 parent a62a369 commit c120ee5

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

nipype/utils/filemanip.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ def __init__(self, path):
5757
from pathlib2 import Path
5858
USING_PATHLIB2 = True
5959

60-
try:
60+
try: # PY35 - strict mode was added in 3.6
6161
Path('/invented/file/path').resolve(strict=True)
6262
except TypeError:
63-
from tempfile import gettempdir
64-
6563
def _patch_resolve(self, strict=False):
6664
"""Add the argument strict to signature in Python>3,<3.6."""
6765
resolved = Path().old_resolve() / self
@@ -72,28 +70,10 @@ def _patch_resolve(self, strict=False):
7270

7371
Path.old_resolve = Path.resolve
7472
Path.resolve = _patch_resolve
75-
76-
if not hasattr(Path, 'write_text'):
77-
def _write_text(self, text):
78-
with open(str(self), 'w') as f:
79-
f.write(text)
80-
Path.write_text = _write_text
81-
82-
try:
83-
with tempfile.TemporaryDirectory() as tmpdir:
84-
(Path(tmpdir) / 'exist_ok_test').mkdir(exist_ok=True)
85-
except TypeError:
86-
def _mkdir(self, mode=0o777, parents=False, exist_ok=False):
87-
if not exist_ok and self.exists():
88-
raise FileExistsError(str(self))
89-
if not parents and not Path(str(self.parents)).exists():
90-
raise FileNotFoundError(str(self.parents))
91-
os.makedirs(str(self), mode=mode, exist_ok=exist_ok)
92-
Path.mkdir = _mkdir
93-
9473
except FileNotFoundError:
9574
pass
9675
except OSError:
76+
# PY2
9777
def _patch_resolve(self, strict=False):
9878
"""Raise FileNotFoundError instead of OSError with pathlib2."""
9979
try:
@@ -106,6 +86,27 @@ def _patch_resolve(self, strict=False):
10686
Path.old_resolve = Path.resolve
10787
Path.resolve = _patch_resolve
10888

89+
if not hasattr(Path, 'write_text'):
90+
# PY34 - Path does not have write_text
91+
def _write_text(self, text):
92+
with open(str(self), 'w') as f:
93+
f.write(text)
94+
Path.write_text = _write_text
95+
96+
if PY3:
97+
try: # PY34 - mkdir does not have exist_ok
98+
from tempfile import TemporaryDirectory
99+
with TemporaryDirectory() as tmpdir:
100+
(Path(tmpdir) / 'exist_ok_test').mkdir(exist_ok=True)
101+
except TypeError:
102+
def _mkdir(self, mode=0o777, parents=False, exist_ok=False):
103+
if parents:
104+
os.makedirs(str(self), mode=mode, exist_ok=exist_ok)
105+
elif not exist_ok or not self.exists():
106+
os.mkdir(str(self), mode=mode)
107+
108+
Path.mkdir = _mkdir
109+
109110

110111
def split_filename(fname):
111112
"""Split a filename into parts: path, base filename and extension.

nipype/utils/tests/test_filemanip.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,6 @@ def test_pickle(tmp_path, save_versioning):
601601
def test_Path(tmpdir):
602602
tmp_path = Path(tmpdir.strpath)
603603

604-
assert hasattr(tmp_path, 'write_text')
605-
606604
(tmp_path / 'textfile').write_text('some text')
607605

608606
with pytest.raises(OSError):

0 commit comments

Comments
 (0)