File: dict.py

package info (click to toggle)
plover 4.0.0~dev10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,644 kB
  • sloc: python: 17,030; sh: 664; ansic: 25; makefile: 19
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)