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 in a e.g. 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 .. versionadded:: 3.7 47 48 .. abstractmethod:: open_resource(resource) 49 50 Returns an opened, :term:`file-like object` for binary reading 51 of the *resource*. 52 53 If the resource cannot be found, :exc:`FileNotFoundError` is 54 raised. 55 56 .. abstractmethod:: resource_path(resource) 57 58 Returns the file system path to the *resource*. 59 60 If the resource does not concretely exist on the file system, 61 raise :exc:`FileNotFoundError`. 62 63 .. abstractmethod:: is_resource(name) 64 65 Returns ``True`` if the named *name* is considered a resource. 66 :exc:`FileNotFoundError` is raised if *name* does not exist. 67 68 .. abstractmethod:: contents() 69 70 Returns an :term:`iterable` of strings over the contents of 71 the package. Do note that it is not required that all names 72 returned by the iterator be actual resources, e.g. it is 73 acceptable to return names for which :meth:`is_resource` would 74 be false. 75 76 Allowing non-resource names to be returned is to allow for 77 situations where how a package and its resources are stored 78 are known a priori and the non-resource names would be useful. 79 For instance, returning subdirectory names is allowed so that 80 when it is known that the package and resources are stored on 81 the file system then those subdirectory names can be used 82 directly. 83 84 The abstract method returns an iterable of no items. 85 86 87.. class:: Traversable 88 89 An object with a subset of :class:`pathlib.Path` methods suitable for 90 traversing directories and opening files. 91 92 For a representation of the object on the file-system, use 93 :meth:`importlib.resources.as_file`. 94 95 .. versionadded:: 3.9 96 97 .. attribute:: name 98 99 Abstract. The base name of this object without any parent references. 100 101 .. abstractmethod:: iterdir() 102 103 Yield Traversable objects in self. 104 105 .. abstractmethod:: is_dir() 106 107 Return True if self is a directory. 108 109 .. abstractmethod:: is_file() 110 111 Return True if self is a file. 112 113 .. abstractmethod:: joinpath(child) 114 115 Return Traversable child in self. 116 117 .. abstractmethod:: __truediv__(child) 118 119 Return Traversable child in self. 120 121 .. abstractmethod:: open(mode='r', *args, **kwargs) 122 123 *mode* may be 'r' or 'rb' to open as text or binary. Return a handle 124 suitable for reading (same as :attr:`pathlib.Path.open`). 125 126 When opening as text, accepts encoding parameters such as those 127 accepted by :attr:`io.TextIOWrapper`. 128 129 .. method:: read_bytes() 130 131 Read contents of self as bytes. 132 133 .. method:: read_text(encoding=None) 134 135 Read contents of self as text. 136 137 138.. class:: TraversableResources 139 140 An abstract base class for resource readers capable of serving 141 the :meth:`importlib.resources.files` interface. Subclasses 142 :class:`importlib.resources.abc.ResourceReader` and provides 143 concrete implementations of the :class:`importlib.resources.abc.ResourceReader`'s 144 abstract methods. Therefore, any loader supplying 145 :class:`importlib.abc.TraversableResources` also supplies ResourceReader. 146 147 Loaders that wish to support resource reading are expected to 148 implement this interface. 149 150 .. versionadded:: 3.9 151 152 .. abstractmethod:: files() 153 154 Returns a :class:`importlib.resources.abc.Traversable` object for the loaded 155 package. 156