mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-97930: Apply changes from importlib_resources 5.10. (GH-100598)
This commit is contained in:
parent
ba1342ce99
commit
447d061bc7
9 changed files with 268 additions and 90 deletions
50
Lib/test/test_importlib/resources/_path.py
Normal file
50
Lib/test/test_importlib/resources/_path.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import pathlib
|
||||
import functools
|
||||
|
||||
|
||||
####
|
||||
# from jaraco.path 3.4
|
||||
|
||||
|
||||
def build(spec, prefix=pathlib.Path()):
|
||||
"""
|
||||
Build a set of files/directories, as described by the spec.
|
||||
|
||||
Each key represents a pathname, and the value represents
|
||||
the content. Content may be a nested directory.
|
||||
|
||||
>>> spec = {
|
||||
... 'README.txt': "A README file",
|
||||
... "foo": {
|
||||
... "__init__.py": "",
|
||||
... "bar": {
|
||||
... "__init__.py": "",
|
||||
... },
|
||||
... "baz.py": "# Some code",
|
||||
... }
|
||||
... }
|
||||
>>> tmpdir = getfixture('tmpdir')
|
||||
>>> build(spec, tmpdir)
|
||||
"""
|
||||
for name, contents in spec.items():
|
||||
create(contents, pathlib.Path(prefix) / name)
|
||||
|
||||
|
||||
@functools.singledispatch
|
||||
def create(content, path):
|
||||
path.mkdir(exist_ok=True)
|
||||
build(content, prefix=path) # type: ignore
|
||||
|
||||
|
||||
@create.register
|
||||
def _(content: bytes, path):
|
||||
path.write_bytes(content)
|
||||
|
||||
|
||||
@create.register
|
||||
def _(content: str, path):
|
||||
path.write_text(content)
|
||||
|
||||
|
||||
# end from jaraco.path
|
||||
####
|
Loading…
Add table
Add a link
Reference in a new issue