| 1 |
:mod:`!importlib.resources.abc` -- Abstract base classes for resources |
| 2 |
---------------------------------------------------------------------- |
| 3 |
|
| 4 |
.. module:: importlib.resources.abc |
| 5 |
:synopsis: Abstract base classes for resources |
| 6 |
|
| 7 |
**Source code:** :source:`Lib/importlib/resources/abc.py` |
| 8 |
|
| 9 |
-------------- |
| 10 |
|
| 11 |
.. versionadded:: 3.11 |
| 12 |
|
| 13 |
.. class:: ResourceReader |
| 14 |
|
| 15 |
*Superseded by TraversableResources* |
| 16 |
|
| 17 |
An :term:`abstract base class` to provide the ability to read |
| 18 |
*resources*. |
| 19 |
|
| 20 |
From the perspective of this ABC, a *resource* is a binary |
| 21 |
artifact that is shipped within a package. Typically this is |
| 22 |
something like a data file that lives next to the ``__init__.py`` |
| 23 |
file of the package. The purpose of this class is to help abstract |
| 24 |
out the accessing of such data files so that it does not matter if |
| 25 |
the package and its data file(s) are stored e.g. in a zip file |
| 26 |
versus on the file system. |
| 27 |
|
| 28 |
For any of methods of this class, a *resource* argument is |
| 29 |
expected to be a :term:`path-like object` which represents |
| 30 |
conceptually just a file name. This means that no subdirectory |
| 31 |
paths should be included in the *resource* argument. This is |
| 32 |
because the location of the package the reader is for, acts as the |
| 33 |
"directory". Hence the metaphor for directories and file |
| 34 |
names is packages and resources, respectively. This is also why |
| 35 |
instances of this class are expected to directly correlate to |
| 36 |
a specific package (instead of potentially representing multiple |
| 37 |
packages or a module). |
| 38 |
|
| 39 |
Loaders that wish to support resource reading are expected to |
| 40 |
provide a method called ``get_resource_reader(fullname)`` which |
| 41 |
returns an object implementing this ABC's interface. If the module |
| 42 |
specified by fullname is not a package, this method should return |
| 43 |
:const:`None`. An object compatible with this ABC should only be |
| 44 |
returned when the specified module is a package. |
| 45 |
|
| 46 |
.. deprecated:: 3.12 |
| 47 |
Use :class:`importlib.resources.abc.TraversableResources` instead. |
| 48 |
|
| 49 |
.. method:: open_resource(resource) |
| 50 |
:abstractmethod: |
| 51 |
|
| 52 |
Returns an opened, :term:`file-like object` for binary reading |
| 53 |
of the *resource*. |
| 54 |
|
| 55 |
If the resource cannot be found, :exc:`FileNotFoundError` is |
| 56 |
raised. |
| 57 |
|
| 58 |
.. method:: resource_path(resource) |
| 59 |
:abstractmethod: |
| 60 |
|
| 61 |
Returns the file system path to the *resource*. |
| 62 |
|
| 63 |
If the resource does not concretely exist on the file system, |
| 64 |
raise :exc:`FileNotFoundError`. |
| 65 |
|
| 66 |
.. method:: is_resource(path) |
| 67 |
:abstractmethod: |
| 68 |
|
| 69 |
Returns ``True`` if the named *path* is considered a resource. |
| 70 |
:exc:`FileNotFoundError` is raised if *path* does not exist. |
| 71 |
|
| 72 |
.. versionchanged:: 3.10 |
| 73 |
The argument *name* was renamed to *path*. |
| 74 |
|
| 75 |
.. method:: contents() |
| 76 |
:abstractmethod: |
| 77 |
|
| 78 |
Returns an :term:`iterable` of strings over the contents of |
| 79 |
the package. Do note that it is not required that all names |
| 80 |
returned by the iterator be actual resources, e.g. it is |
| 81 |
acceptable to return names for which :meth:`is_resource` would |
| 82 |
be false. |
| 83 |
|
| 84 |
Allowing non-resource names to be returned is to allow for |
| 85 |
situations where how a package and its resources are stored |
| 86 |
are known a priori and the non-resource names would be useful. |
| 87 |
For instance, returning subdirectory names is allowed so that |
| 88 |
when it is known that the package and resources are stored on |
| 89 |
the file system then those subdirectory names can be used |
| 90 |
directly. |
| 91 |
|
| 92 |
The abstract method returns an iterable of no items. |
| 93 |
|
| 94 |
|
| 95 |
.. class:: Traversable |
| 96 |
|
| 97 |
An object with a subset of :class:`pathlib.Path` methods suitable for |
| 98 |
traversing directories and opening files. |
| 99 |
|
| 100 |
For a representation of the object on the file-system, use |
| 101 |
:meth:`importlib.resources.as_file`. |
| 102 |
|
| 103 |
.. attribute:: name |
| 104 |
|
| 105 |
Abstract. The base name of this object without any parent references. |
| 106 |
|
| 107 |
.. method:: iterdir() |
| 108 |
:abstractmethod: |
| 109 |
|
| 110 |
Yield Traversable objects in self. |
| 111 |
|
| 112 |
.. method:: is_dir() |
| 113 |
:abstractmethod: |
| 114 |
|
| 115 |
Return ``True`` if self is a directory. |
| 116 |
|
| 117 |
.. method:: is_file() |
| 118 |
:abstractmethod: |
| 119 |
|
| 120 |
Return ``True`` if self is a file. |
| 121 |
|
| 122 |
.. method:: joinpath(*pathsegments) |
| 123 |
:abstractmethod: |
| 124 |
|
| 125 |
Traverse directories according to *pathsegments* and return |
| 126 |
the result as :class:`!Traversable`. |
| 127 |
|
| 128 |
Each *pathsegments* argument may contain multiple names separated by |
| 129 |
forward slashes (``/``, ``posixpath.sep`` ). |
| 130 |
For example, the following are equivalent:: |
| 131 |
|
| 132 |
files.joinpath('subdir', 'subsuddir', 'file.txt') |
| 133 |
files.joinpath('subdir/subsuddir/file.txt') |
| 134 |
|
| 135 |
Note that some :class:`!Traversable` implementations |
| 136 |
might not be updated to the latest version of the protocol. |
| 137 |
For compatibility with such implementations, provide a single argument |
| 138 |
without path separators to each call to ``joinpath``. For example:: |
| 139 |
|
| 140 |
files.joinpath('subdir').joinpath('subsubdir').joinpath('file.txt') |
| 141 |
|
| 142 |
.. versionchanged:: 3.11 |
| 143 |
|
| 144 |
``joinpath`` accepts multiple *pathsegments*, and these segments |
| 145 |
may contain forward slashes as path separators. |
| 146 |
Previously, only a single *child* argument was accepted. |
| 147 |
|
| 148 |
.. method:: __truediv__(child) |
| 149 |
:abstractmethod: |
| 150 |
|
| 151 |
Return Traversable child in self. |
| 152 |
Equivalent to ``joinpath(child)``. |
| 153 |
|
| 154 |
.. method:: open(mode='r', *args, **kwargs) |
| 155 |
:abstractmethod: |
| 156 |
|
| 157 |
*mode* may be 'r' or 'rb' to open as text or binary. Return a handle |
| 158 |
suitable for reading (same as :attr:`pathlib.Path.open`). |
| 159 |
|
| 160 |
When opening as text, accepts encoding parameters such as those |
| 161 |
accepted by :class:`io.TextIOWrapper`. |
| 162 |
|
| 163 |
.. method:: read_bytes() |
| 164 |
|
| 165 |
Read contents of self as bytes. |
| 166 |
|
| 167 |
.. method:: read_text(encoding=None) |
| 168 |
|
| 169 |
Read contents of self as text. |
| 170 |
|
| 171 |
|
| 172 |
.. class:: TraversableResources |
| 173 |
|
| 174 |
An abstract base class for resource readers capable of serving |
| 175 |
the :meth:`importlib.resources.files` interface. Subclasses |
| 176 |
:class:`ResourceReader` and provides |
| 177 |
concrete implementations of the :class:`!ResourceReader`'s |
| 178 |
abstract methods. Therefore, any loader supplying |
| 179 |
:class:`!TraversableResources` also supplies :class:`!ResourceReader`. |
| 180 |
|
| 181 |
Loaders that wish to support resource reading are expected to |
| 182 |
implement this interface. |
| 183 |
|
| 184 |
.. method:: files() |
| 185 |
:abstractmethod: |
| 186 |
|
| 187 |
Returns a :class:`importlib.resources.abc.Traversable` object for the loaded |
| 188 |
package. |