Skip to content

Instantly share code, notes, and snippets.

@LeoDT
Created March 11, 2012 04:44
Show Gist options
  • Save LeoDT/2015068 to your computer and use it in GitHub Desktop.
Save LeoDT/2015068 to your computer and use it in GitHub Desktop.
bootle static file merge
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from bottle import Bottle, debug, get, static_file, run
@get("/<filetype>/<filename:path>")
def static(filetype,filename):
merged_file = "./cache/" + filename
if not os.path.exists(merged_file):
file_list = get_filenames(filename, filetype)
merge(filename, file_list)
return static_file(merged_file, root="./")
def merge(filename, file_list):
try:
files = [open("./static/" + item) for item in file_list]
except IOError, e:
# file not exists
return
merged_path = "./cache/" + filename;
merged = open(merged_path, "w+")
for item in files:
merged.write("========== " + item.name + " ===========")
merged.writelines(item)
merged.write("\n\n\n\n")
item.close()
merged.close()
def get_filenames(filename, filetype):
'''
filename example:
css/reset,global,lib~jqeury.ui.css
'''
filename = filename.replace(filetype + "/", "").replace("." + filetype, "")
return ["%s/%s.%s" % (filetype, single.replace("~", "/"), filetype) for single in filename.split(",")]
debug(True)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment