forked from detro/bloggart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifier.py
122 lines (100 loc) · 4.59 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
122
import logging
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
"""
Request Handler for Minified CSS
This allows to apply Templating and Minification to CSS Stylesheets.
"""
class CssMinifier( webapp.RequestHandler ):
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
if config.url_prefix != '':
requestedCssFilename = requestedCssFilename[len(config.url_prefix):]# Strip off prefix
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);
self.error(404); # Client Error 4xx - 404 Not Found
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.info("Minifying CSS: "+requestedCssFilename);
# Minify CSS
try:
cssRenderingResult = cssmin.minify(cssRenderingResult);
except:
logging.warning("CSS Minification failed: " + requestedCssFilename);
# Save in Memcache
memcache.set(cssPath, cssRenderingResult);
# Setting Content-Type as "text/javascript"
self.response.headers['Content-Type'] = 'text/css'
logging.debug("Serving Minified CSS: "+cssPath);
# Rendering the Result
self.response.out.write( cssRenderingResult );
"""
Request Handler for "./js/*".
This allows to apply Templating and Minification to Javascript.
"""
class JSMinifier( webapp.RequestHandler ):
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
if config.url_prefix != '':
requestedJsFilename = requestedJsFilename[len(config.url_prefix):]# Strip off prefix
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);
self.error(404); # Client Error 4xx - 404 Not Found
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.info("Minifying Javascript: " + requestedJsFilename);
# Minify Javascript
try:
jsRenderingResult = jsmin.jsmin(jsRenderingResult);
except:
logging.warning("Javascript Minification failed: " + requestedJsFilename);
# Save in Memcache
memcache.set(jsPath, jsRenderingResult);
# Setting Content-Type as "text/javascript"
self.response.headers['Content-Type'] = 'application/javascript'
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()