Package: van.static
Serving static files from a CDNA Content Delivery Network (CDN) allows you to serve static files to
users faster. For a web application the primary benefit is that your
pages render faster, giving a better user experience.Additionally, content such as JavaScript or CSS files should be
“slimmed” before being placed on a CDN. By decreasing the file size you
further improve the user experience with even faster loading.van.static gives you a tool to optionally deploy files to a CDN without
making the development process more onerous.WorkflowThis tool aids in implementing a very specific workflow using static files and
pyramid:During development static files are stored in subversion and are configured
as normal in Pyramid applications.Before deployment, the static files are extracted from the eggs by the
system administrator and uploaded to a CDN. The URL on the CDN varies
depending on the egg version the files where extracted from.During extraction, CSS and JS files can be optionally minified.In production, the system administrator configures the application to use
static files from the CDN.This workflow has these advantages:Minimal impact on development. Changes to files are immediately visible,
also developers work with un-compressed files.CDN served files can have very long cache-control times while still allowing
them to be updated almost immediately on application upgrade.Testing an extractionYou can try out the extract tool by running thecdn.pyfile directly. The
following commands will extract thestaticresource from thedeformpackage to thetest_extractdirectory:$ mkdir test_extract
$ python van/static/cdn.py –target “file://$(pwd)/test_extract” –resource deform:staticNOTE: the deform package must be on the python path.If you use a url like thiss3://mybucket/path/to/files/the extracted
resources will be placed directly in Amazon S3. You need to manually installbototo be able to use this functionality.Implementing in your applicationOne way would be to have code like this in your package:>>> def my_extract_filesystem_command():
...     """Customized extract command for my application"""
...     cdn.extract_cmd(['myapp:static', 'deform:static'], yui_compressor=True)>>> from pyramid.config import Configurator
>>> def make_pyramid_app(cdn_url=None):
...     config = Configurator()
...     config.include('van.static.cdn')
...     config.add_cdn_view(cdn_url or 'myapp_static', 'myapp:static')
...     config.add_cdn_view(cdn_url or 'deform_static', 'deform:static')
...     return config.make_wsgi_app()You would makemy_extract_filesystem_commanda command line script
for the system administrator to run on deployment. Likewise thecdn_urlconfiguration option is set by the system administrator to
the url where the files were exported to.GZip Content-Encoding compressionCompressing resources during extraction is supported for the S3 target.
S3 and Cloudfront does not directly support on the fly compression at
this time so a workaround is used where multiple copies of resources are
uploaded. One without any encoding and the others with encodings.The links to resources should then be generated to compressed or
non-compressed resources depending on the capabilities of the browser.An example of how to modify the resource generation in a pyramid
application is:>>> from pyramid.request import Request
>>> class ZippingPyramidRequest(Request):
...
...     def static_url(self, path, **kw):
...         if 'gzip' in self.accept_encoding:
...             package, path = path.split(':', 1)
...             path = '{package}:gzip/{path}'.format(
...                     package=package,
...                     path=path)
...         return Request.static_url(self, path, **kw)The extractor is configured to upload resources with the gzip encoding
with the –encoding parameter.WARNING: TheVaryHTTP will need to containAccept-Encodingto play
well with any caches.APT integrationFor system administrators who use APT to install packages, a useful trick is
put a snippet into/etc/apt/apt.conf.d/:DPkg::Post-Invoke::      "/path/to/extraction/script";So that the extraction script runs whenever packages are installed on the
application servers. Note that if you haveetckeeperinstalled, this should
be placed afterwards.JSLint testing supportNOTE: To use this functionality you must have ajslintcommand on your PATH.This allows you to runjslinton all the files in a directory from a
unittest. For example:>>> import unittest>>> class TestJSLint(unittest.TestCase):
...
...     def test_static(self):
...         from van.static.testing import assert_jslint_dir
...         from pkg_resources import resource_filename, cleanup_resources
...         assert_jslint_dir(resource_filename('vanguardistas.publicview', 'static/js'))
...         cleanup_resources()YUI3 loader configuration helpervan.static.yuiholds utilities to simplify setting up a YUI3 loader
configuration from a directory of JS modules.ContributingIf you’re interested, the primary development repository over at githubhttps://github.com/jinty/van.static