Show More
@@ -1,57 +1,58 b'' | |||
|
1 | 1 | """ This module contains classes - NamedFileInTemporaryDirectory, TemporaryWorkingDirectory. |
|
2 | 2 | |
|
3 | 3 | These classes add extra features such as creating a named file in temporary directory and |
|
4 | 4 | creating a context manager for the working directory which is also temporary. |
|
5 | 5 | """ |
|
6 | 6 | |
|
7 | 7 | import os as _os |
|
8 | from pathlib import Path | |
|
8 | 9 | from tempfile import TemporaryDirectory |
|
9 | 10 | |
|
10 | 11 | |
|
11 | 12 | class NamedFileInTemporaryDirectory(object): |
|
12 | 13 | |
|
13 | 14 | def __init__(self, filename, mode='w+b', bufsize=-1, **kwds): |
|
14 | 15 | """ |
|
15 | 16 | Open a file named `filename` in a temporary directory. |
|
16 | 17 | |
|
17 | 18 | This context manager is preferred over `NamedTemporaryFile` in |
|
18 | 19 | stdlib `tempfile` when one needs to reopen the file. |
|
19 | 20 | |
|
20 | 21 | Arguments `mode` and `bufsize` are passed to `open`. |
|
21 | 22 | Rest of the arguments are passed to `TemporaryDirectory`. |
|
22 | 23 | |
|
23 | 24 | """ |
|
24 | 25 | self._tmpdir = TemporaryDirectory(**kwds) |
|
25 |
path = |
|
|
26 | path = Path(self._tmpdir.name) / filename | |
|
26 | 27 | self.file = open(path, mode, bufsize) |
|
27 | 28 | |
|
28 | 29 | def cleanup(self): |
|
29 | 30 | self.file.close() |
|
30 | 31 | self._tmpdir.cleanup() |
|
31 | 32 | |
|
32 | 33 | __del__ = cleanup |
|
33 | 34 | |
|
34 | 35 | def __enter__(self): |
|
35 | 36 | return self.file |
|
36 | 37 | |
|
37 | 38 | def __exit__(self, type, value, traceback): |
|
38 | 39 | self.cleanup() |
|
39 | 40 | |
|
40 | 41 | |
|
41 | 42 | class TemporaryWorkingDirectory(TemporaryDirectory): |
|
42 | 43 | """ |
|
43 | 44 | Creates a temporary directory and sets the cwd to that directory. |
|
44 | 45 | Automatically reverts to previous cwd upon cleanup. |
|
45 | 46 | Usage example: |
|
46 | 47 | |
|
47 | 48 | with TemporaryWorkingDirectory() as tmpdir: |
|
48 | 49 | ... |
|
49 | 50 | """ |
|
50 | 51 | def __enter__(self): |
|
51 |
self.old_wd = |
|
|
52 | self.old_wd = Path.cwd() | |
|
52 | 53 | _os.chdir(self.name) |
|
53 | 54 | return super(TemporaryWorkingDirectory, self).__enter__() |
|
54 | 55 | |
|
55 | 56 | def __exit__(self, exc, value, tb): |
|
56 | 57 | _os.chdir(self.old_wd) |
|
57 | 58 | return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb) |
@@ -1,28 +1,29 b'' | |||
|
1 | 1 | #----------------------------------------------------------------------------- |
|
2 | 2 | # Copyright (C) 2012- The IPython Development Team |
|
3 | 3 | # |
|
4 | 4 | # Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | # the file COPYING, distributed as part of this software. |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | import os | |
|
8 | from pathlib import Path | |
|
9 | 9 | |
|
10 | 10 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
11 | 11 | from IPython.utils.tempdir import TemporaryWorkingDirectory |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | def test_named_file_in_temporary_directory(): |
|
15 | 15 | with NamedFileInTemporaryDirectory('filename') as file: |
|
16 | 16 | name = file.name |
|
17 | 17 | assert not file.closed |
|
18 |
assert |
|
|
18 | assert Path(name).exists() | |
|
19 | 19 | file.write(b'test') |
|
20 | 20 | assert file.closed |
|
21 |
assert not |
|
|
21 | assert not Path(name).exists() | |
|
22 | 22 | |
|
23 | 23 | def test_temporary_working_directory(): |
|
24 | with TemporaryWorkingDirectory() as dir: | |
|
25 | assert os.path.exists(dir) | |
|
26 | assert os.path.realpath(os.curdir) == os.path.realpath(dir) | |
|
27 | assert not os.path.exists(dir) | |
|
28 | assert os.path.abspath(os.curdir) != dir | |
|
24 | with TemporaryWorkingDirectory() as directory: | |
|
25 | directory_path = Path(directory) | |
|
26 | assert directory_path.exists() | |
|
27 | assert Path.cwd() == directory_path.resolve() | |
|
28 | assert not directory_path.exists() | |
|
29 | assert Path.cwd() != directory_path.resolve() |
General Comments 0
You need to be logged in to leave comments.
Login now