Python 3.7's importing mechanism offers a new "resource reader"
API that allows module loaders to provide an object which can be
used for obtaining information and data about "resources" in a
Python package. This API is the new recommended way for modules
to load data for non-module entities, such as text files and
other support files residing next to sources. This functionality
is similar to pkg_resources (which has existed for a long time)
and importlib.abc.ResourceLoader, which was introduced in Python 3
and is deprecated in favor of ResourceReader.
Using a "resource reader" is a superior approach to pkg_resources
because it is newer and faster. And both are superior to file
based filesystem reading because they allow the storage of
resources in places that aren't the filesystem. This allows things
like embedding resources in zip files or single file binaries.
This commit introduces a "resource reader" API into the procutil
module.
We introduce an importlib.abc.ResourceReader compatible class
which can load resources from the filesystem.
We introduce another class that wraps an existing ResourceReader
so callers don't need to worry about bytes/str differences.
Finally, there is a new "resourcereader()" that returns a
ResourceReader for a given Python package. The idea is that
code will call `pycompat.resourcereader(__package__)` (or
similar) to obtain an object conforming to
importlib.abc.ResourceReader then from there on, things will
look like they would as if the code were Python 3.7 native.
The ultimate goal is to remove the dependence on file
and util.datapath so non-module data file access is abstract
and doesn't need to be serviced by a traditional filesystem.
The new code will return bad results for py2exe and macOS
application builds because resource files live in immediate
subdirectories of the binary instead of next to the source.
This will be addressed in future commits.
- no-check-commit because we must use foo_bar naming