pyramid.static¶
- class static_view(root_dir, cache_max_age=3600, package_name=None, use_subpath=False, index='index.html', reload=False, content_encodings=())[source]¶
An instance of this class is a callable which can act as a Pyramid view callable; this view will serve static files from a directory on disk based on the
root_diryou provide to its constructor.The directory may contain subdirectories (recursively); the static view implementation will descend into these directories as necessary based on the components of the URL in order to resolve a path into a response.
You may pass an absolute or relative filesystem path or a asset specification representing the directory containing static files as the
root_dirargument to this class' constructor.If the
root_dirpath is relative, and thepackage_nameargument isNone,root_dirwill be considered relative to the directory in which the Python file which callsstaticresides. If thepackage_namename argument is provided, and a relativeroot_diris provided, theroot_dirwill be considered relative to the Python package specified bypackage_name(a dotted path to a Python package).cache_max_ageinfluences theExpiresandMax-Ageresponse headers returned by the view (default is 3600 seconds or one hour).use_subpathinfluences whetherrequest.subpathwill be used asPATH_INFOwhen calling the underlying WSGI application which actually serves the static files. If it isTrue, the static application will considerrequest.subpathasPATH_INFOinput. If it isFalse, the static application will consider request.environ[PATH_INFO] asPATH_INFOinput. By default, this isFalse.reloadcontrols whether a cache of files is maintained or the asset subsystem is queried per-request to determine what files are available. By default, this isFalseand new files added while the process is running are not recognized.content_encodingsis a list of alternative file encodings supported in theAccept-EncodingHTTP Header. Alternative files are found using file extensions defined inmimetypes.encodings_map. An encoded asset will be returned with theContent-Encodingheader set to the selected encoding. If the asset contains alternative encodings then theAccept-Encodingvalue will be added to the response'sVaryheader. By default, the list is empty and no alternatives will be supported.Note
If the
root_diris relative to a package, or is a asset specification the Pyramidpyramid.config.Configuratormethod can be used to override assets within the namedroot_dirpackage-relative directory. However, if theroot_diris absolute, configuration will not be able to override the assets it contains.Changed in version 2.0: Added
reloadandcontent_encodingsoptions.
- class ManifestCacheBuster(manifest_spec, reload=False)[source]¶
An implementation of
ICacheBusterwhich uses a supplied manifest file to map an asset path to a cache-busted version of the path.The
manifest_speccan be an absolute path or a asset specification pointing to a package-relative file.The manifest file is expected to conform to the following simple JSON format:
{ "css/main.css": "css/main-678b7c80.css", "images/background.png": "images/background-a8169106.png", }
By default, it is a JSON-serialized dictionary where the keys are the source asset paths used in calls to
static_url(). For example:>>> request.static_url('myapp:static/css/main.css') "http://www.example.com/static/css/main-678b7c80.css"
The file format and location can be changed by subclassing and overriding
parse_manifest().If a path is not found in the manifest it will pass through unchanged.
If
reloadisTruethen the manifest file will be reloaded when changed. It is not recommended to leave this enabled in production.If the manifest file cannot be found on disk it will be treated as an empty mapping unless
reloadisFalse.New in version 1.6.
- static exists(path)¶
Test whether a path exists. Returns False for broken symbolic links
- static getmtime(filename)¶
Return the last modification time of a file, reported by os.stat().
- property manifest¶
The current manifest dictionary.
- class QueryStringCacheBuster(param='x')[source]¶
An implementation of
ICacheBusterwhich adds a token for cache busting in the query string of an asset URL.The optional
paramargument determines the name of the parameter added to the query string and defaults to'x'.To use this class, subclass it and provide a
tokenizemethod which acceptsrequest, pathspec, kwand returns a token.New in version 1.6.
- class QueryStringConstantCacheBuster(token, param='x')[source]¶
An implementation of
ICacheBusterwhich adds an arbitrary token for cache busting in the query string of an asset URL.The
tokenparameter is the token string to use for cache busting and will be the same for every request.The optional
paramargument determines the name of the parameter added to the query string and defaults to'x'.New in version 1.6.