File: util.py

package info (click to toggle)
python-srsly 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,256 kB
  • sloc: python: 17,567; ansic: 1,434; sh: 12; makefile: 6
file content (16 lines) | stat: -rw-r--r-- 415 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tempfile
from pathlib import Path
from contextlib import contextmanager
import shutil


@contextmanager
def make_tempdir(files={}, mode="w"):
    temp_dir_str = tempfile.mkdtemp()
    temp_dir = Path(temp_dir_str)
    for name, content in files.items():
        path = temp_dir / name
        with path.open(mode) as file_:
            file_.write(content)
    yield temp_dir
    shutil.rmtree(temp_dir_str)