On Thu, Aug 25, 2011 at 05:58:06AM -0700, Luca Frosini wrote:
> does anyone knows if it possible to add_static_view for a single file.
> I would like use override assets for that file and I can't match it
> with some prefix.
What I did in this case was to write a thin wrapper around paste's FileApp.
I don't know if this is the best way, but it seems to work.
from pyramid.path import caller_package
from pyramid.asset import abspath_from_asset_spec
from paste.fileapp import FileApp
class StaticView(object):
""" A Pyramid :term:`view` callable which serves up a static file.
:param asset_spec:
A ``Pyramid`` :term:`asset specification` identifying the file
to be served.
:param cache_max_age:
Value to put in the ``max_age`` field of the ``Cache-Control``
header. (Also sets the ``Expires`` header appropriately for
HTTP/1.0 clients.)
:param package_name:
The package in which to resolve :arg:`asset_spec` (if the spec
does not include a package name). Defaults to the caller’s
package.
"""
def __init__(self, asset_spec, cache_max_age=3600, package_name=None):
pname = package_name or caller_package().__name__
filename = abspath_from_asset_spec(asset_spec, pname)
# Check that file is readable. We want to throw an error now,
# rather than wait until a client requests the file.
file(filename, "r")
self.app = FileApp(filename)
self.app.cache_control(max_age=cache_max_age)
def __call__(self, request):
return request.get_response(self.app)
... then, in your config code, something like ...
config.add_route('favicon', '/favicon.ico')
config.add_view(StaticView('static/favicon.ico'), route_name='favicon')
Cheers,
Jeff
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.