Load application resources from a known path.
Loading resources by specifying relative paths to filenames is often problematic in Python, as the working directory is not necessarily the same directory as the application’s script files.
This module allows applications to specify a search path for resources.
Relative paths are taken to be relative to the application’s __main__
module. ZIP files can appear on the path; they will be searched inside. The
resource module also behaves as expected when applications are bundled using py2exe or py2app.
As well as providing file references (with the file()
function), the
resource module also contains convenience functions for loading images,
textures, fonts, media and documents.
3rd party modules or packages not bound to a specific application should
construct their own Loader
instance and override the path to use the
resources in the module’s directory.
The resource path path
(see also Loader.__init__()
and
Loader.path()
)
is a list of locations to search for resources. Locations are searched in the
order given in the path. If a location is not valid (for example, if the
directory does not exist), it is skipped.
Locations in the path beginning with an ampersand (”@” symbol) specify Python packages. Other locations specify a ZIP archive or directory on the filesystem. Locations that are not absolute are assumed to be relative to the script home. Some examples:
# Search just the `res` directory, assumed to be located alongside the
# main script file.
path = ['res']
# Search the directory containing the module `levels.level1`, followed
# by the `res/images` directory.
path = ['@levels.level1', 'res/images']
Paths are always case-sensitive and forward slashes are always used as path separators, even in cases when the filesystem or platform does not do this. This avoids a common programmer error when porting applications between platforms.
The default path is ['.']
. If you modify the path, you must call
reindex()
.
New in version 1.1.
ResourceNotFoundException
(name)¶The named resource was not found on the search path.
Loader
(path=None, script_home=None)¶Load program resource files from disk.
The loader contains a search path which can include filesystem directories, ZIP archives and Python packages.
Variables: |
|
---|
add_font
(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. |
---|
animation
(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: |
|
---|---|
Return type: |
attributed
(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. |
---|---|
Return type: | FormattedDocument |
file
(name, mode=’rb’)¶Load a resource.
Parameters: |
|
---|---|
Return type: | file object |
get_cached_animation_names
()¶Get a list of animation filenames that have been cached.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of str |
get_cached_image_names
()¶Get a list of image filenames that have been cached.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of str |
get_cached_texture_names
()¶Get the names of textures currently cached.
Return type: | list of str |
---|
get_texture_bins
()¶Get a list of texture bins in use.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of TextureBin |
html
(name)¶Load an HTML document.
Parameters: | name (str) – Filename of the HTML resource to load. |
---|---|
Return type: | FormattedDocument |
image
(name, flip_x=False, flip_y=False, rotate=0, atlas=True)¶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: |
|
---|---|
Return type: | Texture |
Returns: | A complete texture if the image is large or not in an atlas,
otherwise a |
location
(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. |
---|---|
Return type: | Location |
media
(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: |
|
---|---|
Return type: | media.Source |
reindex
()¶Refresh the file index.
You must call this method if path is changed or the filesystem layout changes.
text
(name)¶Load a plain text document.
Parameters: | name (str) – Filename of the plain text resource to load. |
---|---|
Return type: | UnformattedDocument |
texture
(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. |
---|---|
Return type: | Texture |
Location
¶Abstract resource location.
Given a location, a file can be loaded from that location with the open method. This provides a convenient way to specify a path to load files from, and not necessarily have that path reside on the filesystem.
open
(filename, mode=’rb’)¶Open a file at this location.
Parameters: |
|
---|---|
Return type: | file object |
URLLocation
(base_url)¶Location on the network.
This class uses the urlparse
and urllib2
modules to open files on
the network given a URL.
open
(filename, mode=’rb’)¶get_script_home
()¶Get the directory containing the program entry module.
For ordinary Python scripts, this is the directory containing the
__main__
module. For executables created with py2exe the result is
the directory containing the running executable file. For OS X bundles
created using Py2App the result is the Resources directory within the
running bundle.
If none of the above cases apply and the file for __main__
cannot
be determined the working directory is returned.
When the script is being run by a Python profiler, this function
may return the directory where the profiler is running instead of
the directory of the real script. To workaround this behaviour the
full path to the real script can be specified in pyglet.resource.path
.
Return type: | str |
---|
get_settings_path
(name)¶Get a directory to save user preferences.
Different platforms have different conventions for where to save user
preferences, saved games, and settings. This function implements those
conventions. Note that the returned path may not exist: applications
should use os.makedirs
to construct it if desired.
On Linux, a directory name in the user’s configuration directory is
returned (usually under ~/.config
).
On Windows (including under Cygwin) the name directory in the user’s
Application Settings
directory is returned.
On Mac OS X the name directory under ~/Library/Application Support
is returned.
Parameters: | name (str) – The name of the application. |
---|---|
Return type: | str |
path
= [‘.’]¶Default resource search path.
Locations in the search path are searched in order and are always case-sensitive. After changing the path you must call reindex.
See the module documentation for details on the path format.
Type: | list of str |
---|
reindex
()¶Refresh the file index.
You must call this method if path is changed or the filesystem layout changes.
file
(name, mode=’rb’)¶Load a resource.
Parameters: |
|
---|---|
Return type: | file object |
location
(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. |
---|---|
Return type: | Location |
add_font
(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
(name, flip_x=False, flip_y=False, rotate=0, atlas=True)¶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: |
|
---|---|
Return type: | Texture |
Returns: | A complete texture if the image is large or not in an atlas,
otherwise a |
animation
(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: |
|
---|---|
Return type: |
get_cached_image_names
()¶Get a list of image filenames that have been cached.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of str |
get_cached_animation_names
()¶Get a list of animation filenames that have been cached.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of str |
get_texture_bins
()¶Get a list of texture bins in use.
This is useful for debugging and profiling only.
Return type: | list |
---|---|
Returns: | List of TextureBin |
media
(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: |
|
---|---|
Return type: | media.Source |
texture
(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. |
---|---|
Return type: | Texture |
html
(name)¶Load an HTML document.
Parameters: | name (str) – Filename of the HTML resource to load. |
---|---|
Return type: | FormattedDocument |
attributed
(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. |
---|---|
Return type: | FormattedDocument |
text
(name)¶Load a plain text document.
Parameters: | name (str) – Filename of the plain text resource to load. |
---|---|
Return type: | UnformattedDocument |
get_cached_texture_names
()¶Get the names of textures currently cached.
Return type: | list of str |
---|