| 13 | |
| 14 | |
| 15 | class NamedFileInTemporaryDirectory: |
| 16 | def __init__(self, filename: str, mode: str, bufsize: int=-1, add_to_syspath: bool=False, **kwds): |
| 17 | """ |
| 18 | Open a file named `filename` in a temporary directory. |
| 19 | |
| 20 | This context manager is preferred over `NamedTemporaryFile` in |
| 21 | stdlib `tempfile` when one needs to reopen the file. |
| 22 | |
| 23 | Arguments `mode` and `bufsize` are passed to `open`. |
| 24 | Rest of the arguments are passed to `TemporaryDirectory`. |
| 25 | |
| 26 | """ |
| 27 | self._tmpdir = TemporaryDirectory(**kwds) |
| 28 | path = Path(self._tmpdir.name) / filename |
| 29 | encoding = None if "b" in mode else "utf-8" |
| 30 | self.file = open(path, mode, bufsize, encoding=encoding) |
| 31 | |
| 32 | def cleanup(self): |
| 33 | self.file.close() |
| 34 | self._tmpdir.cleanup() |
| 35 | |
| 36 | __del__ = cleanup |
| 37 | |
| 38 | def __enter__(self) -> BufferedWriter: |
| 39 | return self.file |
| 40 | |
| 41 | def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]): |
| 42 | self.cleanup() |
| 43 | |
| 44 | |
| 45 | class TemporaryWorkingDirectory(TemporaryDirectory): |
no outgoing calls
searching dependent graphs…