Commit 513bd9fb by andreas.pelme

Added YUI Compressor for CSS and JavaScript. Thanks Alexander Pugachev for initial patch.


git-svn-id: https://django-compress.googlecode.com/svn/trunk@71 98d35234-f74b-0410-9e22-51d878bdf110
parent e5d0ab0a
......@@ -5,4 +5,10 @@ class FilterBase:
def filter_css(self, css):
raise NotImplementedError
def filter_js(self, js):
raise NotImplementedError
\ No newline at end of file
raise NotImplementedError
class FilterError(Exception):
"""
This exception is raised when a filter fails
"""
pass
\ No newline at end of file
import subprocess
from django.conf import settings
from compress.filter_base import FilterBase, FilterError
BINARY = getattr(settings, 'COMPRESS_YUI_BINARY', 'java -jar yuicompressor.jar')
CSS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_CSS_ARGUMENTS', '')
JS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_JS_ARGUMENTS', '')
class YUICompressorFilter(FilterBase):
def filter_common(self, content, type_, arguments):
command = '%s --type=%s %s' % (BINARY, type_, arguments)
if self.verbose:
command += ' --verbose'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(content)
p.stdin.close()
filtered_css = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply YUI Compressor filter'
raise FilterError(err)
if self.verbose:
print err
return filtered_css
def filter_js(self, js):
return self.filter_common(js, 'js', JS_ARGUMENTS)
def filter_css(self, css):
return self.filter_common(css, 'css', CSS_ARGUMENTS)
\ No newline at end of file
......@@ -91,10 +91,13 @@ def get_output_filename(filename, version):
else:
return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, settings.COMPRESS_VERSION_DEFAULT)
def get_version(mtime):
return str(int(mtime))
def get_version(version):
try:
return str(int(version))
except ValueError:
return str(version)
def remove_files(path, filename, verbosity=0):
def remove_files(path, filename, verbosity=0):
regex = re.compile(r'^%s$' % (os.path.basename(get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'\d+'))))
for f in os.listdir(path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment