Skip to content

Instantly share code, notes, and snippets.

@rahulkmr
Created May 19, 2012 10:59
Show Gist options
  • Select an option

  • Save rahulkmr/2730466 to your computer and use it in GitHub Desktop.

Select an option

Save rahulkmr/2730466 to your computer and use it in GitHub Desktop.
def auto_version(endpoint, **values):
"""
Auto versions static assets.
"""
from flask import current_app as app
from flask import url_for
if app.debug:
return url_for(endpoint, **values)
# Check if accessing static file.
static_root = None
if endpoint == 'static':
static_root = app.static_folder
if not static_root:
m = re.match(r'(.+)\.static', endpoint)
if m:
try:
static_root = app.blueprints[m.group(1)].static_folder
except:
pass
# Get the timestamp of the file.
if static_root:
filename = values.get('filename', None)
if filename:
file_path = os.path.join(static_root, filename)
if os.path.splitext(file_path) in ['.css', '.coffee']:
file_path = re.sub('(css|coffee)', {'css': 'less', 'coffee': 'js'}["\\1"], file_path)
if os.path.isfile(file_path):
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment