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 calledget_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,FileNotFoundError israised.

abstractmethodresource_path(resource)

Returns the file system path to theresource.

If the resource does not concretely exist on the file system,raiseFileNotFoundError.

abstractmethodis_resource(name)

ReturnsTrue if the namedname is considered a resource.FileNotFoundError is 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 whichis_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 ofpathlib.Path methods suitable fortraversing directories and opening files.

For a representation of the object on the file-system, useimportlib.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 aspathlib.Path.open).

When opening as text, accepts encoding parameters such as thoseaccepted byio.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 servingtheimportlib.resources.files() interface. SubclassesResourceReader and providesconcrete implementations of theResourceReader’sabstract methods. Therefore, any loader supplyingTraversableResources also suppliesResourceReader.

Loaders that wish to support resource reading are expected toimplement this interface.

New in version 3.9.

abstractmethodfiles()

Returns aimportlib.resources.abc.Traversable object for the loadedpackage.