]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/_vendor/importlib_resources/_legacy.py
7 from typing
import Union
, Iterable
, ContextManager
, BinaryIO
, TextIO
, Any
11 Package
= Union
[types
.ModuleType
, str]
16 @functools.wraps(func
)
17 def wrapper(*args
, **kwargs
):
19 f
"{func.__name__} is deprecated. Use files() instead. "
20 "Refer to https://importlib-resources.readthedocs.io"
21 "/en/latest/using.html#migrating-from-legacy for migration advice.",
25 return func(*args
, **kwargs
)
30 def normalize_path(path
: Any
) -> str:
31 """Normalize a path by ensuring it is a string.
33 If the resulting string contains path separators, an exception is raised.
36 parent
, file_name
= os
.path
.split(str_path
)
38 raise ValueError(f
'{path!r} must be only a file name')
43 def open_binary(package
: Package
, resource
: Resource
) -> BinaryIO
:
44 """Return a file-like object opened for binary reading of the resource."""
45 return (_common
.files(package
) / normalize_path(resource
)).open('rb')
49 def read_binary(package
: Package
, resource
: Resource
) -> bytes:
50 """Return the binary contents of the resource."""
51 return (_common
.files(package
) / normalize_path(resource
)).read_bytes()
58 encoding
: str = 'utf-8',
59 errors
: str = 'strict',
61 """Return a file-like object opened for text reading of the resource."""
62 return (_common
.files(package
) / normalize_path(resource
)).open(
63 'r', encoding
=encoding
, errors
=errors
71 encoding
: str = 'utf-8',
72 errors
: str = 'strict',
74 """Return the decoded string of the resource.
76 The decoding-related arguments have the same semantics as those of
79 with open_text(package
, resource
, encoding
, errors
) as fp
:
84 def contents(package
: Package
) -> Iterable
[str]:
85 """Return an iterable of entries in `package`.
87 Note that not all entries are resources. Specifically, directories are
88 not considered resources. Use `is_resource()` on each entry returned here
89 to check if it is a resource or not.
91 return [path
.name
for path
in _common
.files(package
).iterdir()]
95 def is_resource(package
: Package
, name
: str) -> bool:
96 """True if `name` is a resource inside `package`.
98 Directories are *not* resources.
100 resource
= normalize_path(name
)
102 traversable
.name
== resource
and traversable
.is_file()
103 for traversable
in _common
.files(package
).iterdir()
111 ) -> ContextManager
[pathlib
.Path
]:
112 """A context manager providing a file path object to the resource.
114 If the resource does not already exist on its own on the file system,
115 a temporary file will be created. If the file was created, the file
116 will be deleted upon exiting the context manager (no exception is
117 raised if the file was deleted prior to the context manager
120 return _common
.as_file(_common
.files(package
) / normalize_path(resource
))