-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpkg_resources.py
More file actions
31 lines (22 loc) · 1.21 KB
/
Copy pathpkg_resources.py
File metadata and controls
31 lines (22 loc) · 1.21 KB
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
"""Lightweight subset of :mod:`pkg_resources` for offline environments.
This project depends on ``pronouncing``/``cmudict`` which import
``pkg_resources.resource_stream`` and ``pkg_resources.resource_string`` to load
packaged data. In environments where ``setuptools`` (which provides
``pkg_resources``) is unavailable, importing ``pronouncing`` fails during test
collection. This shim supplies just the APIs needed by our dependencies using
:mod:`importlib.resources` so the package can function without the external
runtime dependency.
"""
from __future__ import annotations
import importlib.resources
from typing import BinaryIO
def resource_stream(package: str, resource_name: str) -> BinaryIO:
"""Return a binary stream for ``resource_name`` within ``package``.
This mirrors the small portion of ``pkg_resources`` exercised by our
dependencies and intentionally supports only file-like binary access.
"""
return importlib.resources.files(package).joinpath(resource_name).open("rb")
def resource_string(package: str, resource_name: str) -> bytes:
"""Return the full contents of a packaged resource as bytes."""
with resource_stream(package, resource_name) as stream:
return stream.read()