forked from Arachnid/bloggart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
minifier.py
121 lines (99 loc) · 4.17 KB
/
minifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import logging
import datetime
import wsgiref.handlers
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.api import memcache
# Fix sys.path
import fix_path
fix_path.fix_sys_path()
import config
import jsmin
import cssmin
HTTP_DATE_FMT = "%a, %d %b %Y %H:%M:%S GMT"
class CssMinifier(webapp.RequestHandler):
"""
Request Handler for Minified CSS
This allows to apply Templating and Minification to CSS Stylesheets.
"""
def get(self, requestedCssFilename):
logging.debug("Requested CSS Path: "+requestedCssFilename)
# Build the Path to the CSS and check if we already have rendered it and is sitting in Memcache ready to use
cssPath = os.path.join( os.path.dirname( __file__ ), 'themes/%s/%s' % (config.theme, requestedCssFilename) )
if not os.path.exists(cssPath):
logging.error("CSS not found: "+cssPath)
# Client Error 4xx - 404 Not Found
self.response.set_status(404)
return
cssRenderingResult = None
# Check if Memcaching is enabled
if config.memcaching:
cssRenderingResult = memcache.get(cssPath)
# In case the Memcache didn't contain the CSS rendered
if cssRenderingResult is None:
logging.debug("Rendering CSS: "+cssPath)
# Render the CSS
cssRenderingResult = template.render(cssPath, {'config' : config})
# Minify ONLY IF NOT Debugging
if config.logging_level != logging.DEBUG:
logging.debug("Minifying CSS: "+requestedCssFilename)
# Minify CSS
cssRenderingResult = cssmin.minify(cssRenderingResult)
# Save in Memcache
memcache.set(cssPath, cssRenderingResult)
# Setting Content-Type as "text/css"
self.response.headers['Content-Type'] = 'text/css'
now = datetime.datetime.now().replace(second=0, microsecond=0)
self.response.headers['Last-Modified'] = now.strftime(HTTP_DATE_FMT)
#self.response.headers['Expires'] = now
logging.debug("Serving Minified CSS: " + cssPath)
# Rendering the Result
self.response.out.write(cssRenderingResult)
class JSMinifier(webapp.RequestHandler):
"""
Request Handler for "./js/*".
This allows to apply Templating and Minification to Javascript.
"""
def get( self, requestedJsFilename ):
logging.debug("Requested Javascript: "+requestedJsFilename)
# Build the Path to the Javascript and check if we already have rendered it and is sitting in Memcache ready to use
jsPath = os.path.join( os.path.dirname(__file__), 'themes/%s/%s' % (config.theme, requestedJsFilename) )
if not os.path.exists(jsPath):
logging.error("Javascript not found: "+jsPath)
# Client Error 4xx - 404 Not Found
self.response.set_status(404)
return
jsRenderingResult = None
if config.memcaching:
jsRenderingResult = memcache.get(jsPath)
# In case the Memcache didn't contain the Javascript rendered
if jsRenderingResult is None:
logging.debug("Rendering Javascript: "+requestedJsFilename)
# Render the Javascript
jsRenderingResult = template.render(jsPath, { 'config' : config })
# Minify ONLY IF NOT Debugging
if config.logging_level != logging.DEBUG:
logging.debug("Minifying Javascript: "+requestedJsFilename)
# Minify Javascript
jsRenderingResult = jsmin.jsmin(jsRenderingResult)
# Save in Memcache
memcache.set(jsPath, jsRenderingResult)
# Setting Content-Type as "text/javascript"
self.response.headers['Content-Type'] = 'application/javascript'
now = datetime.datetime.now().replace(second=0, microsecond=0)
self.response.headers['Last-Modified'] = now.strftime(HTTP_DATE_FMT)
logging.debug("Serving Minified Javascript: "+jsPath)
# Rendering the Result
self.response.out.write( jsRenderingResult )
# Creating a WSGI Application Handler
application = webapp.WSGIApplication([
( '(/.*\.js)$', JSMinifier ),
( '(/.*\.css)$', CssMinifier )
], debug=True )
def main():
logging.getLogger().setLevel(config.logging_level)
# Executing the WSGI Application
wsgiref.handlers.CGIHandler().run( application )
if __name__ == "__main__":
main()