The ResourceLoader class

class ferenda.ResourceLoader(*loadpath, **kwargs)[source]
static make_loadpath(instance, suffix='res')[source]

Given an object instance, returns a list of path locations corresponding to the physical location of the implementation of that instance, with a specified suffix.

ie. if provided an Foo instance, whose class is defined in project/subclass/foo.py, and Foo derives from Bar, whose class is defined in project/bar.py, the returned make_loadpath will return ['project/subclass/res', 'project/res']

exists(resourcename)[source]

Returns True iff the named resource can be found anywhere in any place where this loader searches, False otherwise

load(resourcename, binary=False)[source]

Returns the contents of the resource, either as a string or a bytes object, depending on whether binary is False or True.

Might raise ResourceNotFound.

openfp(resourcename, binary=False)[source]

Opens the specified resource and returns a open file object. Caller must call .close() on this object when done.

Might raise ResourceNotFound.

open(resourcename, binary=False)[source]

Opens the specified resource as a context manager, ie call with with:

>>> loader = ResourceLoader()
>>> with resource.open("robots.txt") as fp:
...     fp.read()

Might raise ResourceNotFound.

filename(resourcename)[source]

Return a filename pointing to the physical location of the resource. If the resource is only found using the ResourceManager API, extract ‘ the resource to a temporary file and return its path.

Might raise ResourceNotFound.

extractdir(resourcedir, target, suffixes=None)[source]

Extract all file resources contained in the specified resource directory to the target directory.

Searches all loadpaths and optionally the Resources API for any file contained within. This means the target dir may end up with eg. one file from a high-priority path and other files from the system dirs/resources. This in turns makes it easy to just override a single file in a larger set of resource files.

Even if the resourcedir might contain resources in subdirectories (eg “source/sub/dir/resource.xml”), the extraction will be to the top-level target directory (eg “target/resource.xml”).