Package: pyramid_assetgen
# Pyramid Assetgen[pyramid_assetgen][] allows you to integrate [Assetgen][] with a [Pyramid][]project.## tl;drInclude the package, set a custom request factory (using `AssetGenRequestMixin`as the *first* class you inherit from):class MyRequest(AssetGenRequestMixin, Request): passconfig.set_request_factory(MyRequest)config.include('pyramid_assetgen')Use the `add_assetgen_manifest` configuration directive to associate an Assetgenmanifest file with a static directory:config.add_static_view('static', 'mypkg:static')# defaults to look for an assets.json file in the same directoryconfig.add_assetgen_manifest('mypkg:static')And then just use Pyramid's built-in `request.static_url(path, **kw)` as normal.## Rationale[Assetgen]() is a static file build tool.  When using in production, you canenable its hashing mode to output files with a hash in the filename.  This meansthat when you change a file (for example, maybe updating your site's stylesheet)its name will change.On the one hand this is excellent news, as it allows you to implement an optimalHTTP caching strategy (telling browser clients to cache your static filesforever).  On the other, it means you need to update your templates and / or viewcode to serve the right url to resolves to the hashed filename.  This isrelatively easy when you're using [Pyramid]() as you're already using a dynamicfunction to generate your static urls: `request.static_url`.This package, [pyramid_assetgen]() extends the Pyramid machinery to automaticallyupdate your static urls so that they resolve to the correct hashed file names.You can use it to integrate [Assetgen]() with your [Pyramid]() application withouthaving to change any of your templates or view code or learn any new APIs.## WorkflowIf you run a Pyramid application configured to look for a manifest file, then thefile needs to be there, otherwise the application will throw an exception (atconfiguration time).  You should thus build your manifest file using somethinglike:assetgen etc/assetgen.yaml --forceBefore you run your Pyramid app with something like:pserve etc/production.iniIf running in development mode using [paste.reloader], e.g.:pserve etc/development.ini --reloadYou could add your manifest file to the list of files the reloader should watchusing, e.g.:from paste.reloader import add_file_callbackdef watch_manifest_files():return ['/var/www/static/assets.json',]add_file_callback(watch_manifest_files)However, you're unlikely to need this, as you shouldn't auto-reload in productionand in development mode you shouldn't hash your assetgen files.## ConfigurationIf we presume `config` is a `pyramid.config.Configurator` instance, (perhapsavailable in your main / WSGI app factory function), we can add the directiveeither using:from pyramid_assetgen import add_assetgen_manifestconfig.add_directive('add_assetgen_manifest', add_assetgen_manifest)Or more simply:config.include('pyramid_assetgen')In addition, you'll need to extend the default request factory using, e.g.:from pyramid.request import Requestfrom pyramid_assetgen import AssetGenRequestMixinclass MyRequest(AssetGenRequestMixin, Request):passconfig.set_request_factory(MyRequest)(Note that the `AssetGenRequestMixin` argument must come **before** `Request` inyour request factory class definition.  Otherwise the `static_url` method willnot be overridden).## UsageWith that configuration, when you expose a static directory using`config.add_static_view`, you can now associate an Assetgen manifest with it:config.add_static_view('static', 'mypkg:static')config.add_assetgen_manifest('mypkg:static')This will look for a manifest file in `mypkg:static/assets.json`.  If thefile is somewhere else use:config.add_assetgen_manifest('mypkg:static', manifest='/foo/bar.json')You can then use `request.static_path` and `request.static_url` as normal.So, for example, if you have registered a manifest containing:{'foo.js': 'foo-fdsf465ds4f567ds4ds5674567f4s7.js'}Calling:request.static_path('mypkg:static/foo.js')Will return:'/static/foo-fdsf465ds4f567ds4ds5674567f4s7.js'## TestsI've run the tests under Python2.6 and Python3.2 using, e.g.:$ ../bin/nosetests --cover-package=src/pyramid_assetgen --cover-erase --with-coverage --with-doctest.......Name                                  Stmts   Miss  Cover   Missing-------------------------------------------------------------------src/pyramid_assetgen/__init__            59      0   100%src/pyramid_assetgen/tests/__init__      58      0   100%-------------------------------------------------------------------TOTAL                                   117      0   100%----------------------------------------------------------------------Ran 7 tests in 0.552sOK[assetgen]: http://github.com/tav/assetgen[pyramid]: http://pypi.python.org/pypi/pyramid[pyramid_assetgen]: http://github.com/thruflo/pyramid_assetgen