# HG changeset patch # User paulb # Date 1137369260 0 # Node ID 3ff6598f2d943e1caf5bb07e53cc0f8ada49172e # Parent 57138519c16d4c620ca00132cbbc6bf92312abe4 [project @ 2006-01-15 23:54:20 by paulb] Added content type parameters to DirectoryResource. diff -r 57138519c16d -r 3ff6598f2d94 docs/directory-resource.html --- a/docs/directory-resource.html Sun Jan 15 23:54:16 2006 +0000 +++ b/docs/directory-resource.html Sun Jan 15 23:54:20 2006 +0000 @@ -29,11 +29,11 @@ a file type indicating that the contents of the file are HTML (or some variant thereof). Instances of DirectoryResource employ a special registry mapping filename extensions to file types in order -to automate the process of deciding what type a file might be.

Using the media_types parameter (see below), one might choose to serve all files whose names have the .html extension with the HTML file (or content) type. This would be expressed with the following dictionary:

{"html" : "text/html"}

Note that the . -character is omitted from the filename extension and that the file type -(or more correctly, the media type) does not include character set -information.

Further Reading

The API documentation for the DirectoryResource class provides more information on the usage of the class.

Initialisation

The DirectoryResource class (found in the WebStack.Resources.Static module) accepts the following parameters when being initialised:

-
directory
The directory from which static files shall be served.
media_types
A dictionary or dictionary-like object mapping filename extensions to MIME types.
unrecognised_media_type
An optional parameter setting the MIME type for files whose extensions do not map to any MIME type according to the media_types parameter.
urlencoding
When +to automate the process of deciding what type a file might be.

Using the content_types parameter (see below), one might choose to serve all files whose names have the .html extension with the HTML file (or content) type. This would be expressed with the following dictionary:

{"html" : WebStack.Generic.ContentType("text/html", "utf-8")}

Note that the . +character is omitted from the filename extension. Where it is tedious +to create content type objects and where the same character encoding +applies for all types of files, it is possible to use the media_types and default_encoding parameters instead.

Further Reading

The API documentation for the DirectoryResource class provides more information on the usage of the class.

Initialisation

The DirectoryResource class (found in the WebStack.Resources.Static module) accepts the following parameters when being initialised:

+
directory
The directory from which static files shall be served.
media_types
A dictionary or dictionary-like object mapping filename extensions to media types.
unrecognised_media_type
An optional parameter setting the media type for files whose extensions do not map to any media type according to the media_types parameter.
content_types
A dictionary or dictionary-like object mapping filename extensions to content type objects (such as WebStack.Generic.ContentType).
unrecognised_content_type
An optional parameter setting the content type for files whose extensions do not map to any content type according to the content_types parameter.
default_encoding
An optional parameter setting the character encoding for all media types defined in the media_types and unrecognised_media_type parameters. If not specified, no character encoding will be stated for content associated with such media types.
urlencoding
When specified, this parameter forces a particular interpretation of "URL encoded" character values in the path. Otherwise, the default encoding -is employed to interpret such values. (See "URLs and Paths" for an explanation of "URL encoded" values.)

Combining MapResource with DirectoryResource

One might combine MapResource with DirectoryResource to provide stylesheet and script file directories for an application as follows:

from WebStack.Resources.ResourceMap import MapResource
from WebStack.Resources.Static import DirectoryResource

# This is where the application's resources would be obtained.

app_resource = ...

# Here is where we combine MapResource and DirectoryResource.
# Note that one would not necessarily hard-code directory paths into the application.

top_resource = MapResource({
"styles" : DirectoryResource("/usr/share/apps/MyApp/styles", {"css" : "text/css"}),
"scripts" : DirectoryResource("/usr/share/apps/MyApp/scripts", {"js" : "text/javascript"}),
"" : app_resource
})

In the above example, any file in the styles directory whose name ends with .css is served as text/css. Similarly, any file in the scripts directory whose name ends with .js is served as text/javascript.

\ No newline at end of file +is employed to interpret such values. (See "URLs and Paths" for an explanation of "URL encoded" values.)

Combining MapResource with DirectoryResource

One might combine MapResource with DirectoryResource to provide stylesheet and script file directories for an application as follows:

from WebStack.Resources.ResourceMap import MapResource
from WebStack.Resources.Static import DirectoryResource

# This is where the application's resources would be obtained.

app_resource = ...

# Here is where we combine MapResource and DirectoryResource.
# Note that one would not necessarily hard-code directory paths into the application.

top_resource = MapResource({
"styles" : DirectoryResource("/usr/share/apps/MyApp/styles", media_types={"css" : "text/css"}),
"scripts" : DirectoryResource("/usr/share/apps/MyApp/scripts", media_types={"js" : "text/javascript"}),
"" : app_resource
})

In the above example, any file in the styles directory whose name ends with .css is served as text/css. Similarly, any file in the scripts directory whose name ends with .js is served as text/javascript.

\ No newline at end of file