File tree Expand file tree Collapse file tree 2 files changed +32
-3
lines changed
tensorflow_datasets/core/utils Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -308,17 +308,32 @@ def _tmp_file_prefix() -> str:
308
308
return f'{ constants .INCOMPLETE_PREFIX } { uuid .uuid4 ().hex } '
309
309
310
310
311
- def _tmp_file_name (path : epath .PathLike ) -> epath .Path :
311
+ def _tmp_file_name (
312
+ path : epath .PathLike ,
313
+ subfolder : str | None = None ,
314
+ ) -> epath .Path :
315
+ """Returns the temporary file name for the given path.
316
+
317
+ Args:
318
+ path: The path to the file.
319
+ subfolder: The subfolder to use. If None, then the parent of the path will
320
+ be used.
321
+ """
312
322
path = epath .Path (path )
313
- return path .parent / f'{ _tmp_file_prefix ()} .{ path .name } '
323
+ file_name = f'{ _tmp_file_prefix ()} .{ path .name } '
324
+ if subfolder :
325
+ return path .parent / subfolder / file_name
326
+ else :
327
+ return path .parent / file_name
314
328
315
329
316
330
@contextlib .contextmanager
317
331
def incomplete_file (
318
332
path : epath .Path ,
333
+ subfolder : str | None = None ,
319
334
) -> Iterator [epath .Path ]:
320
335
"""Writes to path atomically, by writing to temp file and renaming it."""
321
- tmp_path = _tmp_file_name (path )
336
+ tmp_path = _tmp_file_name (path , subfolder = subfolder )
322
337
try :
323
338
yield tmp_path
324
339
tmp_path .replace (path )
Original file line number Diff line number Diff line change 14
14
# limitations under the License.
15
15
16
16
import collections
17
+ import os
17
18
import pathlib
19
+ from unittest import mock
18
20
19
21
from etils import epath
20
22
import pytest
@@ -370,5 +372,17 @@ def test_make_valid_name(name: str, expected: str):
370
372
assert py_utils .make_valid_name (name ) == expected
371
373
372
374
375
+ @pytest .mark .parametrize (
376
+ ['path' , 'subfolder' , 'expected' ],
377
+ [
378
+ ('/a/file.ext' , None , '/a/foobar.file.ext' ),
379
+ ('/a/file.ext' , 'sub' , '/a/sub/foobar.file.ext' ),
380
+ ],
381
+ )
382
+ def test_tmp_file_name (path , subfolder , expected ):
383
+ with mock .patch .object (py_utils , '_tmp_file_prefix' , return_value = 'foobar' ):
384
+ assert os .fspath (py_utils ._tmp_file_name (path , subfolder )) == expected
385
+
386
+
373
387
if __name__ == '__main__' :
374
388
tf .test .main ()
You can’t perform that action at this time.
0 commit comments