websauna.system.http.static module

Registering static assets with cache busting policies.

For more information read Static assets narrative.

class websauna.system.http.static.CollectedStaticCacheBuster(static_view_name, path, settings)[source]

Bases: object

A Pyramid cache buster which uses persistent static item folder from ws-collect-static command to serve static assets.

manifest[source]

Read manifest file which maps filenames to their MD5 stamped counterparts.

Returns

Manifest entry for a view.

class websauna.system.http.static.CopyAndHashCollector(root, settings, target_path=None)[source]

Bases: object

Toss all static files into perma-asset folder, MD5 hash included in the name.

collect(root, static_view_name, entry, relative_path)[source]

Process one file and add it to our collection.

Parameters
  • root (str) – Root path.

  • static_view_name (str) – Asset name.

  • entry (DirEntry) – DirEntry for asset.

  • relative_path (str) – Relative path of the asset.

finish()[source]

Finish collection and create manifest.json file.

Return type

dict

Returns

Collected files.

get_permanent_path(root, static_view_name, relative_path, hash)[source]

Return the permanent path for an asset.

Parameters
  • root (str) – Root path.

  • static_view_name (str) – Name of the asset.

  • relative_path (str) – Relative path of the asset.

  • hash (str) – Hash of the file.

Return type

str

Returns

Permanent path to the asset.

process(root, static_view_name, entry, relative_path)[source]

Make a persistent copy of a file.

Parameters
  • root (str) – Root path.

  • static_view_name (str) – Asset name.

  • entry (DirEntry) – DirEntry for asset.

  • relative_path (str) – Relative path of the asset.

Return type

str

Returns

Permanent path to the asset

class websauna.system.http.static.DefaultStaticAssetPolicy(config)[source]

Bases: websauna.system.http.static.StaticAssetPolicy

Default inplementation of StaticAssetPolicy.

add_static_view(name, path)[source]

Include a path in static assets and configures cache busting for it.

This does not only include the static resources in the routing, but sets the default cache busting policy for them in the production environment.

See pyramid.config.Configurator.add_static_view() and websauna.system.Initializer.add_cache_buster()

Parameters
  • name (str) – Asset name.

  • path (str) – Asset path.

collect_static()[source]

Collect all static files from all static views for the manifest.

Return type

dict

Returns

Collect assets.

views = None

Maintain registry of all registered static views

websauna.system.http.static.MANIFEST_FILE = '.manifest.json'

Generated at the root of each perma-asset folder

class websauna.system.http.static.StaticAssetPolicy(config)[source]

Bases: abc.ABC

A helper class to add static views and apply a configured cache busting policy on them.

abstract add_static_view(name, path)[source]

Include a path in static assets and configures cache busting for it.

This does not only include the static resources in the routing, but sets the default cache busting policy for them in the production environment.

See pyramid.config.Configurator.add_static_view() for more details.

Parameters
  • name (str) – Name of the asset.

  • path (str) – Path to the asset.

websauna.system.http.static.md5(filename)[source]

Generate the md5 hash for a file with given filename.

Parameters

filename (str) – Name of the file to generate the MD5 hash for.

Return type

str

Returns

md5 hash of the file.