pyramid_assetmutator API

assign_assetmutator(config, ext, cmd, new_ext)

Configuration method to set up/assign an asset mutator. This allows the various assetmutator_* view helper methods to know which mutator to run for a specified asset path.

Parameters:
  • ext (string - Required) – The file extension this mutator should match (e.g. coffee).
  • cmd (string - Required) – The command to run (e.g. coffee -c -p). The filename to be mutated will automatically be appended to the end of this string when running the command.
  • new_ext (string - Required) – The extension that the mutated filename should have (e.g. js).

Warning

The specified mutator command must be installed, must be executable by the Pyramid process, and must output the mutated data to stdout. The last point can get tricky depending on the command, so be sure to check its command switches for the appropriate option.

For example, a mutator that would run .coffee files through the coffee command (compiling them into JavaScript) would look like:

config.assign_assetmutator('coffee', 'coffee -c -p', 'js')
assetmutator_url(path, **kw)

Returns a Pyramid static_url() of the mutated asset (and mutates the asset if needed).

Parameters:
  • path (string - Required) – The Pyramid asset path to process.
  • mutator (dict or string - Optional) – Allows you to override/specify a specific mutator to use (e.g. coffee), or assign a brand new mutator dictionary to be used (e.g. {'cmd': 'lessc', 'ext': 'css'})
assetmutator_path(path, **kw)

Returns a Pyramid static_path() of the mutated asset (and mutates the asset if needed).

Parameters:
  • path (string - Required) – The Pyramid asset path to process.
  • mutator (dict or string - Optional) – Allows you to override/specify a specific mutator to use (e.g. coffee), or assign a brand new mutator dictionary to be used (e.g. {'cmd': 'lessc', 'ext': 'css'})
assetmutator_source(path, **kw)

Returns the source data/contents of the mutated asset (and mutates the asset if needed). This is useful when you want to output inline data (e.g. for inline JavaScript blocks).

Parameters:
  • path (string - Required) – The Pyramid asset path to process.
  • mutator (dict or string - Optional) – Allows you to override/specify a specific mutator to use (e.g. coffee), or assign a brand new mutator dictionary to be used (e.g. {'cmd': 'lessc', 'ext': 'css'})

Note

Many template packages escape output by default. Consult your template language’s syntax to output an unescaped string.

assetmutator_assetpath(path, **kw)

Returns a Pyramid asset specification such as pkg:static/path/to/file.ext (and mutates the asset if needed).

Parameters:
  • path (string - Required) – The Pyramid asset path to process.
  • mutator (dict or string - Optional) – Allows you to override/specify a specific mutator to use (e.g. coffee), or assign a brand new mutator dictionary to be used (e.g. {'cmd': 'lessc', 'ext': 'css'})

This function could be used to nest pyramid_assetmutator calls. e.g. assetmutator_path(assetmutator_assetpath('pkg:static/js/script.coffee')) could compile a CoffeeScript file into JS, and then further minify the JS file if your mutator configuration looked something like:

config.assign_assetmutator('coffee', 'coffee -c -p', 'js')
config.assign_assetmutator('js', 'uglifyjs', 'js')
includeme(config)

Activate the package; typically called via config.include('pyramid_assetmutator') instead of being invoked directly.