# Class pyglet.resource.Loader

Load program resource files from disk.

The loader contains a search path which can include filesystem directories, ZIP archives and Python packages.

## Methods

| __init__(self, path=None, script_home=None) Create a loader for the given path.
| reindex(self) Refresh the file index.
file object | file(self, name, mode=`'``rb``'`) Load a resource.
Location | location(self, name) Get the location of a resource.
| add_font(self, name) Add a font resource to the application.
Texture | image(self, name, flip_x=False, flip_y=False, rotate=0) Load an image with optional transformation.
Animation | animation(self, name, flip_x=False, flip_y=False, rotate=0) Load an animation with optional transformation.
list | get_cached_image_names(self) Get a list of image filenames that have been cached.
list | get_cached_animation_names(self) Get a list of animation filenames that have been cached.
list | get_texture_bins(self) Get a list of texture bins in use.
media.Source | media(self, name, streaming=True) Load a sound or video resource.
Texture | texture(self, name) Load a texture.
FormattedDocument | html(self, name) Load an HTML document.
FormattedDocument | attributed(self, name) Load an attributed text document.
UnformattedDocument | text(self, name) Load a plain text document.
list of str | get_cached_texture_names(self) Get the names of textures currently cached.

## Instance Variables

list of str | path List of search locations.
str | script_home Base resource location, defaulting to the location of the application script.

## Method Details

### __init__

(Constructor) __init__(self, path=None, script_home=None)

Create a loader for the given path.

If no path is specified it defaults to `['.']`; that is, just the program directory.

See the module documentation for details on the path format.

Parameters:
  path : list of str
    List of locations to search for resources.
  script_home : str
    Base location of relative files. Defaults to the result of get_script_home.

### reindex

reindex(self)

Refresh the file index.

You must call this method if path is changed or the filesystem layout changes.

### file

file(self, name, mode=`'``rb``'`)
Load a resource.

Parameters:
  name : str
    Filename of the resource to load.
  mode : str
    Combination of `r`, `w`, `a`, `b` and `t` characters with the meaning as for the builtin `open` function.
Returns: file object

### location

location(self, name)

Get the location of a resource.

This method is useful for opening files referenced from a resource. For example, an HTML file loaded as a resource might reference some images. These images should be located relative to the HTML file, not looked up individually in the loader's path.

Parameters:
  name : str
    Filename of the resource to locate.
Returns: Location

### add_font

add_font(self, name)

Add a font resource to the application.

Fonts not installed on the system must be added to pyglet before they can be used with font.load. Although the font is added with its filename using this function, it is loaded by specifying its family name. For example:

    resource.add_font('action_man.ttf')
    action_man = font.load('Action Man')

Parameters:
  name : str
    Filename of the font resource to add.

### image

image(self, name, flip_x=False, flip_y=False, rotate=0)

Load an image with optional transformation.

This is similar to texture, except the resulting image will be packed into a TextureBin if it is an appropriate size for packing. This is more efficient than loading images into separate textures.

Parameters:
  name : str
    Filename of the image source to load.
  flip_x : bool
    If True, the returned image will be flipped horizontally.
  flip_y : bool
    If True, the returned image will be flipped vertically.
  rotate : int
    The returned image will be rotated clockwise by the given number of degrees (a mulitple of 90).
Returns:
  Texture: A complete texture if the image is large, otherwise a TextureRegion of a texture atlas.

### animation

animation(self, name, flip_x=False, flip_y=False, rotate=0)

Load an animation with optional transformation.

Animations loaded from the same source but with different transformations will use the same textures.

Parameters:
  name : str
    Filename of the animation source to load.
  flip_x : bool
    If True, the returned image will be flipped horizontally.
  flip_y : bool
    If True, the returned image will be flipped vertically.
  rotate : int
    The returned image will be rotated clockwise by the given number of degrees (a mulitple of 90).
Returns: Animation

### get_cached_image_names

get_cached_image_names(self)

Get a list of image filenames that have been cached.

This is useful for debugging and profiling only.

Returns:
  list: List of str

### get_cached_animation_names

get_cached_animation_names(self)

Get a list of animation filenames that have been cached.

This is useful for debugging and profiling only.

Returns:
  list: List of str

### get_texture_bins

get_texture_bins(self)

Get a list of texture bins in use.

This is useful for debugging and profiling only.

Returns:
  list: List of TextureBin

### media

media(self, name, streaming=True)

Load a sound or video resource.

The meaning of `streaming` is as for media.load. Compressed sources cannot be streamed (that is, video and compressed audio cannot be streamed from a ZIP archive).

Parameters:
  name : str
    Filename of the media source to load.
  streaming : bool
    True if the source should be streamed from disk, False if it should be entirely decoded into memory immediately.
Returns: media.Source

### texture

texture(self, name)

Load a texture.

The named image will be loaded as a single OpenGL texture. If the dimensions of the image are not powers of 2 a TextureRegion will be returned.

Parameters:
  name : str
    Filename of the image resource to load.
Returns: Texture

### html

html(self, name)
Load an HTML document.

Parameters:
  name : str
    Filename of the HTML resource to load.
Returns: FormattedDocument

### attributed

attributed(self, name)

Load an attributed text document.

See pyglet.text.formats.attributed for details on this format.

Parameters:
  name : str
    Filename of the attribute text resource to load.
Returns: FormattedDocument

### text

text(self, name)
Load a plain text document.

Parameters:
  name : str
    Filename of the plain text resource to load.
Returns: UnformattedDocument

## Instance Variable Details

### path

List of search locations. After modifying the path you must call the reindex method.

Type:
  list of str