importlib.resources.abc – Abstract base classes for resources¶
Source code:Lib/importlib/resources/abc.py
New in version 3.11.
- classimportlib.resources.abc.ResourceReader¶
Superseded by TraversableResources
Anabstract base class to provide the ability to readresources.
From the perspective of this ABC, aresource is a binaryartifact that is shipped within a package. Typically this issomething like a data file that lives next to the
__init__.pyfile of the package. The purpose of this class is to help abstractout the accessing of such data files so that it does not matter ifthe package and its data file(s) are stored in a e.g. zip fileversus on the file system.For any of methods of this class, aresource argument isexpected to be apath-like object which representsconceptually just a file name. This means that no subdirectorypaths should be included in theresource argument. This isbecause the location of the package the reader is for, acts as the“directory”. Hence the metaphor for directories and filenames is packages and resources, respectively. This is also whyinstances of this class are expected to directly correlate toa specific package (instead of potentially representing multiplepackages or a module).
Loaders that wish to support resource reading are expected toprovide a method called
get_resource_reader(fullname)whichreturns an object implementing this ABC’s interface. If the modulespecified by fullname is not a package, this method should returnNone. An object compatible with this ABC should only bereturned when the specified module is a package.New in version 3.7.
- abstractmethodopen_resource(resource)¶
Returns an opened,file-like object for binary readingof theresource.
If the resource cannot be found,
FileNotFoundErrorisraised.
- abstractmethodresource_path(resource)¶
Returns the file system path to theresource.
If the resource does not concretely exist on the file system,raise
FileNotFoundError.
- abstractmethodis_resource(name)¶
Returns
Trueif the namedname is considered a resource.FileNotFoundErroris raised ifname does not exist.
- abstractmethodcontents()¶
Returns aniterable of strings over the contents ofthe package. Do note that it is not required that all namesreturned by the iterator be actual resources, e.g. it isacceptable to return names for which
is_resource()wouldbe false.Allowing non-resource names to be returned is to allow forsituations where how a package and its resources are storedare known a priori and the non-resource names would be useful.For instance, returning subdirectory names is allowed so thatwhen it is known that the package and resources are stored onthe file system then those subdirectory names can be useddirectly.
The abstract method returns an iterable of no items.
- classimportlib.resources.abc.Traversable¶
An object with a subset of
pathlib.Pathmethods suitable fortraversing directories and opening files.For a representation of the object on the file-system, use
importlib.resources.as_file().New in version 3.9.
- name¶
Abstract. The base name of this object without any parent references.
- abstractmethoditerdir()¶
Yield Traversable objects in self.
- abstractmethodis_dir()¶
Return True if self is a directory.
- abstractmethodis_file()¶
Return True if self is a file.
- abstractmethodjoinpath(child)¶
Return Traversable child in self.
- abstractmethod__truediv__(child)¶
Return Traversable child in self.
- abstractmethodopen(mode='r',*args,**kwargs)¶
mode may be ‘r’ or ‘rb’ to open as text or binary. Return a handlesuitable for reading (same as
pathlib.Path.open).When opening as text, accepts encoding parameters such as thoseaccepted by
io.TextIOWrapper.
- read_bytes()¶
Read contents of self as bytes.
- read_text(encoding=None)¶
Read contents of self as text.
- classimportlib.resources.abc.TraversableResources¶
An abstract base class for resource readers capable of servingthe
importlib.resources.files()interface. SubclassesResourceReaderand providesconcrete implementations of theResourceReader’sabstract methods. Therefore, any loader supplyingTraversableResourcesalso suppliesResourceReader.Loaders that wish to support resource reading are expected toimplement this interface.
New in version 3.9.
- abstractmethodfiles()¶
Returns a
importlib.resources.abc.Traversableobject for the loadedpackage.