forked from projectfluent/python-fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (27 loc) · 944 Bytes
/
utils.py
File metadata and controls
35 lines (27 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Utilities for testing."""
import textwrap
from functools import wraps
from os import mkdir
from os.path import join
from tempfile import TemporaryDirectory
def dedent_ftl(text):
return textwrap.dedent(f"{text.rstrip()}\n")
def patch_files(tree: dict):
def build_file_tree(root: str, tree: dict) -> None:
for name, value in tree.items():
path = join(root, name)
if isinstance(value, str):
with open(path, "x", encoding="utf-8", newline="\n") as file:
if value:
file.write(value)
else:
mkdir(path)
build_file_tree(path, value)
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
with TemporaryDirectory() as root:
build_file_tree(root, tree)
return fn(*args, root, **kwargs)
return wrapper
return decorator