Commit 40ce897 1 parent b69f1d1 commit 40ce897 Copy full SHA for 40ce897
File tree 1 file changed +20
-5
lines changed
1 file changed +20
-5
lines changed Original file line number Diff line number Diff line change 1
1
import contextlib
2
2
import pathlib
3
+ from typing import Callable , ContextManager
3
4
4
5
from .compat .py311 import importlib
5
6
6
7
8
+ def write_pyproject (target : pathlib .Path = pathlib .Path ()) -> ContextManager [None ]:
9
+ return assured (
10
+ target / 'pyproject.toml' ,
11
+ importlib .resources .files ().joinpath ('system.toml' ).read_text ,
12
+ )
13
+
14
+
7
15
@contextlib .contextmanager
8
- def write_pyproject (target : pathlib .Path = pathlib .Path ()):
9
- path = target / 'pyproject.toml'
10
- if path .exists ():
16
+ def assured (
17
+ target : pathlib .Path , content_factory : Callable [[], str ]
18
+ ) -> ContextManager [None ]:
19
+ """
20
+ Yield with target existing on the file system.
21
+
22
+ If target does not already exist, it is created with the contents
23
+ as supplied by ``content_factory()`` and deleted on exit.
24
+ """
25
+ if target .exists ():
11
26
yield
12
27
return
13
- path .write_text (importlib . resources . files (). joinpath ( 'system.toml' ). read_text ())
28
+ target .write_text (content_factory ())
14
29
try :
15
30
yield
16
31
finally :
17
- path .unlink ()
32
+ target .unlink ()
You can’t perform that action at this time.
0 commit comments