File: dict.py

package info (click to toggle)
plover 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 14,356 kB
  • sloc: python: 21,589; sh: 682; ansic: 25; makefile: 11
file content (20 lines) | stat: -rw-r--r-- 506 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from contextlib import contextmanager
from pathlib import Path
import os
import tempfile


@contextmanager
def make_dict(tmp_path, contents, extension=None, name=None):
    kwargs = {"dir": str(tmp_path)}
    if name is not None:
        kwargs["prefix"] = name + "_"
    if extension is not None:
        kwargs["suffix"] = "." + extension
    fd, path = tempfile.mkstemp(**kwargs)
    try:
        os.write(fd, contents)
        os.close(fd)
        yield Path(path)
    finally:
        os.unlink(path)