-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For re-use by coherent.test to save ruff config. Ref coherent-oss/coherent.test#14
- Loading branch information
Showing
1 changed file
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
import contextlib | ||
import pathlib | ||
from typing import Callable, ContextManager | ||
|
||
from .compat.py311 import importlib | ||
|
||
|
||
def write_pyproject(target: pathlib.Path = pathlib.Path()) -> ContextManager[None]: | ||
return assured( | ||
target / 'pyproject.toml', | ||
importlib.resources.files().joinpath('system.toml').read_text, | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def write_pyproject(target: pathlib.Path = pathlib.Path()): | ||
path = target / 'pyproject.toml' | ||
if path.exists(): | ||
def assured( | ||
target: pathlib.Path, content_factory: Callable[[], str] | ||
) -> ContextManager[None]: | ||
""" | ||
Yield with target existing on the file system. | ||
If target does not already exist, it is created with the contents | ||
as supplied by ``content_factory()`` and deleted on exit. | ||
""" | ||
if target.exists(): | ||
yield | ||
return | ||
path.write_text(importlib.resources.files().joinpath('system.toml').read_text()) | ||
target.write_text(content_factory()) | ||
try: | ||
yield | ||
finally: | ||
path.unlink() | ||
target.unlink() |